[UVA] 710 - The Game
The Game
The Game |
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 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:
- 1.
- It consists of straight segments, each one being either horizontal or vertical.
- 2.
- 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 file contains descriptions of several different game situations. The first line of each description contains two integers w and h ( ), 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 . 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.
Miguel A. Revilla
2000-02-09
題目類似於連連看的那種配對遊戲,並且限定轉折數。
而這題則是計算最少轉折數。
考慮將題目轉換成路徑問題,如果曾經是相同方向行進時,則花費為 0。
反之花費為 1,則最後的最少花費就是答案。
#include <stdio.h>
#include <string.h>
#include <queue>
#include <algorithm>
using namespace std;
int g[105][105];
int n, m;
void spfa(int x1, int y1, int x2, int y2) {
int dp[105][105][4] = {}, inq[105][105][4] = {};
int dx[] = {0,1,-1,0};
int dy[] = {1,0,0,-1};
int i, j, k, tx, ty, td, x, y, d, cost;
queue<int> X, Y, D;
X.push(x1), Y.push(y1), D.push(-1);
while(!X.empty()) {
x = X.front(), X.pop();
y = Y.front(), Y.pop();
d = D.front(), D.pop();
inq[x][y][d] = 0;
//printf("%d %d %d %d\n", x, y, d, dp[x][y][d]);
for(i = 0; i < 4; i++) {
tx = x+dx[i], ty = y+dy[i], cost = i != d;
if(tx < 0 || ty < 0 || tx > m+1 || ty > n+1)
continue;
if(tx == x2 && ty == y2) {}
else if(g[tx][ty] == 1) continue;
if(dp[tx][ty][i] == 0 || dp[tx][ty][i] > dp[x][y][d]+cost) {
dp[tx][ty][i] = dp[x][y][d]+cost;
if(inq[tx][ty][i] == 0) {
inq[tx][ty][i] = 1;
X.push(tx), Y.push(ty), D.push(i);
}
}
}
}
int mn = 0xffff;
for(i = 0; i < 4; i++)
if(dp[x2][y2][i])
mn = min(mn, dp[x2][y2][i]);
if(mn == 0xffff)
puts("impossible.");
else
printf("%d segments.\n", mn);
}
int main() {
int x1, y1, x2, y2;
int cases = 0;
int i, j, k;
char s[105];
while(scanf("%d %d", &n, &m) == 2 && n) {
while(getchar() != '\n');
memset(g, 0, sizeof(g));
for(i = 1; i <= m; i++) {
gets(s);
for(j = 0; s[j]; j++) {
if(s[j] == 'X')
g[i][j+1] = 1;
}
}
printf("Board #%d:\n", ++cases);
int pairs = 0;
while(scanf("%d %d %d %d", &x1, &y1, &x2, &y2) == 4) {
if(x1+x2+y1+y2 == 0)
break;
swap(x1, y1), swap(x2, y2);
printf("Pair %d: ", ++pairs);
spfa(x1, y1, x2, y2);
}
puts("");
}
return 0;
}