Maze

Description

Master Tang is kidnapped by a monster and put in a maze, and Mr. Sha wants to rescue him.

The maze is an n*m matrix. There are two types rooms in the maze, 1 for safe and 0 for dangerous. Of course, Mr. Sha can only stay in the safe room. Mr. Sha is now in the top left corner room, the room (1,1). The lower right corner room is room (n,m)

Mr. Sha can move to another safe room in up, down, left or right direction. Each move takes 1 unit of time. Mr. Sha wants to find Master Tang as soon as possible. 

Can you help Mr. Sha to count the least time to get to the room where Master Tang is in?

Input

The first line contains an integer t (0<t<=10), which means t test cases followed.

The first line of each test case contains 2 integers n (1≤n≤120) and m (1≤n≤120), which is the size of the maze. The next n lines input the maze, each line contains m number (0 or 1) seperated by one space. 

The next line contains 2 integers x (1≤n≤120) and y (1≤n≤120), which means Master Tang is in the room (x,y).

Output

For each test case, output the answer in a single line: the least time for Mr. Sha to get to Master Tang. If Mr. Sha can't get to Mast Tang, output -1. 

题目解释:对于一个用二维数组表示的迷宫,1表示可行,0表示不可行,而我们行走的路线只能是上、下、左、右。求解从左上角位置走到指定位置所需要的最短路径,要是没有解则输出-1,有解则输出最小解,也就是最短路径

解题思路:对于这道题使用广度搜索算法。创建一个vector<point> c[14400],这是一个数组,每个数组的对象是一个vector,vector存放的是point(坐标点)。从左上角开始,逐个检测当前点的上下左右四个点是否可行(也就是迷宫矩阵对应的值为1),把可行点push_back在c[k+1]里面,同时将该点的迷宫值置0.最终能够到达目标点(targetx,targety)则输出k的值,否则输入-1。原理就是使用广度搜索看是否能够达到目标点,而广度搜索的深度就是所求的最短路径。


#include <iostream>
#include <vector>
using namespace std;
int caseNum, size_n, size_m;
int maze[125][125];
int next_pos[4][2] = {{1,0},{-1,0},{0,1},{0,-1}};
struct point{
    int x, y;
};
bool canMove(int x,int y){
    if (x >= 0 && x <size_n && y >=0 & y <size_m && maze[x][y] == 1) {
        return true;
    }
    return false;
}
int main(int argc, const char * argv[]) {
    // insert code here...
    cin >> caseNum;
    while (caseNum--) {
        cin >> size_n >> size_m;
        for (int i = 0; i < size_n; i++) {  // 初始化迷宫地图
            for (int j = 0; j < size_m; j++) {
                cin >> maze[i][j];
            }
        }
        int targetx,targety;
        bool flag = false;
        cin >> targetx >> targety;
        targetx --;
        targety --;
        vector<point> v[14400];
        point v0 ;
        v0.x = 0;
        v0.y = 0;
        v[0].push_back(v0);  // 存储起始点位置
        int k = 0;
        while (1) {
            if (v[k].size() == 0) {  
                break;
            }
            vector<point>::iterator it;
            for (it = v[k].begin(); it!= v[k].end(); it++) {
                point current = *it;
                for (int i = 0; i < 4; i++) {
                    point tmp;
                    tmp.x = current.x + next_pos[i][0];
                    tmp.y = current.y + next_pos[i][1];
                    if (tmp.x == targetx && tmp.y == targety) { //下一个点为目标点,则跳出,说明到达了目的地
                        flag = true;                      
                        break;
                    }
                    if (canMove(tmp.x,tmp.y)) {        // 下一个点能够达到,则将该点存入到v[k+1]
                        v[k+1].push_back(tmp);
                        maze[tmp.x][tmp.y] = 0;
                    }
                    
                }
            }
            k ++;
            if (flag) {
                break;
            }
        }
        if(flag) cout << k << endl;
        else cout << -1 << endl;
        
    }
    return 0;
}


你可能感兴趣的:(算法,sicily)