20-1-19-图的深度优先和广度优先遍历-POJ1101

POJ1101

The Game

Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 12249 Accepted: 3760

Description

One morning, you wake up and think: "I am such a good programmer. Why not make some money?’’ So you decide to write a computer game.
The game takes place on a rectangular board consisting of w * h squares. Each square might or might not contain a game piece, as shown in the picture.

One important aspect of the game is whether two game pieces can be connected by a path which satisfies the two following properties:

It consists of straight segments, each one being either horizontal or vertical.

It does not cross any other game pieces.

(It is allowed that the path leaves the board temporarily.)

Here is an example:
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-vZoh3pho-1579706060492)(D:\AFiles\TyporaPictures\1101_1-1579535688515.jpg)]
The game pieces at (1,3) and at (4, 4) can be connected. The game pieces at (2, 3) and (3, 4) cannot be connected; each path would cross at least one other game piece.

The part of the game you have to write now is the one testing whether two game pieces can be connected according to the rules above.

Input

The input contains descriptions of several different game situations. The first line of each description contains two integers w and h (1 <= w,h <= 75), the width and the height of the board. The next h lines describe the contents of the board; each of these lines contains exactly w characters: a “X” if there is a game piece at this location, and a space if there is no game piece.

Each description is followed by several lines containing four integers x1, y1, x2, y2 each satisfying 1 <= x1,x2 <= w, 1 <= y1,y2 <= h. These are the coordinates of two game pieces. (The upper left corner has the coordinates (1, 1).) These two game pieces will always be different. The list of pairs of game pieces for a board will be terminated by a line containing “0 0 0 0”.

The entire input is terminated by a test case starting with w=h=0. This test case should not be procesed.

Output

For each board, output the line “Board #n:”, where n is the number of the board. Then, output one line for each pair of game pieces associated with the board description. Each of these lines has to start with "Pair m: ", where m is the number of the pair (starting the count with 1 for each board). Follow this by “ksegments.”, where k is the minimum number of segments for a path connecting the two game pieces, or “impossible.”, if it is not possible to connect the two game pieces as described above.

Output a blank line after each board.

Sample Input

5 4
XXXXX
X   X
XXX X
 XXX 
2 3 5 3
1 3 4 4
2 3 3 4
0 0 0 0
0 0

Sample Output

Board #1:
Pair 1: 4 segments.
Pair 2: 3 segments.
Pair 3: impossible.

Source

Mid-Central European Regional Contest 1999

  • 题意:

    • 给一张图, 有墙壁和空格. 图高度为第二个数字h, 宽度为第一个数字w
    • 下面输出若干组数据, (xi,yi), (xj, yj)代表两个点的坐标, 问这两个点可以通过几条线段连起来.
    • 小时候玩过的一个小游戏
  • 1<=w,h<=75

  • 广搜

  • 和走迷宫的广搜不同的是这里搜索的直线转弯的次数, 每次从迷宫的一个位置转移到另一个位置时, 这两个位置之间的所有的符合要求的路径都要push进入队列, 其cost均为第一个位置的cost+1.

    for (int i = 0; i < 4; i++){
    			int qa = top.x, qb = top.y;
    			while (ok(qa + dirx[i], qb + diry[i]))
    			{
    				qa += dirx[i]; qb += diry[i];
    				q.push(Node(qa, qb, top.cost + 1));
    				vis[qa][qb] = true;
    			}
    		}
    
#pragma warning(disable:4996)
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include<iostream>
using namespace std;
const int maxn = 105;

char g[maxn][maxn];
int vis[maxn][maxn];
int n, m;
int sx, sy, ex, ey;

int kase = 0, cnt;

int dirx[] = { -1,1,0,0 };
int diry[] = { 0,0,1,-1 };

bool ok(int xx, int yy) {
	if (xx <= n + 1 && yy <= m + 1 && xx >= 0 && yy >= 0 && g[xx][yy] != 'X' && !vis[xx][yy])return 1;
	return 0;
}

struct Node {
	int x, y, cost;
	Node(int x_ = 0, int y_ = 0, int cost_ = 0) :x(x_), y(y_), cost(cost_) {}
};

queue<Node>q;

int bfs(){
	while (!q.empty()) {
		q.pop();
	}
	q.push(Node(sx, sy, 0));
	vis[sx][sy] = true;
	while (!q.empty()){
		Node top = q.front();
		q.pop();

		if (top.x == ex && top.y == ey){
			return top.cost;
		}

		for (int i = 0; i < 4; i++){
			int qa = top.x, qb = top.y;
			while (ok(qa + dirx[i], qb + diry[i]))
			{
				qa += dirx[i]; qb += diry[i];
				q.push(Node(qa, qb, top.cost + 1));
				vis[qa][qb] = true;
			}
		}
	}
	return -1;
}
int main()
{
	while (scanf("%d%d", &m, &n) && (n || m)) {
		memset(g, 0, sizeof(g));

		cnt = 0;

		printf("Board #%d:\n", ++kase);
		for (int i = 1; i <= n; i++) {
			for (int j = 0; j <= m; j++) {
				scanf("%c", &g[i][j]);
			}
		}
		while (scanf("%d%d%d%d", &sy, &sx, &ey, &ex) && (sx || sy || ex || ey)) {
			memset(vis, 0, sizeof(vis));
			g[ex][ey] = ' ';
			int ans = bfs();
			if (ans != -1)
				printf("Pair %d: %d segments.\n", ++cnt, ans);
			else
				printf("Pair %d: impossible.\n", ++cnt);
			g[ex][ey] = 'X';
		}
		printf("\n");
	}
}
		if (ans != -1)
			printf("Pair %d: %d segments.\n", ++cnt, ans);
		else
			printf("Pair %d: impossible.\n", ++cnt);
		g[ex][ey] = 'X';
	}
	printf("\n");
}

}

发布了21 篇原创文章 · 获赞 8 · 访问量 2718

猜你喜欢

转载自blog.csdn.net/shen253135371/article/details/104073450
今日推荐