hdu 1401双广

Solitaire

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 2526    Accepted Submission(s): 822


Problem Description
Solitaire is a game played on a chessboard 8x8. The rows and columns of the chessboard are numbered from 1 to 8, from the top to the bottom and from left to right respectively.

There are four identical pieces on the board. In one move it is allowed to:

> move a piece to an empty neighboring field (up, down, left or right),

> jump over one neighboring piece to an empty field (up, down, left or right). 

hdu 1401双广_第1张图片


There are 4 moves allowed for each piece in the configuration shown above. As an example let's consider a piece placed in the row 4, column 4. It can be moved one row up, two rows down, one column left or two columns right.

Write a program that:

> reads two chessboard configurations from the standard input,

> verifies whether the second one is reachable from the first one in at most 8 moves,

> writes the result to the standard output.
 

Input
Each of two input lines contains 8 integers a1, a2, ..., a8 separated by single spaces and describes one configuration of pieces on the chessboard. Integers a2j-1 and a2j (1 <= j <= 4) describe the position of one piece - the row number and the column number respectively. Process to the end of file.
 

Output
The output should contain one word for each test case - YES if a configuration described in the second input line is reachable from the configuration described in the first input line in at most 8 moves, or one word NO otherwise.
 

Sample Input
 
   
4 4 4 5 5 4 6 5 2 4 3 3 3 6 4 6
 

Sample Output
 
   
YES
 

Source
Southwestern Europe 2002
 

Recommend
Ignatius.L
 
fuck啊一个小地方wa了好1个多小时啊~~
#include 
#include 
#include 
#include 
#define INF 0x3f3f3f3f
#define N 1000000
#define BUG printf("here!\n")
using namespace std;
struct point
{
    int x,y;
};
struct node
{
    point num[4];
    int d;
};
node q1[N],q2[N];
char vis[8][8][8][8][8][8][8][8];
int map[10][10];
int xx[4]={0,1,0,-1};
int yy[4]={1,0,-1,0};
bool cmp(point lhs,point rhs)
{
    if(lhs.x!=rhs.x)
        return lhs.x=8||y<0||y>=8)
        return 0;
    if(map[x][y]==1)
        return 1;
    return 2;
}
int bfs(node start,node end)
{
    memset(vis,0,sizeof(vis));
    int front1=0,tail1=1,front2=0,tail2=1;
    sort(start.num,start.num+4,cmp);
    sort(end.num,end.num+4,cmp);
    make_vis(start,'1');
    make_vis(end,'2');
    q1[0]=start;q2[0]=end;
    q1[0].d=0;q2[0].d=0;
    while(front18)
                        return -1;
                    if(visit(next)=='1')
                        continue;
                    else if(visit(next)=='2')
                    {
                        return next.d+cur.d+1;
                    }
                    make_vis(next,'1');
                    next.d=cur.d+1;

                    q1[tail1++]=next;

                }
            }
        }



        tmp=tail2;
        for(;front28)
                        return -1;
                    if(visit(next)=='2')
                        continue;
                    else if(visit(next)=='1')
                    {
                        return next.d+cur.d+1;
                    }
                    make_vis(next,'2');
                    next.d=cur.d+1;

                    q2[tail2++]=next;

                }
            }
        }
    }
    return -1;
}

int main()
{
    int x,y;
    node t1,t2;
    while(scanf("%d%d",&x,&y)!=EOF)
    {
        t1.num[0].x=x-1;
        t1.num[0].y=y-1;
        int i;
        for(i=1;i<4;i++)
        {
            scanf("%d%d",&x,&y);
            t1.num[i].x=x-1;
            t1.num[i].y=y-1;
        }
        for(i=0;i<4;i++)
        {
            scanf("%d%d",&x,&y);
            t2.num[i].x=x-1;
            t2.num[i].y=y-1;
        }
        int res=bfs(t1,t2);
        if(res==-1||res>8)
            printf("NO\n");
        else
            printf("YES\n");
    }
    return 0;
}


你可能感兴趣的:(acm,搜索)