POJ1101(The Game)

The Game
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 3058 Accepted: 899

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.

 

 

  1/*
  2It consists of straight segments, each one being either horizontal or vertical. 
  3
  4
  5It does not cross any other game pieces.*/

  6#include <iostream>
  7#include <queue>
  8#define NIL INT_MAX
  9using namespace std;
 10
 11const int N = 80;
 12
 13struct Point
 14{
 15    int x,y;
 16    int cnt;//记录转化方向次数
 17    bool operator <(const Point &tt) const
 18    {
 19        return cnt > tt.cnt;
 20    }

 21}
;
 22
 23int graph[N][N];
 24int Visited[N][N];//记录转向次数
 25int turn[N][N];//记录转向方向
 26int mk[N][N];
 27int w, h;
 28int m, n;
 29int dir[4][2= {{01}{0-1}{10}{-10}};
 30
 31bool isBound(int x, int y)
 32{
 33    if(x < 0 || y < 0)
 34        return false;
 35    if(x > m || y > n)
 36        return false;
 37    return true;
 38}

 39
 40int bfs(Point bp, Point ep)
 41{
 42    Point a, b;
 43    int i, j;
 44    priority_queue<Point> Q;
 45
 46    //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
 47
 48    for(i = 0; i <= m; i++)
 49        for(j = 0; j <= n; j++)
 50        {
 51            Visited[i][j] = NIL;
 52            mk[i][j] = 0;
 53        }

 54    
 55    Visited[bp.x][bp.y] = 0;
 56    turn[bp.x][bp.y] = -1;
 57    mk[bp.x][bp.y] = 1;
 58
 59    for(i = 0; i < 4; i++)
 60    {
 61        a.x = bp.x + dir[i][0];
 62        a.y = bp.y + dir[i][1];
 63        a.cnt = 1;
 64
 65        Visited[a.x][a.y] = a.cnt;//记录拐弯次数
 66        turn[a.x][a.y] = i;//记录拐弯方向
 67
 68        Q.push(a);
 69    }

 70
 71    //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
 72
 73    while(!Q.empty())
 74    {
 75        a = Q.top();
 76        Q.pop();
 77        
 78        if(a.x == ep.x && a.y == ep.y)
 79            return Visited[a.x][a.y];
 80        if(graph[a.x][a.y])
 81            continue;
 82
 83        mk[i][j] = 1;
 84        for(i = 0; i < 4; i++)
 85        {
 86            b.x = a.x + dir[i][0];
 87            b.y = a.y + dir[i][1];
 88
 89            if(isBound(b.x, b.y) && !mk[b.x][b.y])
 90            {    
 91                    if(i == turn[a.x][a.y])
 92                        b.cnt = Visited[a.x][a.y];
 93                    else 
 94                        b.cnt = Visited[a.x][a.y] + 1;
 95
 96                    if(b.cnt < Visited[b.x][b.y])
 97                    {
 98                        Visited[b.x][b.y] = b.cnt;
 99                        turn[b.x][b.y] = i;
100                        Q.push(b);
101                    }

102                    
103            }
//for
104        }

105    }
//while
106    return -1;
107}

108
109int main()
110{
111    char tc;
112    int i, j, ans, cc = 1, kk;
113    Point bp, ep;
114    while(scanf("%d%d"&w, &h))
115    {
116        //cc = 1;
117        m = h + 1;//m行
118        n = w + 1;//n列
119
120        if(!&& !h)
121            break;//输入结束
122
123        for(i = 0; i <= m; i++)
124            for(j = 0; j <= n; j++)
125                graph[i][j] = 0;//初始化
126        for(i = 1; i <= h; i++)
127        {
128            getchar();
129            for(j = 1; j <= w; j++)
130            {
131                scanf("%c"&tc);
132                if(tc == 'X')
133                    graph[i][j] = 1;
134            }

135        }

136        cout<<"Board #"<<cc++<<":"<<endl;
137        kk = 1;
138        while(scanf("%d%d%d%d"&bp.y, &bp.x, &ep.y, &ep.x))
139        {
140            if(!bp.x && !bp.y && !ep.x && !ep.y)
141                break;//测试用例输入结束
142            ans = bfs(bp, ep);
143            if(ans != -1)
144                cout<<"Pair "<<kk++<<""<<ans<<" segments."<<endl;
145            else
146                cout<<"Pair "<<kk++<<": impossible."<<endl;
147        }

148        cout<<endl;
149    }

150    return 0;
151}

152

 

posted on 2009-04-24 09:15  Xredman  阅读(542)  评论(0编辑  收藏  举报

导航