广搜。。深搜是不行的。。
深搜和广搜搜索的顺序不一样。。
一开始 深搜 wa死了
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<cmath>
#include<queue>
#include<map>
#include<set>
using namespace std;
#define inf 0x3f3f3f3f
#define eps 1e-8
#define LL long long
#define ull unsigned long long
#define MP make_pair
#define mnx 505
int n, m;
int d[8][3][2] = { -1, 0, -1, -1, -1, 1, // 'N'
1, 0, 1, -1, 1, 1, // 'S'
0, 1, -1, 1, 1, 1, // 'E'
0, -1, -1, -1, 1, -1, // 'W'
-1, -1, 0, -1, -1, 0, // 'V'
-1, 1, 0, 1, -1, 0, // 'Y'
1, 1, 1, 0, 0, 1, // 'Q'
1, -1, 1, 0, 0, -1}; // 'J'
char ch[mnx][mnx];
int qx[500005], qy[500005]; //wa了好多次,后来发现是队列开太小了。。至少要 mnx * mnx
char dr[8] = {'N', 'S', 'E', 'W', 'V', 'Y', 'Q', 'J'}, qc[500005];
char alp[3][3] = {'V', 'N', 'Y', 'W', ' ', 'E', 'J', 'S', 'Q'};
// -1, 0 代表N, 由于数组的下标不能为负, 就用 [0][1] 代表'N'了。。其他的也是一样
// -1, -1 代表 V, [0][0] 就代表 V
void bfs( int a, int b, char str ){
int k, head = 0, tail = 0;
ch[a][b] = str;
qx[tail] = a, qy[tail] = b, qc[tail++] = str;
while( head < tail ){
int sx = qx[head], sy = qy[head]; char c = qc[head++];
for( int i = 0; i < 8; i++ ){
if( dr[i] == c ){
k = i; break;
}
}
for( int i = 0; i < 3; i++ ){
int a0 = d[k][i][0], b0 = d[k][i][1];
int bx = sx + a0, by = sy + b0;
if( bx > 0 && by > 0 && bx <= n && by <= m && ch[bx][by] == 'X' ){
ch[bx][by] = alp[a0+1][b0+1];
qx[tail] = bx, qy[tail] = by, qc[tail++] = ch[bx][by];
}
}
}
}
int main(){
int ok = 1;
while( scanf("%d%d", &n, &m) != EOF ){
if( ok == 1 ) ok = 0; // 这里 PE了好几次。。主要是题目没有看清楚
else cout<<endl;
for( int i = 1; i <= n; i++ )
for( int j = 1; j<= m; j++ )
cin>>ch[i][j];
char c;
int a, b;
cin>>a>>b>>c;
bfs(a, b, c);
for( int i = 1; i <= n; i++ ){
for( int j = 1; j<= m; j++ ){
cout<<ch[i][j];
}
cout<<endl;
}
}
return 0;
}
代码不难写。。不过,哎,细心啊