#include<iostream>
#include<queue>
#include<cstring>
using namespace std;
char map[50][50];
int dl[4][2] = {{0, -1}, {-1, 0}, {0, 1}, {1, 0}};
int dr[4][2] = {{0, 1}, {-1, 0}, {0, -1}, {1, 0}};
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};
int sx, sy, ex, ey;
int w, h;
int vis[50][50];
struct Pos{
int x, y, s;
};
int dfs(int x, int y, int d, int step, int dir[][2]) {
int i;
for(i = 0; i < 4; i++) {
int j = ((d - 1 + 4) % 4 + i) % 4;
int nx = x + dir[j][0];
int ny = y + dir[j][1];
if(nx == ex && ny == ey) return step + 1;
else if(nx < 0 || ny < 0 || nx > h || ny > w || map[nx][ny] == '#') continue;
return dfs(nx, ny, j, step + 1, dir);
}
}
int BFS(int sx, int sy) {
queue<Pos> Q;
Pos t = {sx, sy, 1};
Q.push(t);
vis[sx][sy] = 1;
while(!Q.empty()) {
Pos p = Q.front();
Q.pop();
if(p.x == ex && p.y == ey) return p.s;
Pos np;
int i;
for(i = 0; i < 4; i++) {
np.x = p.x + dx[i];
np.y = p.y + dy[i];
np.s = p.s + 1;
if(np.x < 0 || np.y < 0 || np.x > h || np.y > w || map[np.x][np.y] == '#' || vis[np.x][np.y] == 1) continue;
vis[np.x][np.y] = 1;
Q.push(np);
}
}
return -1;
}
int main() {
int n;
cin >> n;
while(n--) {
int d1, d2;
cin >> w >> h;
cin.get();
int i, j;
for(i = 0; i < h; i++) {
for(j = 0; j < w; j++) {
cin >> map[i][j];
if(map[i][j] == 'S') {
sx = i;
sy = j;
}
else if(map[i][j] == 'E') {
ex = i;
ey = j;
}
}
}
if(sx == 0) {
d1 = 3;
d2 = 3;
}
else if(sx == h - 1) {
d1 = 1;
d2 = 1;
}
else if(sy == 0) {
d1 = 2;
d2 = 0;
}
else if(sy == w - 1) {
d1 = 0;
d2 = 2;
}
cout << dfs(sx, sy, d1, 1, dl) << ' ';
cout << dfs(sx, sy, d2, 1, dr) << ' ';
memset(vis, 0, sizeof(vis));
cout << BFS(sx, sy) << endl;
}
return 0;
}
第一次做,看了题解,代码是上面这样的。
很久以后自己重新做了一遍,代码是下面这样的。。。
本题的难点就在于方向的控制吧
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
int h, w;
char map[50][50];
int fol[4][2] = {0, -1, -1, 0, 0, 1, 1, 0};
int fori[4][2] = {0, 1, -1, 0, 0, -1, 1, 0};
int sx, sy, ex, ey;
int l, r, s;
struct p {
int x, y, num;
};
void dfsl(int x, int y, int dir, int step) {
if(l) return;
if(x == ex && y == ey) {
l = step + 1;
return;
}
int i, j;
for(i = 0, j = dir; i < 4; i++, j = (j + 1) % 4) {
int tx = x + fol[j][0];
int ty = y + fol[j][1];
if(tx < 1 || tx > h || ty < 1 || ty > w) continue;
if(map[tx][ty] == '#') continue;
dfsl(tx, ty, (j + 3) % 4, step + 1);
}
}
void dfsr(int x, int y, int dir, int step) {
if(r) return;
if(x == ex && y == ey) {
r = step + 1;
return;
}
int i, j;
for(i = 0, j = dir; i < 4; i++, j = (j + 1) % 4) {
int tx = x + fori[j][0];
int ty = y + fori[j][1];
if(tx < 1 || tx > h || ty < 1 || ty > w) continue;
if(map[tx][ty] == '#') continue;
dfsr(tx, ty, (j + 3) % 4, step + 1);
}
}
void bfs() {
queue<p> q;
p t;
map[sx][sy] = '#';
t.x = sx;
t.y = sy;
t.num = 0;
q.push(t);
while(!q.empty()) {
int i;
p tt = q.front();
q.pop();
if(tt.x == ex && tt.y == ey) {
s = tt.num + 1;
return;
}
for(i = 0; i < 4; i++) {
p ttt;
ttt.x = tt.x + fol[i][0];
ttt.y = tt.y + fol[i][1];
if(ttt.x < 1 || ttt.x > h || ttt.y < 1 || ttt.y > w) continue;
if(map[ttt.x][ttt.y] == '#') continue;
map[ttt.x][ttt.y] = '#';
ttt.num = tt.num + 1;
q.push(ttt);
}
}
}
int main() {
int T;
scanf("%d", &T);
while(T--) {
scanf("%d %d", &w, &h);
getchar();
int i, j;
for(i = 1; i <= h; i++) {
for(j = 1; j <= w; j++) {
scanf("%c", &map[i][j]);
if(map[i][j] == 'S') sx = i, sy = j;
else if(map[i][j] == 'E') ex = i, ey = j;
}
getchar();
}
int ordir; //original direction
if(sx == h) ordir = 0;
else if(sy == 1) ordir = 1;
else if(sx == 1) ordir = 2;
else if(sy == w) ordir = 3;
l = 0;
dfsl(sx, sy, ordir, 0);
r = 0;
dfsr(sx, sy, ordir, 0);
s = 0;
bfs();
printf("%d %d %d\n", l, r, s);
}
return 0;
}