TOJ2470 Robot in Maze(宽搜)

来源:互联网 发布:制作广告的软件 编辑:程序博客网 时间:2024/06/06 15:49

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.


宽度优先搜索

#include <cstdio>#include <algorithm>#include <iostream>#include <cmath>#include <cstdlib>#include <cstring>using namespace std;char map[105][105];//记录迷宫int n,m;int visited[105][105][4];//记录有没有到过改状态,第三个下标表示方向int sx[4] = {0,1,0,-1};//决定行走方向int sy[4] = {1,0,-1,0};//同上typedef struct Node{    int x,y,face,step;//face表示面朝哪面,step表示步数} node;node s,e;//s表示起点,e表示终点node queue[160000];//队列int judge(int x,int y){//判断该位置能否到达    return x >=0 && x < n && y >= 0 && y < m && map[x][y] != '#';}int BFS();int main(){    int N;    cin >> N;    while (N--){        scanf("%d%d",&n,&m);        memset(visited,0,sizeof(visited));        for (int i = 0;i < n;i++){            for (int j = 0;j < m;j++){                cin >> map[i][j];                if (map[i][j] == 'S'){                    s.x = i;                    s.y = j;                    s.step = 0;                    s.face = 3;                }                if (map[i][j] == 'T'){                    e.x = i;                    e.y = j;                }            }        }        printf("%d\n",BFS());    }    return 0;}int BFS(){    queue[0] = s;    visited[s.x][s.y][3] = 1;    int top = 1,tx,ty;    for (int i = 0;i < top;i++){        //go forward        tx = queue[i].x + sx[queue[i].face];        ty = queue[i].y + sy[queue[i].face];        if (judge(tx,ty) && !visited[tx][ty][queue[i].face]){            if (map[tx][ty] == 'T') return queue[i].step + 1;            queue[top].x = tx;            queue[top].y = ty;            queue[top].face = queue[i].face;            queue[top].step = queue[i].step + 1;            visited[tx][ty][queue[top].face] = 1;            top ++;        }        //turnleft        int face = (queue[i].face + 3) % 4;        tx = queue[i].x,ty = queue[i].y;        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][queue[top].face] = 1;            top ++;        }        //turnright        face = (queue[i].face + 1) % 4;        tx = queue[i].x,ty = queue[i].y;        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][queue[top].face] = 1;            top ++;        }    }    return -1;}


阅读全文
'); })();
0 0
原创粉丝点击
热门IT博客
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 华清池离兵马俑多远 华清池里的酒店 华清池博物馆 华清池表演时间 华清池公交车 华清池的门票是多少 新华清池 华清池疗养院 华清池景区电话 华清池好不好 兵马俑离华清池有多远 华清池需要门票吗 火车站华清池 华清池 表演 轩辕变华清池 兵马俑华清池联票 华清池周围的酒店 华清池门票是多少 犀浦华清池 兵马俑离华清池多远 华清池门票攻略 西安住宿推荐 西安中旅 西安一日游景点 西安附近一日游 西安周边游玩 西安周边自驾游景点 离西安近的旅游城市 西安市区旅游景点 西安市区旅游攻略 西安市区一日游攻略 西安游5 西安周边游玩一日游 西安城内景点 西安附近的旅游景点 西安一日游哪里好玩 西安市内一日游 西安周边一日游好去处 西安市区一日游 西安周边一日游景点推荐 西安旅游股票