Robot in Maze

Robot in Maze

Time Limit:1000MS  Memory Limit:65536K
Total Submit:42 Accepted:15

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

2
5 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.

Source

Source

Problem Id:1319  User Id:bingshen
Memory:5112K  Time:45MS
Language:G++  Result:Accepted

  • Source

    #include<stdio.h>
    #include<queue>
    #include<algorithm>
    
    using namespace std;
    
    #define GO 1
    #define LEFT 2
    #define RIGHT 3
    #define U 1
    #define D 2
    #define L 3
    #define R 4
    
    struct node
    {
    	int x;
    	int y;
    	int step;
    	int face;
    };
    bool used[1005][1005][5];
    char map[1005][1005];
    int n,m,sx,sy,ex,ey;
    
    void TRUE(int x,int y)
    {
    	int i;
    	for(i=0;i<5;i++)
    		used[x][y][i]=true;
    }
    
    bool judge(int x,int y)
    {
    	if(x>=0&&x<n&&y>=0&&y<m)
    		return true;
    	else
    		return false;
    }
    
    void bfs()
    {
    	int i;
    	node k1,k2;
    	queue<node>q;
    	k1.x=sx;
    	k1.y=sy;
    	k1.face=U;
    	k1.step=0;
    	q.push(k1);
    	while(!q.empty())
    	{
    		k2=q.front();
    		q.pop();
    		if(k2.x==ex&&k2.y==ey)
    		{
    			printf("%d/n",k2.step);
    			return;
    		}
    		else
    		{
    			for(i=1;i<=3;i++)
    			{
    				if(i==GO)
    				{
    					if(k2.face==U)
    					{
    						if(judge(k2.x,k2.y)&&!used[k2.x][k2.y][k2.face])
    						{
    				//			used[k2.x][k2.y][k2.face]=true;
    							k1.face=U;
    							k1.step=k2.step+1;
    							k1.x=k2.x-1;
    							k1.y=k2.y;
    							q.push(k1);
    						}
    					}
    					else if(k2.face==D)
    					{
    						if(judge(k2.x,k2.y)&&!used[k2.x][k2.y][k2.face])
    						{
    				//			used[k2.x][k2.y][k2.face]=true;
    							k1.face=D;
    							k1.step=k2.step+1;
    							k1.x=k2.x+1;
    							k1.y=k2.y;
    							q.push(k1);
    						}
    					}
    					else if(k2.face==L)
    					{
    						if(judge(k2.x,k2.y)&&!used[k2.x][k2.y][k2.face])
    						{
    				//			used[k2.x][k2.y][k2.face]=true;
    							k1.face=L;
    							k1.step=k2.step+1;
    							k1.x=k2.x;
    							k1.y=k2.y-1;
    							q.push(k1);
    						}
    					}
    					else if(k2.face==R)
    					{
    						if(judge(k2.x,k2.y)&&!used[k2.x][k2.y][k2.face])
    						{
    				//			used[k2.x][k2.y][k2.face]=true;
    							k1.face=R;
    							k1.step=k2.step+1;
    							k1.x=k2.x;
    							k1.y=k2.y+1;
    							q.push(k1);
    						}
    					}
    				}
    				if(i==LEFT)
    				{
    					if(k2.face==U)
    					{
    						if(judge(k2.x,k2.y)&&!used[k2.x][k2.y][k2.face])
    						{
    				//			used[k2.x][k2.y][k2.face]=true;
    							k1.face=L;
    							k1.step=k2.step+1;
    							k1.x=k2.x;
    							k1.y=k2.y;
    							q.push(k1);
    						}
    					}
    					else if(k2.face==D)
    					{
    						if(judge(k2.x,k2.y)&&!used[k2.x][k2.y][k2.face])
    						{
    				//			used[k2.x][k2.y][k2.face]=true;
    							k1.face=R;
    							k1.step=k2.step+1;
    							k1.x=k2.x;
    							k1.y=k2.y;
    							q.push(k1);
    						}
    					}
    					else if(k2.face==L)
    					{
    						if(judge(k2.x,k2.y)&&!used[k2.x][k2.y][k2.face])
    						{
    				//			used[k2.x][k2.y][k2.face]=true;
    							k1.face=D;
    							k1.step=k2.step+1;
    							k1.x=k2.x;
    							k1.y=k2.y;
    							q.push(k1);
    						}
    					}
    					else if(k2.face==R)
    					{
    						if(judge(k2.x,k2.y)&&!used[k2.x][k2.y][k2.face])
    						{
    				//			used[k2.x][k2.y][k2.face]=true;
    							k1.face=U;
    							k1.step=k2.step+1;
    							k1.x=k2.x;
    							k1.y=k2.y;
    							q.push(k1);
    						}
    					}
    				}
    				if(i==RIGHT)
    				{
    					if(k2.face==U)
    					{
    						if(judge(k2.x,k2.y)&&!used[k2.x][k2.y][k2.face])
    						{
    				//			used[k2.x][k2.y][k2.face]=true;
    							k1.face=R;
    							k1.step=k2.step+1;
    							k1.x=k2.x;
    							k1.y=k2.y;
    							q.push(k1);
    						}
    					}
    					else if(k2.face==D)
    					{
    						if(judge(k2.x,k2.y)&&!used[k2.x][k2.y][k2.face])
    						{
    				//			used[k2.x][k2.y][k2.face]=true;
    							k1.face=L;
    							k1.step=k2.step+1;
    							k1.x=k2.x;
    							k1.y=k2.y;
    							q.push(k1);
    						}
    					}
    					else if(k2.face==L)
    					{
    						if(judge(k2.x,k2.y)&&!used[k2.x][k2.y][k2.face])
    						{
    				//			used[k2.x][k2.y][k2.face]=true;
    							k1.face=U;
    							k1.step=k2.step+1;
    							k1.x=k2.x;
    							k1.y=k2.y;
    							q.push(k1);
    						}
    					}
    					else if(k2.face==R)
    					{
    						if(judge(k2.x,k2.y)&&!used[k2.x][k2.y][k2.face])
    						{
    				//			used[k2.x][k2.y][k2.face]=true;
    							k1.face=D;
    							k1.step=k2.step+1;
    							k1.x=k2.x;
    							k1.y=k2.y;
    							q.push(k1);
    						}
    					}
    					used[k2.x][k2.y][k2.face]=true;
    				}
    			}
    		}
    	}
    	printf("-1/n");
    	return;
    }
    
    int main()
    {
    	int i,t,j;
    	scanf("%d",&t);
    	while(t--)
    	{
    		scanf("%d%d",&n,&m);
    		memset(used,0,sizeof(used));
    		for(i=0;i<n;i++)
    			scanf("%s",map[i]);
    		for(i=0;i<n;i++)
    			for(j=0;j<m;j++)
    			{
    				if(map[i][j]=='#')
    					TRUE(i,j);
    				if(map[i][j]=='S')
    				{
    					sx=i;
    					sy=j;
    		//			TRUE(i,j);
    				}
    				if(map[i][j]=='T')
    				{
    					ex=i;
    					ey=j;
    				}
    			}
    		bfs();
    	}
    	return 0;
    }
    

     

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值