蓝桥杯练习赛----BlueAI【算法赛】(附70分debug测试集)
测试点:
很好看出来dfs,不过有一个小细节需要注意,要不然就卡70分,卡了我好久啊,题目还是做少了
.......
...Q.Q.
..L....
.Q.Q.Q.
.......
.......
.......
用这个一试便知
代码附上:
#include <iostream>
#include <vector>
using namespace std;
struct node{
int x, y;
};
bool flag;
vector<node> pos;
char graph[15][15];
int dx[] = {-1, 1, 1, -1}, dy[] = {1, 1, -1, -1}, ans = 0, res = 0, N;
void dfs(int x, int y){
for(int i = 0; i < 4; i++){
int nx = x + dx[i], ny = y + dy[i];
int nx_ = nx + dx[i], ny_ = ny + dy[i];
if(nx_ >= 1 && ny_ >= 1 && nx_ <= N && ny_ <= N && graph[nx_][ny_] == '.' && graph[nx][ny] == 'Q'){
res++;
graph[nx][ny] = '.';
dfs(nx + dx[i], ny + dy[i]);
graph[nx][ny] = 'Q';
res--;
}
}
if(res > ans) ans = res;
}
int main()
{
// 请在此输入您的代码
cin >> N;
for(int i = 1; i <= N; i++){
for(int j = 1; j <= N; j++){
cin >> graph[i][j];
if(graph[i][j] == 'L') pos.push_back({i, j});
}
}
for(int i = 0; i < pos.size(); i++){
graph[pos[i].x][pos[i].y] = '.';
dfs(pos[i].x, pos[i].y);
graph[pos[i].x][pos[i].y] = 'L';
}
cout << ans;
return 0;
}