大体题意:
给你一个10×10的棋盘,有白棋和黑棋,它们只能落在#上,白棋要想吃黑棋,必须相邻对角线有个黑棋,并且黑棋通用的对角线位置不能有棋,求最多吃多少?
思路:
直接回溯搜索即可!不用vis数组,改变棋盘,在回溯回来即可!
详细见代码:
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#define ps push_back
#define mr make_pair
#define fi first
#define se second
using namespace std;
char s[17][17];
vector<pair<int,int> >v;
int ans = 0;
const int dx[] = {1,-1,1,-1};
const int dy[] = {1,-1,-1,1};
bool init(int x,int y){
return x >= 0 && x < 10 && y >= 0 && y < 10;
}
void dfs(int x,int y,int cur){
bool ok = 0;
for (int i = 0 ; i < 4 ; ++i){
int xx = dx[i] + x;
int yy = dy[i] + y;
int xx2 = dx[i]*2 + x;
int yy2 = dy[i]*2 + y;
if (!init(xx,yy) || !init(xx2,yy2) )continue;
if (s[xx2][yy2] != '#')continue;
if (s[xx][yy] != 'B')continue;
ok = 1;
char ch = s[x][y];
s[x][y] = '#';
s[xx][yy] = '#';
dfs(xx2,yy2,cur+1);
s[xx][yy] = 'B';
s[x][y] = ch;
}
if (!ok) ans = max(ans,cur);
}
int main(){
int T;
scanf("%d",&T);
while(T--){
v.clear();
for (int i = 0; i < 10; ++i){
scanf("%s",s[i]);
for (int j = 0; j < 10; ++j){
if (s[i][j] == 'W')v.ps(mr(i,j));
}
}
int len = v.size();
ans = 0;
for (int i = 0; i < len; ++i){
s[v[i].fi][v[i].se] = '#';
dfs(v[i].fi,v[i].se,0);
s[v[i].fi][v[i].se] = 'W';
}
printf("%d\n",ans);
}
return 0;
}