<OJ_Sicily>Maze

来源:互联网 发布:电子琴和吉他知乎 编辑:程序博客网 时间:2024/06/03 17:43

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,1)处,然后到达题目给出的终点(x,y),求是否能够到达指定终点,要是不能到达则输出-1,能够到达便输出最短使用时间(其实也是最短路径)

解题思路:使用广度搜索算法求解。这里使用vector<point> 变量存放点,作用有点类似于队列,进行广度搜索

#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){   // 位置(x,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].push_back(tmp);                        maze[tmp.x][tmp.y] = 0;                    }                                    }            }            k ++;            if (flag) {                break;            }        }        if(flag) cout << k << endl;        else cout << -1 << endl;            }    return 0;}


0 0
原创粉丝点击