hdu1401 Solitaire

来源:互联网 发布:经济学博士申请 知乎 编辑:程序博客网 时间:2024/05/16 17:54

Solitaire

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


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



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 52 4 3 3 3 6 4 6
 

Sample Output
YES
 

Source
Southwestern Europe 2002
 

Recommend
Ignatius.L
 


双向BFS,由于题目限定的步数,所以可以先做起点,再做终点,这样比较方便
但是对于其它题目,可以起点终点同时拓展

#include<cstdio>#include<cstring>#include<set>#include<queue>#include<iostream>#include<algorithm>using namespace std;struct data{    int s,idx;};struct Point{    int x,y;};Point St[4],En[4],tmp[4];int dx[4]={-1,1,0,0};int dy[4]={0,0,-1,1};bool bz[11][11];set   <int>  s1, s2;queue <data> q;bool cmp(Point a,Point b){    return (a.x<b.x|| (a.x==b.x&&a.y<b.y) );}int change( Point *a){    sort(a,a+4,cmp);    int t=0;    for (int i=0; i<4; i++)    {        t=t*10+a[i].x;        t=t*10+a[i].y;    }    return t;}void recovery( int x ){    for (int i=0; i<4; i++)    {        tmp[i].y=x % 10;        x/=10;        tmp[i].x=x % 10;        x/=10;    }    sort(tmp,tmp+4,cmp);}bool bound(int tx,int ty){        if (tx<1||tx>8||ty<1||ty>8 ) return false;        return true;}int main(){    while (scanf("%d%d%d%d%d%d%d%d",&St[0].x,&St[0].y,&St[1].x,&St[1].y,&St[2].x,&St[2].y,&St[3].x,&St[3].y)!=EOF)    {        while (!q.empty()) q.pop();        s1.clear();        s2.clear();        scanf("%d%d%d%d%d%d%d%d",&En[0].x,&En[0].y,&En[1].x,&En[1].y,&En[2].x,&En[2].y,&En[3].x,&En[3].y);        sort(St,St+4,cmp);        sort(En,En+4,cmp);        int st=0, en=0;        st=change(St);        s1.insert(st);        q.push( (data){st,0} );        while (!q.empty())        {            data u=q.front();            q.pop();            for (int i=0; i<4; i++)            {                for (int j=0; j<4; j++)                {                    recovery( u.s );                    memset(bz,0,sizeof(bz));                    for (int k=0; k<4; k++)                        bz[tmp[k].x][tmp[k].y]=1;                    int tx=tmp[j].x+dx[i];                    int ty=tmp[j].y+dy[i];                    if (!bound(tx,ty))  continue;                    if (bz[tx][ty] )                    {                        tx=tx+dx[i];                        ty=ty+dy[i];                    }                    if (!bound(tx,ty))  continue;                    if (bz[tx][ty])  continue;                    tmp[j].x=tx; tmp[j].y=ty;                    data v;                    v.s=change( tmp );                    if ( s1.count(v.s) ) continue;                    s1.insert( v.s );                    v.idx=u.idx+1;                    if (v.idx<4)                    {                        q.push( v );                    }                }            }        }        while (!q.empty()) q.pop();        bool flag=0;        en=change(En);        if ( s1.count(en) )        {            printf("YES\n");            continue;        }        s2.insert(en);        q.push((data){en,0});        while (!q.empty())        {            data u=q.front();            q.pop();            for (int i=0; i<4; i++)            {                for (int j=0; j<4; j++)                {                    recovery( u.s );                    memset(bz,0,sizeof(bz));                    for (int k=0; k<4; k++)                        bz[tmp[k].x][tmp[k].y]=1;                    int tx=tmp[j].x+dx[i];                    int ty=tmp[j].y+dy[i];                    if (!bound(tx,ty))  continue;                    if (bz[tx][ty] )                    {                        tx=tx+dx[i];                        ty=ty+dy[i];                    }                    if (!bound(tx,ty))  continue;                    if (bz[tx][ty])  continue;                    tmp[j].x=tx; tmp[j].y=ty;                    data v;                    v.s=change( tmp );                    if ( s2.count(v.s) ) continue;                    if (s1.count(v.s))                    {                        flag=1;                        break;                    }                    s2.insert( v.s );                    v.idx=u.idx+1;                    if (v.idx<4)                        q.push( v );                }                if (flag)  break;            }            if (flag)  break;        }        if (flag)  printf("YES\n");        else            printf("NO\n");    }    return 0;}


0 0