poj 1101 The Game【BFS+思维】

xiaoxiao2021-02-27  104

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:  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.

题意:问点X到点Y,最少需要使用几条线段,间接求最少拐几次弯,注意先给的是y坐标,后给的是x坐标。

思路:

类似于连连看,不过它可以在边界外围一圈行走,需要初始化和把四周字符变一下。需要注意,它走过的点也可能会再次用到,但是从队列里取出的元素不能用了(相当于优先直线搜索,先把最少拐弯搜了,这个换个表达也可以),记录方向来更新线段数。

#include <cstdio> #include <cstring> #include <algorithm> #include <queue> #include <cmath> #define max_n 1010 using namespace std; int mapp[max_n][max_n]; int vis[max_n][max_n]; int X1, Y1, X2, Y2, n, m, cnt; int mov[4][2] = {1, 0, -1, 0, 0, 1, 0, -1}; struct node { int x, y, z; int step; bool friend operator <(node a, node b) { return a.step > b.step; } }now, Next; void bfs(int X, int Y, int Z) { memset(vis, 0, sizeof(vis)); bool flag = false; priority_queue<node> q; now.x = X; now.y = Y; now.z = Z; now.step = 0; q.push(now); while(!q.empty()) { Next = q.top(); q.pop(); if(Next.x == X2 && Next.y == Y2) { flag = true; break; } if(vis[Next.x][Next.y]) continue; vis[Next.x][Next.y] = 1; for(int i = 0; i < 4; i++) { now.x = Next.x + mov[i][0]; now.y = Next.y + mov[i][1]; if(now.x < 0 || now.x > n + 1 || now.y < 0 || now.y > m + 1 || mapp[now.x][now.y] || vis[now.x][now.y] == 1) continue; if(i != Next.z) { now.z = i; now.step = Next.step + 1; q.push(now); } else { now.z = i; now.step = Next.step; q.push(now); } } } if(flag) printf("Pair %d: %d segments.\n", cnt++, Next.step); else printf("Pair %d: impossible.\n", cnt++); } int main() { int p = 1;char ch; while(scanf("%d %d", &m, &n) && (n + m)) { memset(mapp, 0, sizeof(mapp)); //注意初始化 for(int i = 1; i <= n; i++) { getchar(); for(int j = 1; j <= m; j++) { scanf("%c", &ch); if(ch == 'X') mapp[i][j] = 1; else mapp[i][j] = 0; } } cnt = 1; printf("Board #%d:\n", p++); while(scanf("%d %d %d %d", &Y1, &X1, &Y2, &X2) && (X1 + X2 + Y1 + Y2)) { int ant = mapp[X2][Y2]; mapp[X2][Y2] = 0; //终点位置更改一下 bfs(X1, Y1, -1); mapp[X2][Y2] = ant; } printf("\n"); } return 0; }

转载请注明原文地址: https://www.6miu.com/read-13252.html

最新回复(0)