AcWing BlueAI【算法赛】. 蓝桥杯第五场算法季度赛第三题
原题链接
简单
#include <bits/stdc++.h>
using namespace std;
#define x first
#define y second
const int N = 13;
int dx[4] = {-1, -1, 1, 1}, dy[4] = {-1, 1, -1, 1};
char qpan[N][N];
int n;
pair<int, int> xiaolan[N * N];
int cnt;
int ans;
void out(char std[N][N])
{
for (int i = 0; i < n; i ++ )
{
for (int j = 0; j < n; j ++ )
cout << std[i][j] << ' ';
cout << endl;
}
cout << endl;
}
void dfs(int a, int b, char tq[N][N], int r)
{
// out(tq);
ans = max(ans, r);
for (int i = 0; i < 4; i ++ )
{
int x = a + dx[i], y = b + dy[i];
if (x + dx[i] >= 0 && x + dx[i] < n && y + dy[i] >= 0 && y + dy[i] < n && x >= 0 && x < n && y >= 0 && y < n && tq[x][y] == 'Q' && tq[x + dx[i]][y + dy[i]] == '.')
{
tq[a][b] = '.';
tq[x][y] = '.';
tq[x + dx[i]][y + dy[i]] = 'L';
dfs(x + dx[i], y + dy[i], tq, r + 1);
tq[a][b] = 'L';
tq[x][y] = 'Q';
tq[x + dx[i]][y + dy[i]] = '.';
}
}
}
int main()
{
cin >> n;
for (int i = 0; i < n; i ++ )
for (int j = 0; j < n; j ++ )
{
cin >> qpan[i][j];
if (qpan[i][j] == 'L') xiaolan[cnt ++ ] = {i, j};
}
for (int i = 0; i < cnt; i ++ )
{
// cout << xiaolan[i].x << ' ' << xiaolan[i].y << endl;
// out(qpan);
dfs(xiaolan[i].x, xiaolan[i].y, qpan, 0);
// cout << i << endl;
}
cout << ans << endl;
return 0;
}