fjnu 1083 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 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.
KEY:这题是很明显的搜索题,也好麻烦(我是这么想的),我用了递归,一开始搞了半天出不来,一个原因是因为
    每次搞完目标的那个方块都变成0了,改了还是不行……有发现原来我每次开始都向一个方向……要从四个方向都走
    一遍,我算的转弯的个数……这样就能算出段数了……;
 
 
Source:

#include
< iostream >
#define  MAX 9999999
using   namespace  std;

char  a[ 80 ][ 80 ];
int  f[ 80 ][ 80 ];
int  n,m;
int  dx[ 5 ] = {0,-1,0,1,0} ;
int  dy[ 5 ] = {0,0,1,0,-1} ;
int  p1,q1,p2,q2;
int  dirs;
int  best;

/*void test()
{
    for(int i=0;i<=n+1;i++)
    {
        for(int j=0;j<=m+1;j++)
            printf("%5d",f[i][j]);
        cout<<endl;
    }
    cout<<endl;
}
*/


void  input()
{
    
int i,j;
    
char tmp[100];
    
for(i=1;i<=n;i++)
    
{
        gets(tmp);
        
for(j=1;j<=m;j++)
        
{
            a[i][j]
=tmp[j-1];
            
if(a[i][j]=='X') f[i][j]=-1;
        }

    }

}


int  ok( int  x, int  y)
{
    
if(x==p2&&y==q2) return 1;
    
if(x>=0&&x<=n+1&&y>=0&&y<=m+1&&f[x][y]==0
        
return 1;
    
else return 0;
}


void  DFS( int  x, int  y, int  di)
{
    
if(x==p2&&y==q2&&dirs<best)
    
{
        best
=dirs;
//        cout<<"    "<<best<<endl;
//        test();    
        return ;
    }

    
if(dirs>best) return ;
    
for(int i=1;i<=4;i++)
    
{
        
if(ok(x+dx[i],y+dy[i]))
        
{
            f[x
+dx[i]][y+dy[i]]=1;
            
if(di!=i) dirs++;
            DFS(x
+dx[i],y+dy[i],i);
            
if(di!=i) dirs--;
            f[x
+dx[i]][y+dy[i]]=0;
            f[p1][q1]
=f[p2][q2]=-1;
        }

    }

}



void  init1()
{
    
int i,j;
    
for(i=0;i<=75;i++)
        
for(j=0;j<=75;j++)
            f[i][j]
=0;
}


void  init2()
{
    best
=MAX;
    dirs
=0;
    
int i,j;
    
for(i=0;i<=n+2;i++)
        
for(j=0;j<=m+2;j++)
        
{
            
if(f[i][j]!=-1) f[i][j]=0;
        }

}


int  main()
{
//    freopen("fjnu_1083.in","r",stdin);
    int N=0;
    
while(cin>>m>>n)
    
{
        init1();
        getchar();
        
if(n==0&&m==0break;    
        input();
//        test();
        cout<<"Board #"<<++N<<':'<<endl;
        
int k=0;
        
while(cin>>q1>>p1>>q2>>p2)
        
{    
            init2();
            
if(p1==0&&p2==0&&q1==0&&q2==0break;
            DFS(p1,q1,
1);
            DFS(p1,q1,
2);
            DFS(p1,q1,
3);
            DFS(p1,q1,
4);
            cout
<<"Pair "<<++k<<"";
            
if(best==MAX) cout<<"impossible."<<endl;
            
else cout<<best+1<<" segments."<<endl;
        }

        cout
<<endl;
    }

    
return 0;
}



















            

 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值