标题:帮忙看一下这个代码,觉得有些数据不对,但是思路是对的
只看楼主
枫叶无痕
Rank: 2
等 级:论坛游民
帖 子:80
专家分:30
注 册:2011-2-10
结帖率:73.91%
 问题点数:0 回复次数:0 
帮忙看一下这个代码,觉得有些数据不对,但是思路是对的
The Game

Time Limit:1000MS  Memory Limit:65536K
Total Submit:30 Accepted:18

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.
Source

Mid-Central European Regional Contest 1999


#include<stdio.h>
#include<string.h>
int dir[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
char str[100][100];
int map[100][100];
int n,m;
struct node
{
    int x;
    int y;
    int step;
    int bend;
}g,h,q[10000];
int bfs()
{
    int e,f,i;
    struct node s,t;
    e=f=0;
    q[e++]=g;
    while(f<e)
    {
        s=q[f++];
        if(s.x==h.x&&s.y==h.y)
        return s.step;
        for(i=0;i<4;i++)
        {
            t.x=s.x+dir[i][0];
            t.y=s.y+dir[i][1];
            t.step=s.step;
            t.bend=s.bend;
            if(i!=t.bend)
            {
                t.step++;
                t.bend=i;
            }
            if(map[t.x][t.y])
            continue;
            if(t.x<0||t.y<0||t.y>n+1||t.x>m+1)
            continue;
            if(str[t.x][t.y]=='X')
            continue;
            q[e++]=t;
            map[t.x][t.y]=1;
        }
    }
    return 0;
}
int main()
{
    int i,j,k,w,r;
    char c;
    w=1;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        if(n==0&&m==0)
        break;
        getchar();
        for(i=1;i<=m;i++)
        {
            gets(str[i]+1);
        }
        r=1;
        printf("Board #%d:\n",w);
        w++;
        while(scanf("%d%d%d%d",&g.y,&g.x,&h.y,&h.x)!=EOF)
        {
            if(g.x==0&&g.y==0&&h.x==0&&h.y==0)
            break;
            memset(map,0,sizeof(map));
            g.bend=10;
            k=bfs();
            if(k==0)
            printf("Pair %d: impossible.\n",r);
            else
            printf("Pair %d: %d segments.\n",r,k);
            r++;
            str[h.x][h.y]='X';
        }
        printf("\n");
    }
    return 0;
}
搜索更多相关主题的帖子: game important connected contain morning 
2011-08-28 18:32



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-348583-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.600691 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved