P1101 单词方阵
没看清题,说是走了一个方向不换方向,导致一开始写bfs狂WA(因为出现了一个后继对多个前驱的情况)
#include<iostream>
#include<cstring>
#include<queue>
using namespace std;
#define PII pair<int, int>
#define x first
#define y second
const int N = 110, M = 10010;
char g[N][N];
int st[N][N];
string s = "yizhong";
int n;
int dx[] = {1, 0, -1, 0, 1, -1, 1, -1}, dy[] = {0, 1, 0, -1, 1, 1, -1, -1};
vector<PII> res;
void dfs(int x, int y, int u, int k){
if(u == s.size()){
for(auto t : res) st[t.x][t.y] = 1;
return;
}
if(x < 0 || y < 0 || x >= n || y >= n) return;
if(s[u] != g[x][y]) return;
res.push_back({x, y});
if(u == 0)
for(int i = 0; i < 8; i ++){
int a = x + dx[i], b = y + dy[i];
dfs(a, b, u + 1, i);
}
else
dfs(x + dx[k], y + dy[k], u + 1, k);
res.pop_back();
}
int main(){
cin >> n;
for(int i = 0; i < n; i ++) cin >> g[i];
for(int i = 0; i < n; i ++)
for(int j = 0; j < n; j ++)
if(g[i][j] == 'y') dfs(i, j, 0, 0);
for(int i = 0; i < n; i ++){
for(int j = 0; j < n; j ++)
if(st[i][j]) cout << g[i][j];
else cout << '*';
cout << endl;
}
return 0;
}