ZOJ 1148 The Game(bfs)
2017-08-17 12:55
316 查看
The Game
Time Limit: 2 Seconds Memory Limit: 65536 KB
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:
![](http://acm.zju.edu.cn/onlinejudge/showImage.do?name=0000%2F1148%2F1148.gif)
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
求路径中含有最少的直线段(水平或竖直),直接bfs即可,不过需要注意:搜索时,在同一个方向,应该一直向前走,直至不能前进为止。具体看代码。
ps:题目中说在连接时,是可以离开矩形游戏板,所以矩形游戏板的四周要加大一圈。
AC代码:
#include <stdio.h>
#include <memory.h>
struct{
int x, y;
} q[3000];
char g[80][80];
int steps[80][80];
int dir[4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
int main(){
int i;
int n = 1;
int sx, sy, dx, dy;
int h, w;
while(scanf("%d%d", &w, &h) && (h || w)){
getchar();
int m = 1;
for(i = 1; i <= h; i++)
gets(&g[i][1]);
//周围加一圈' '
for(i = 0; i < w + 2; i++)
g[0][i] = g[h + 1][i] = ' ';
for(i = 1; i <= h; i++)
g[i][0] = g[i][w + 1] = ' ';
printf("Board #%d:\n", n++);
while(scanf("%d%d%d%d", &sy, &sx, &dy, &dx)){
if(!(sx || sy || dx|| dy)) break;
memset(steps, -1, sizeof(steps));
g[dx][dy] = ' ';//目标结点设置成可达
steps[sx][sy] = 0;
int head = 0;
int tail = 0;
//入队列
q[head].x = sx;
q[head].y = sy;
//队列不为空,且目标结点未求出
while(head <= tail && steps[dx][dy] == -1){
for(i = 0; i < 4; i++){
int x0 = q[head].x + dir[i][0];
int y0 = q[head].y + dir[i][1];
//沿着该方向一直搜索,该方向都可一步到达
while(x0 >= 0 && x0 <= h+1 && y0 >= 0 && y0 <= w+1
&& g[x0][y0] == ' ' && steps[x0][y0] == -1){
tail++;
q[tail].x = x0;
q[tail].y = y0;
steps[x0][y0] = steps[q[head].x][q[head].y] + 1;
//该方向的下一个结点
x0 += dir[i][0];
y0 += dir[i][1];
}
}
head++;
}
g[dx][dy] = 'X';//恢复该结点状态
if (steps[dx][dy] == -1)
printf("Pair %d: impossible.\n", m++);
else
printf("Pair %d: %d segments.\n", m++, steps[dx][dy]);
}
printf("\n");
}
return 0;
}
Time Limit: 2 Seconds Memory Limit: 65536 KB
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:
![](http://acm.zju.edu.cn/onlinejudge/showImage.do?name=0000%2F1148%2F1148.gif)
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
求路径中含有最少的直线段(水平或竖直),直接bfs即可,不过需要注意:搜索时,在同一个方向,应该一直向前走,直至不能前进为止。具体看代码。
ps:题目中说在连接时,是可以离开矩形游戏板,所以矩形游戏板的四周要加大一圈。
AC代码:
#include <stdio.h>
#include <memory.h>
struct{
int x, y;
} q[3000];
char g[80][80];
int steps[80][80];
int dir[4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
int main(){
int i;
int n = 1;
int sx, sy, dx, dy;
int h, w;
while(scanf("%d%d", &w, &h) && (h || w)){
getchar();
int m = 1;
for(i = 1; i <= h; i++)
gets(&g[i][1]);
//周围加一圈' '
for(i = 0; i < w + 2; i++)
g[0][i] = g[h + 1][i] = ' ';
for(i = 1; i <= h; i++)
g[i][0] = g[i][w + 1] = ' ';
printf("Board #%d:\n", n++);
while(scanf("%d%d%d%d", &sy, &sx, &dy, &dx)){
if(!(sx || sy || dx|| dy)) break;
memset(steps, -1, sizeof(steps));
g[dx][dy] = ' ';//目标结点设置成可达
steps[sx][sy] = 0;
int head = 0;
int tail = 0;
//入队列
q[head].x = sx;
q[head].y = sy;
//队列不为空,且目标结点未求出
while(head <= tail && steps[dx][dy] == -1){
for(i = 0; i < 4; i++){
int x0 = q[head].x + dir[i][0];
int y0 = q[head].y + dir[i][1];
//沿着该方向一直搜索,该方向都可一步到达
while(x0 >= 0 && x0 <= h+1 && y0 >= 0 && y0 <= w+1
&& g[x0][y0] == ' ' && steps[x0][y0] == -1){
tail++;
q[tail].x = x0;
q[tail].y = y0;
steps[x0][y0] = steps[q[head].x][q[head].y] + 1;
//该方向的下一个结点
x0 += dir[i][0];
y0 += dir[i][1];
}
}
head++;
}
g[dx][dy] = 'X';//恢复该结点状态
if (steps[dx][dy] == -1)
printf("Pair %d: impossible.\n", m++);
else
printf("Pair %d: %d segments.\n", m++, steps[dx][dy]);
}
printf("\n");
}
return 0;
}
相关文章推荐
- ZOJ 1148 The Game(BFS)
- 队列,广度搜索-ZOJ 1148 The Game (BFS)
- ZOJ 1148 The Game (BFS)
- ZOJ 1148 The Game
- zoj 1148 The Game 一个晚上终于AC!
- ZOJ 1136 Multiple BFS 取模 POJ 1465
- 简单BFS ZOJ 1649 Rescue
- BFS ZOJ 3814 Sawtooth Puzzle
- ZOJ 3781 Paint the Grid Reloaded 缩点+bfs
- ZOJ 3675 Trim the Nails(bfs)
- ZOJ1079-Robotic Jigsaw(bfs)
- ZOJ 2922 Bombs(类似BFS的数组遍历)
- FUZoj 2188:过河I【bfs】
- 浙江大学2015年校赛F题 ZOJ 3865 Superbot BFS 搜索
- ZOJ 3781 Paint the Grid Reloaded (缩点,bfs)
- ZOJ 1136 Multiple( 搜索 BFS )
- ZOJ 1047 Image Perimeters (BFS)
- ZOJ-1649 Rescue BFS (HDU 1242)
- ZOJ 2864 Catch the thief(多条最短路+BFS)
- ZOJ 649 Rescue(优先队列+bfs)