Maze, more efficient implementation of C ++ code BFS

Source: Internet
Author: User

In the same BFs, this method consumes less memory.

Copy others' code and learn!

Description:

Sun's school holds a computer festival every year. This year's Computer Festival has a new interesting competition project called Chuang maze.
Sun's roommate designed the maze for the Computer Festival, So Sun asked Sun to help calculate the minimum number of steps out of the maze.
By knowing the minimum number of steps, you can help control the difficulty of the game and remove some maps without reaching the end point.
The competition rule is: From the origin point () to the end point (n-1, n-1), you can only go in the left and right directions, only in the given matrix.

Input:

Multiple groups of data are input.
Input N (0 <n <= 100) for each group of data, and then input the 01 matrix of N * n. 0 indicates that there is no obstacle in the grid, and 1 indicates that there is obstacle.
Note: If the origin and endpoint in the input are 1, the maze is inaccessible.

Output:

Output the shortest steps of the maze for each group of inputs. If the input cannot reach, the output is-1.

Sample input:
20 10 050 0 0 0 01 0 1 0 10 0 0 0 00 1 1 1 01 0 1 0 0
Sample output:
28
#include<stdio.h>
#include<queue>
#define INF 0x7fffffff
using namespace std;
struct S {
int x, y;
S(int i, int j) {
x = i, y = j;
}
};
int M[102][102], D[102][102], n, i, j, t;
int main() {
while (~scanf("%d", &n)) {
for (i = 0; i < n; ++i)
for (j = 0; j < n; ++j)
D[i][j] = INF,scanf("%d", &M[i][j]);
queue<S> r;
--n;
if (M[0][0] || M[n][n]) {
puts("-1");
continue;
}
r.push(S(0, 0));
D[0][0] = 0;
while (!r.empty()) {
i = r.front().x, j = r.front().y;
r.pop();
t = D[i][j] + 1;
if (i - 1 >= 0 && !M[i - 1][j] && D[i - 1][j] > t)
D[i - 1][j] = t, r.push(S(i - 1, j));
if (j - 1 >= 0 && !M[i][j - 1] && D[i][j - 1] > t)
D[i][j - 1] = t, r.push(S(i, j - 1));
if (i + 1 <= n && !M[i + 1][j] && D[i + 1][j] > t)
D[i + 1][j] = t, r.push(S(i + 1, j));
if (j + 1 <= n && !M[i][j + 1] && D[i][j + 1] > t)
D[i][j + 1] = t, r.push(S(i, j + 1));
}
if (D[n][n] >= INF)
D[n][n] = -1;
printf("%d\n", D[n][n]);
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.