Poj 1101 the game (BFS + direction of determination)

Source: Internet
Author: User

The game


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 wowould 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 ".

The entire input is terminated by a test case starting with W = H = 0. This test case shocould 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. (PE unknowingly)

Sample Input

5 4XXXXXX   XXXX X XXX 2 3 5 31 3 4 42 3 3 40 0 0 00 0

Sample output

Board #1:Pair 1: 4 segments.Pair 2: 3 segments.Pair 3: impossible.



Similar to a video player. Calculates the minimum number of line segments. For example, the following figure shows the number of line segments, which is 4 and 3.


Idea: I want to solve the problem of two minimum steps. But what should I do. You only need to add a dire record direction. If, next. Dire! = Cur. Dire. The number of line segments + 1.


The Code is as follows:

#include<iostream>#include<cstdio>#include<algorithm>#include<cmath>#include<cstring>#include<cctype>#include<queue>#include<stack>#define INF 0x3f3f3f3f#define maxn 100001using namespace std;int n,m;char map[80][80];int go[4][2]={{-1,0},{0,1},{1,0},{0,-1}};// 0 1 2 3int vis[80][80];int sx,sy,ex,ey;struct node{    int x,y;    int seg;    int dire;    node(){}    node(int a,int b,int c,int d)    {        x=a;        y=b;        seg=c;        dire=d;    }};int  bfs(){    queue<node> que;    que.push(node(sx,sy,0,-1));    memset(vis,0,sizeof vis);    vis[sx][sy]=1;    while(!que.empty())    {        node cur=que.front();        node next;        que.pop();        for(int i=0;i<4;i++)        {            next.x=cur.x+go[i][0];            next.y=cur.y+go[i][1];            next.dire=i;            if(next.x>=0&&next.x<=n+1&&next.y>=0&&next.y<=m+1&&!vis[next.x][next.y])            {                if(next.dire==cur.dire)                    next.seg=cur.seg;                else                    next.seg=cur.seg+1;                if(next.x==ex&&next.y==ey)                    return next.seg;                if(map[next.y][next.x]!='X')                {                    //cout<<next.x<<next.y<<next.seg<<endl;                    vis[next.x][next.y]=1;                    que.push(next);                }            }        }    }    return 0;}int main(){    int t=0;    while(scanf("%d%d",&n,&m),n||m)    {        t++;        int cas=0;        memset(map,' ',sizeof map);        for(int i=1;i<=m;i++)        {            getchar();            for(int j=1;j<=n;j++)                 scanf("%c",&map[i][j]);        }        //cout<<"15"<<map[4][2]<<endl;        printf("Board #%d:\n",t);        while(1)        {            //cout<<"---->\n";            scanf("%d%d%d%d",&sx,&sy,&ex,&ey);            //cout<<sx<<sy<<ex<<ey<<endl;            if(sx||sy||ex||ey)            {                int temp=bfs();                //cout<<"--------->>>>>>>>\n";                if(temp)                   printf("Pair %d: %d segments.\n",++cas,temp);                else                    printf("Pair %d: impossible.\n",++cas);            }            else                break;        }        printf("\n");    }    return 0;}






Zookeeper

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.