Csu 1459: Chess (oak)

Source: Internet
Author: User

1459: ChessTime Limit: 1 Sec Memory Limit: 128 MB Special Judge
Submit: 58 Solved: 31
[Submit] [Status] [Web Board] Description

Input

Output

Sample Input
3E 2 E 3F 1 E 8A 3 A 3
Sample Output
Impossible2 F 1 B 5 E 80 A 3
Given the start point and the end point, the Board has two colors: Black and white. You can only jump to the same color grid. If you can jump to the end, you can output the hop path.
#include
  
   #include
   
    #include
    
     using namespace std;typedef struct nnn{    int x,y,step;}NODE;int path[100],color;NODE node[2];int bfs(){    path[node[0].y*8+node[0].x]=node[0].y*8+node[0].x;    if(node[0].x==node[1].x&&node[0].y==node[1].y)        return 1;    queue
     
      q;    NODE p,pre;    int vist[10][10]={0};    int dir[4][2]={1,-1,-1,1,1,1,-1,-1};    node[0].step=0;    vist[node[0].y][node[0].x]=1;    q.push(node[0]);    while(!q.empty())    {        pre=q.front(); q.pop();        for(int e=0; e<4; e++)        {            p=pre; p.step++;            for(int i=1; i<8; i++)            {                p.y=p.y+dir[e][0]*i;                p.x=p.x+dir[e][1]*i;                if(p.y>=0&&p.y<8&&p.x>=0&&p.x<8&&vist[p.y][p.x]==0)                if(color==(p.y+p.x)%2)                {                    path[p.y*8+p.x]=pre.y*8+pre.x;                    if(p.y==node[1].y&&p.x==node[1].x)                        return 1;                    vist[p.y][p.x]=1;                    if(p.step<3)q.push(p);                }                p.y=p.y-dir[e][0]*i;                p.x=p.x-dir[e][1]*i;            }            p.step--;        }    }    return 0;}int main(){    int t,b,pp[100];    char s[2];    scanf("%d",&t);    while(t--)    {        scanf("%s %d",s,&b);node[0].x=s[0]-'A';node[0].y=8-b;        scanf("%s %d",s,&b);node[1].x=s[0]-'A';node[1].y=8-b;        color=(node[0].y+node[0].x)%2;        if(color!=(node[1].y+node[1].x)%2)        printf("Impossible\n");        else        {            int flog=bfs();            if(flog==0)            printf("Impossible\n");            else            {                int k=0,y;                int x=node[1].y*8+node[1].x;                while(path[x]!=x)                {                    pp[k++]=x; x=path[x];                }                pp[k++]=x;                printf("%d",k-1);                for(int i=k-1;i>=0; i--)                {                    y=pp[i]/8; x=pp[i]%8;                    printf(" %c %d",x+'A',8-y);                }                printf("\n");            }        }    }}
     
    
   
  


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.