您的位置:首页 > 其它

搜索 HOJ 1140 The Game

2014-02-06 11:49 363 查看


The Game

My Tags  (Edit)
 Source : ACM
ICPC Mid-Central European 1999
 Time limit : 3 sec Memory limit : 32 M
Submitted : 386, Accepted :
164

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.


题意:其中输入的X表示这里是一张牌,没有就是可以通过的。然后输入起点和终点,问从起点到终点最少要转多少个弯,开始第一步算转了一个弯。

思路:直接搜索就行了,用turn[i][j]表示从起点到(i,j) 最少要转多少个弯。注意可以走出边界。。。

代码:
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<math.h>
#include<vector>
using namespace std;
#define LL long long
#define mp make_pair
const int inf = 1e9;
int turn[100][100];
int W , H , r1 , c1 , r2 , c2 , Cas , Pair;
char G[100][100];
int Move[2][4] = { { 0,1,0,-1} , {1,0,-1,0} };

void init()
{
for (int i = 0 ; i <= W+1 ; ++i)
for (int j = 0 ; j <= H+1 ; ++j)
turn[j][i] = inf;
}

void input()
{
memset(G,0,sizeof(G));
for (int i = 1 ; i <= H ; ++i)
gets(G[i]+1);
}

void dfs(int r,int c,int dir , int step)
{
if (r < 0 || c < 0 ||
r > H+1 || c > W+1
||step > turn[r][c]) return;
if (r==r2 && c==c2)
{
turn[r][c] = step;
return;
}
if (!(r==r1 && c==c1) && G[r][c]=='X') return;
turn[r][c] = step;
for (int i = 0 ; i < 4 ; ++i)
{
int rr = r + Move[0][i];
int cc = c + Move[1][i];
dfs(rr,cc,i,step+(i!=dir));
}
}

void solve()
{
init();
dfs(r1,c1,-1,0);
if (turn[r2][c2]==inf) printf("Pair %d: impossible.\n" , Pair);
else printf("Pair %d: %d segments.\n",Pair,turn[r2][c2]);
}

int main()
{
Cas = 0;
while (scanf("%d%d%*c",&W,&H))
{
if (W==0 && H==0) return 0;
input();
++Cas;
Pair = 0;
printf("Board #%d:\n",Cas);
while (scanf("%d%d%d%d",&c1,&r1,&c2,&r2))
{
if (r1+r2+c1+c2==0) break;
++Pair;
solve();
}
printf("\n");
}
}


 

 

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: