Robot in Maze----BFS

来源:互联网 发布:android 网络请求mvp 编辑:程序博客网 时间:2024/05/22 14:23

Description

There is a robot trapped in the maze. Now you have to send out some instructions, telling it how to reach its destination.

The maze is an M * N grid. Some of the cells are empty, while others are occupied by the wall. Of course the robot can't move into the wall, and the robot can't move outside the grid too. The robot can only accept three commands: TURN LEFT, TURN RIGHT and GO. The robot may face to North, South, East or West during the movement. When it receive a TURN LEFT command, it will rotate 90 degree to the left. That is, if it faces to east before the command, it will face to north after the TURN LEFT command. The TURN RIGHT command is almost the same, except that the direction is opposite. When receive the GO command, the robot will move 1 unit towards its orientation, unless there is a nonempty cell in front of it.

Please note the robot is always face to north at the beginning, i.e., face to the upper border in the maze map. (The maze map will be described below.)

You want to use minimum number of instructions, so you should write a program for help.

Input

The first line of the input is the number of test cases.

The first line of each test case contains two integers M and N, indicating the size of the maze. There are M lines followed, each line contains exactly N characters, describing an M * N maze. The '#' indicating the wall, the '.' indicating the empty cell, the 'S' and 'T' indicating the start point and the destination of the robot respectively. There are no other characters in the maze map.

The orientation of the maze map is just the same as the common sense. That is, the upper-left corner of the maze map indicating the north-west direction, and the lower-right corner indicating the south-east.

You can assume 1 ≤ M ≤ 100 and 1 ≤ N ≤ 100. There is only one 'S' and one 'T' in the maze.

Output

Output one line for each test case, indicating the minimum number of instructions needed. Or output -1 if it's impossible to reach the robot's destination.

Sample Input

25 5######...##.#.##S#T######4 5#.#.##.#.##S#T######

 

Sample Output

8-1

 

Hint

The best instruction sequence for the first sample test case should be: GO, GO, TURN RIGHT, GO, GO, TURN RIGHT, GO, GO. And the length is 8. 


题目解析:

          题目要找最少的操作数,可以用BFS遍历的方法进行搜索,即从起点开始进行BFS,对每一个新到达的位置记录当前的时间,这样当第一次到达终点的时候就是最早到达的时候。需要注意的是,机器人所在的地点和方向是完全不同的状态,因此机器人在一个地点应该有四种状态,即面向东,面向南,面向西,面向北。因此,表示机器人所在未知的数组应该为visited[100][100][4],多v户的维度用来表示机器人所在方向


#include<iostream>#include<cstdio>#include<cstring>#include<cstdlib>#include<algorithm>using namespace std;char map[200][200];//读入迷宫所需的数组;int  visited[200][200][4];//表示该位置是否被访问int row,col;//行,列;int sx[4]={-1,0,1,0};//行走方向判断的辅助数组 具体使用的方式为(北[0/-1],东[1/0],南[2/1],西[3/0]);int sy[4]={0,1,0,-1};//解释同上typedef struct Node{int x;int y;int face;//记录所面对的方向; int step;//记录步数; }node;//机器人所走过的每一个节点,记录坐标,朝向以及当前的步数; node s,e;//s指的是起点,e指的是终点node queue[16000];//BFS所需要的队列;int judge(int x,int y){return x>=0&&x<row&&y>=0&&y<col&&map[x][y]!='#';} int BFS(){int i,j,k,top,tx,ty,ex,ey,face;ex=e.x,ey=e.y;//记录终点的位置queue[0]=s;//将起点加入队列 visited[s.x][s.y][0]=1;top=1;for(i=0;i<top;i++){//开始搜索    //向前进tx=queue[i].x+sx[queue[i].face];//使用辅助数组,简化代码ty=queue[i].y+sy[queue[i].face];face=queue[i].face;if(judge(tx,ty)&&!visited[tx][ty][queue[i].face]){//并且满足该点未到达时候     queue[top].x=tx; queue[top].y=ty; queue[top].face=queue[i].face; queue[top].step=queue[i].step+1; visited[tx][ty][face]=1; if(map[tx][ty]=='T'){//判断是否目标      return queue[top].step;  }  top++;}//向右转,具体的方法同上;tx=queue[i].x;ty=queue[i].y;face=(queue[i].face+1)%4;if(!visited[tx][ty][face]) { queue[top].x=tx; queue[top].y=ty; queue[top].face=face; queue[top].step=queue[i].step+1; visited[tx][ty][face]=1; if(map[tx][ty]=='T'){//判断是否目标      return queue[top].step;  }  top++;}//向左转,具体的方法同上tx=queue[i].x;ty=queue[i].y;face=(queue[i].face+3)%4;if(!visited[tx][ty][face]) { queue[top].x=tx; queue[top].y=ty; queue[top].face=face; queue[top].step=queue[i].step+1; visited[tx][ty][face]=1; if(map[tx][ty]=='T'){//判断是否目标      return queue[top].step;  }  top++;}}    return -1;//即无法找到目标 }int main(){int n,i,j,k;char c;scanf("%d",&n);while(n--){scanf("%d%d",&row,&col);//读入行和列getchar();//抛弃多余的回车符;for(i=0;i<row;i++){for(j=0;j<col;j++){ c=getchar(); map[i][j]=c; if(map[i][j]=='S'){//记录起始位置    s.x=i;s.y=j;  } else    if(map[i][j]=='T'){//记录结束位置e.x=i;e.y=j; } }  getchar();//消除掉多余的空格符; }s.face=0;s.step=0;memset(visited,0,sizeof(visited));printf("%d\n",BFS());  }   return 0;}