Simple BFS: Miner's Adventures

Source: Internet
Author: User

Minecraft is a well-known independent game, the world has a very high popularity, players can build their own servers for the world to play, so some players to build a number of dedicated small game server, providing a variety of small games, which also provides more play for Minecraft. Here is one of the little games: Miner Adventures.

Now you control the characters in a NXM field, the upper-left corner of the square is (0, 0), the lower-right corner is (n–1, m–1), your first position is (SX, SY), and your home position is (EX, EY). As a miner, you already have several sets of iron and diamonds, so you're rushing back home. But some of the lattice is set up a trap, if go to the top will be shattered, immediately Game over. You certainly don't want to Game over, so you have to avoid these squares.

Assuming that each unit of time can move up, down, left and right a lattice, please calculate the shortest time to go home. Input format

An integer T that indicates how many sets of test data are available.

Next to the T group test data, the first row of each set of test data is two positive integers n, m (1 <= N, M <= 10), indicating the size of the site.

The second line has a positive integer p (0 <= P <= nxm), indicating how many squares have been set to trap.

The next P line, each row is two integers X, Y, which represents the trap coordinates.

The last line is four integer SX, SY, EX, EY, which represents the starting position and home location.

Note that walking cannot go beyond the boundaries of a map. Output format

For each set of input data, the output line is one digit, indicating the minimum required home time. If there is a situation where it is impossible to complete the task, output-1. Sample input

1
3 3
2 1 1 1 2 0 0 2-
2
Sample output

4

#include <stdio.h> #include <queue> using namespace std;
	struct N {int x, y;
int t;
};
Queue<n> Q;
int go[4][2] = {-1, 0, 1, 0, 0,-1, 0, 1};
BOOL MARK[10][10];
int maze[10][10];
int n, m; int BFS (int x, int y) {while!
		Q.empty ()) {N now = Q.front ();
		Q.pop ();
			for (int i = 0; i < 4; i++) {int NX = now.x + go[i][0];
			int NY = now.y + go[i][1];
			if (NX < 0 | | | NX >= N | | NY < 0 | | | NY >= m) continue;
			if (maze[nx][ny] = = 1) continue;
			if (mark[nx][ny] = = true) continue;
			Mark[nx][ny] = true;
			N tmp;
			tmp.x = NX;
			TMP.Y = NY;
			TMP.T = now.t + 1;
			Q.push (TMP);
		if (NX = = x && ny = y) return tmp.t;
}} return-1;
	int main () {int T, p, SX, SY, ex, EY;
	scanf ("%d", &t);
		while (t--) {scanf ("%d%d%d", &n, &m, &p);
		int xx, yy;
			for (int i = 0; i < n; i++) {for (int j = 0; J < m; J +) {Maze[i][j] = 0;
			} while (p--) {scanf ("%d%d", &xx, &yy); MAZE[XX][YY] = 1;
			for (int i = 0; i < n; i++) {for (int j = 0; J < m; j) {mark[i][j] = false;
		} scanf ("%d%d%d%d", &sx, &sy, &ex, &ey); while (!
		Q.empty ()) Q.pop ();
		N start;
		Start.x = SX;
		Start.y = sy;
		start.t = 0;
		Q.push (start);
		Mark[sx][sy] = true;
		int rec = BFS (ex, EY);
	printf ("%d\n", rec);
return 0; }


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.