Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

进入无限WA循环,求大神出个刁钻数据 >﹏<。

Posted by qq11111qqwo at 2015-02-01 13:25:14 on Problem 1101
#include <cstdio>
#include <queue>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <iostream>
using namespace std;
const int maxn = 100;
int x1,y1,x2,y2;
bool visit[4][maxn][maxn];
int w,h;
char maze[maxn][maxn];
int dir[4][2] = {1,0,-1,0,0,1,0,-1};
struct node
{
    int x,y;
    int seg;
};

int ans;
void bfs()
{
    queue<node> Q;
    node tmp;
    tmp.x = x1;
    tmp.y = y1;
    tmp.seg = 0;
    for(int i = 0; i < 4; i++)
        visit[i][x1][y1] = true;
    Q.push(tmp);
    while( !Q.empty() )
    {
        tmp = Q.front();
        Q.pop();
        if( tmp.x == x2 && tmp.y == y2)
        {
            if( ans > tmp.seg )
                ans = tmp.seg;
        }
        else
        {
            for(int i = 0; i < 4; i++)
            {
                int cnt = 1;
                while( true )
                {
                    int nx = tmp.x + dir[i][0]*cnt;
                    int ny = tmp.y + dir[i][1]*cnt;
                    if(  nx >= 0 && ny >= 0 && nx <= h+1 && ny <= w+1
                       && maze[nx][ny] != 'X' &&!visit[i][nx][ny] )
                    {
                        visit[i][nx][ny] = true;
                        node hehe;
                        hehe.x = nx;
                        hehe.y = ny;
                        hehe.seg = tmp.seg+1;
                        Q.push(hehe);
                    }
                    else
                        break;
                    cnt++;
                }
            }
        }
    }
}

int main()
{
    freopen("input.txt","r",stdin);
    int board = 1;
    while( scanf("%d%d",&w,&h) )
    {
        if( w == 0 && h == 0)
            break;
        for(int i = 0; i < h+5; i++)
            for(int j = 0; j < w+5; j++)
                maze[i][j] = ' ';
        for(int i = 1; i <= h; i++)
        {
            getchar();
            for(int j = 1; j <= w; j++)
            {
                scanf("%c",&maze[i][j]);
            }
        }
        int pai =  1;
        printf("Board #%d:\n",board++);
        while( scanf("%d%d%d%d",&y1,&x1,&y2,&x2) )
        {
            memset(visit,0,sizeof(visit));
            maze[x2][y2] = ' ';
            ans = 100000000;
            if( x1==0 && y1==0 && x2==0 && y2==0 )
                break;
            if( (x1 == x2 && abs(y2-y1) == 1) || (y1 == y2 && abs(x2-x1) == 1))
            {
                printf("Pair %d: 3 segments.\n",pai++);
                maze[x2][y2] = 'X';
                continue;
            }
            bfs();
            if( ans != 100000000 )
                printf("Pair %d: %d segments.\n",pai++,ans);
            else
                printf("Pair %d: impossible.\n",pai++);
            maze[x2][y2] = 'X';
        }
    }
    return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator