明白题意,S->E,第一个输出左偏到达的步数,第二个输出右偏到达的步数,第三个输出最短路用BFS
左偏的意思是,开始方向任意定义并且在后面都始终不会变,先看左边的格子是否能走,然后逆时针(就是从左边的第一个格子往右偏).
右偏,开始跟左一样,先看右边的格子是否能走,然后顺时针(说白了就是从右边的第一个格子往左偏),
#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
using namespace std;
struct node
{
int x;
int y;
int step;
}que[2100];
int map[50][50];//能走为1 不能为0
int book[50][50];//标记
int flag;//标记变量,是否能找到
int ansl, ansr, ans;//l的步数,r的步数,最短步数
int ex, ey, sx, sy;//S,E的位置
int n, m;//行 列,输入的时候反着
void DfsL(int x,int y, int step, int face)//1,2,3,4,W,N,E,S
{
if(flag==1) return;
if(x==ex&&y==ey){
flag = 1;
ansl = step;
return ;
}
if(face == 1){
if(map[x+1][y]) DfsL(x+1,y,step+1,4);
if(map[x][y-1]) DfsL(x,y-1,step+1,1);
if(map[x-1][y]) DfsL(x-1,y,step+1,2);
if(map[x][y+1]) DfsL(x,y+1,step+1,3);
}else if(face == 2){
if(map[x][y-1]) DfsL(x,y-1,step+1,1);
if(map[x-1][y]) DfsL(x-1,y,step+1,2);
if(map[x][y+1]) DfsL(x,y+1,step+1,3);
if(map[x+1][y]) DfsL(x+1,y,step+1,4);
}else if(face == 3){
if(map[x-1][y]) DfsL(x-1,y,step+1,2);
if(map[x][y+1]) DfsL(x,y+1,step+1,3);
if(map[x+1][y]) DfsL(x+1,y,step+1,4);
if(map[x][y-1]) DfsL(x,y-1,step+1,1);
}else {
if(map[x][y+1]) DfsL(x,y+1,step+1,3);
if(map[x+1][y]) DfsL(x+1,y,step+1,4);
if(map[x][y-1]) DfsL(x,y-1,step+1,1);
if(map[x-1][y]) DfsL(x-1,y,step+1,2);
}
}
void DfsR(int x, int y, int step, int face)
{
if(flag == 1) return;
if(x == ex && y == ey){
flag = 1;
ansr = step;
return;
}
if(face == 1){
if(map[x-1][y]) DfsR(x-1,y,step+1,2);
if(map[x][y-1]) DfsR(x,y-1,step+1,1);
if(map[x+1][y]) DfsR(x+1,y,step+1,4);
if(map[x][y+1]) DfsR(x,y+1,step+1,3);
}else if(face == 2){
if(map[x][y+1]) DfsR(x,y+1,step+1,3);
if(map[x-1][y]) DfsR(x-1,y,step+1,2);
if(map[x][y-1]) DfsR(x,y-1,step+1,1);
if(map[x+1][y]) DfsR(x+1,y,step+1,4);
}else if(face == 3){
if(map[x+1][y]) DfsR(x+1,y,step+1,4);
if(map[x][y+1]) DfsR(x,y+1,step+1,3);
if(map[x-1][y]) DfsR(x-1,y,step+1,2);
if(map[x][y-1]) DfsR(x,y-1,step+1,1);
}else {
if(map[x][y-1]) DfsR(x,y-1,step+1,1);
if(map[x+1][y]) DfsR(x+1,y,step+1,4);
if(map[x][y+1]) DfsR(x,y+1,step+1,3);
if(map[x-1][y]) DfsR(x-1,y,step+1,2);
}
}
void BFS()
{
memset(book,0,sizeof(book));
que[0].x = sx;
que[0].y = sy;
que[0].step = 1;
book[sx][sy] = 1;
int tail = 1;
int head = 0;
int xx, yy, step;
while(head < tail){
xx = que[head].x;
yy = que[head].y;
step = que[head].step;
head++;
if(xx==ex&&yy==ey){
ans = step;
return;
}
if(!book[xx][yy-1]&&map[xx][yy-1]){
book[xx][yy-1] = 1;
que[tail].x = xx;
que[tail].y = yy-1;
que[tail].step = step+1;
tail++;
}
if(!book[xx][yy+1]&&map[xx][yy+1]){
book[xx][yy+1] = 1;
que[tail].x = xx;
que[tail].y = yy+1;
que[tail].step = step+1;
tail++;
}
if(!book[xx-1][yy]&&map[xx-1][yy]){
book[xx-1][yy] = 1;
que[tail].x = xx-1;
que[tail].y = yy;
que[tail].step = step+1;
tail++;
}
if(!book[xx+1][yy]&&map[xx+1][yy]){
book[xx+1][yy];
que[tail].x = xx+1;
que[tail].y = yy;
que[tail].step = step+1;
tail++;
}
}
}
int main()
{
int t;
scanf("%d", &t);
char s[50];
int i, j;
while(t--){
memset(map,0,sizeof(map));
scanf("%d %d", &m, &n);
for(i = 1;i <=n;i++){
cin >> s;
for(j = 0;j < m;j++){
if(s[j] == '.'){
map[i][j+1] = 1;
}else if(s[j] =='S'){
map[i][j+1] = 1;
sx = i;
sy = j+1;
}else if(s[j] == 'E'){
map[i][j+1] = 1;
ex = i;
ey = j+1;
}
}
}
flag = 0;
DfsL(sx,sy,1,1);
flag = 0;
DfsR(sx,sy,1,1);
BFS();
printf("%d %d %d\n", ansl, ansr, ans);
}
return 0;
}