TOJ2470 Robot in Maze(宽搜)

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.


宽度优先搜索

#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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值