The Game
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 8401 | Accepted: 2564 |
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.
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.
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.
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
在一个h * w的方格里,分步着一些小黑点,现在示输入其中两个小黑点的坐标(bx, by)和(ex,ey),问起点(bx, by)到终点(ex,ey)的路径的最少线段数是多少?(这里的线段指的是起点到终点的路径中在同一条直线上的点组成一条线段)如果不存在从起点到终点的路径,输出impossible.
(注意: h * w的方格的外围一层是有效的,即实际的方格为(h + 2) * (w + 2))
(注意: h * w的方格的外围一层是有效的,即实际的方格为(h + 2) * (w + 2))
直接BFS即可
#include <cstdio>
#include <queue>
#include <cstring>
using namespace std ;
int w , h , vis[80][80] , dis[80][80] , dir[2][4] = { { 0 , 1 , -1 , 0 } , { 1 , 0 , 0 , -1 } } ;
char a[80][80] ;
int x1 , x2 , y1 , y2 ;
void bfs( int x , int y )
{
int tmp = x*(w+2)+y , i ;
queue <int> q ;
q.push(tmp) ;
vis[x][y] = 1 ;
while(!q.empty())
{
tmp = q.front() ;
x = tmp/(w+2) ;
y = tmp%(w+2) ;
if(x==x2&&y==y2) return ;
q.pop() ;
for( i = 0 ; i < 4 ; i++ )
{
int nx = x+dir[0][i] ;
int ny = y+dir[1][i] ;
while(nx>=0&&nx<=h+1&&ny>=0&&ny<=w+1&&!vis[nx][ny]&&a[nx][ny]==' ')
{
tmp = nx*(w+2)+ny ;
vis[nx][ny] = 1 ;
dis[nx][ny] = dis[x][y] + 1 ;
q.push(tmp) ;
nx+=dir[0][i] ;
ny+=dir[1][i] ;
}
}
}
}
int main()
{
int i , j , cnt = 0 ;
while(scanf("%d%d",&w,&h)&&w&&h)
{
for( i = 0 ; i < 80 ; i++ )
{
for( j = 0 ; j < 80 ; j++ )
{
a[i][j] = ' ' ;
}
}
for( i = 1 ; i <= h ; i++ )
{
getchar() ;
for( j = 1 ; j <= w ; j++ )
{
scanf("%c",&a[i][j]) ;
}
}
printf("Board #%d:\n",++cnt) ;
int t = 0 ;
while(scanf("%d%d%d%d",&y1,&x1,&y2,&x2)&&x1&&y1&&x2&&y2)
{
memset(vis,0,sizeof(vis)) ;
memset(dis,0,sizeof(dis)) ;
a[x2][y2] = ' ' ;
bfs(x1,y1) ;
if(!dis[x2][y2]) printf("Pair %d: impossible.\n",++t) ;
else printf("Pair %d: %d segments.\n",++t,dis[x2][y2]) ;
a[x2][y2] = 'X' ;
}
printf("\n") ;
}
return 0 ;
}