[蓝桥杯 2013 省 A] 剪格子
注意事项:读入顺序为m, n
#include <iostream>
using namespace std;
const int N = 11;
int n, m;
int w[N][N];
int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1};
int cnt = 1;
bool st[N][N];
int t;
int res = 1e9;
void dfs(int x, int y, int sum)
{
if(sum == t - sum)
{
res = min(res, cnt);
return ;
}
// cout << sum << endl;
st[x][y] = true;
for(int i = 0; i < 4; i ++)
{
int a = x + dx[i], b = y + dy[i];
if(a < 0 || b < 0 || a >= n || b >= m) continue;
if(st[a][b]) continue;
if(sum + w[a][b] > t - sum) continue;
st[a][b] = true;
cnt ++;
dfs(a, b, sum + w[a][b]);
st[a][b] = false;
cnt --;
}
}
int main ()
{
cin >> m >> n;
for(int i = 0; i < n; i ++)
for(int j = 0; j < m; j ++)
{
cin >> w[i][j];
t += w[i][j];
}
dfs(0, 0, w[0][0]);
cout << res;
return 0;
}
标签:cnt,格子,int,res,sum,st,蓝桥,2013
From: https://www.cnblogs.com/fxc2002/p/17014659.html