跳棋参考代码

  1 #include<iostream>
  2 #include<algorithm>
  3 #include<string>
  4 #include<cstring> 
  5 #include<queue>
  6 using namespace std;
  7 int dx[]={0,0,1,-1};
  8 int dy[]={1,-1,0,0};
  9 struct point{
 10     int x,y;
 11 };
 12 bool cmp(point a,point b)
 13 {
 14     if(a.x==b.x) return a.y<b.y;
 15     return a.x<b.x;
 16 }
 17 struct node{
 18     point s[5];
 19     int dis;
 20     int dir;
 21 };
 22 void copy(node a,node &b)
 23 {
 24     b.dir=a.dir;
 25     b.dis=a.dis;
 26     for(int i=1; i<5; i++)
 27     {
 28         b.s[i].x=a.s[i].x;
 29         b.s[i].y=a.s[i].y;
 30     }    
 31 }
 32 queue<node>s;
 33 char vis[8][8][8][8][8][8][8][8];
 34 bool check(node a,node b)
 35 {
 36     for(int i=1; i<5; i++)
 37     {
 38         if(a.s[i].x!=b.s[i].x) return false;
 39         if(a.s[i].y!=b.s[i].y) return false;
 40     }
 41     return true;
 42 }
 43 bool checkcf(int xx,int yy,node a)
 44 {
 45     for(int i=1; i<5; i++)
 46         if(a.s[i].x==xx&&a.s[i].y==yy) return false;
 47     return true;
 48 }
 49 int bj(node a)
 50 {
 51     if(vis[a.s[1].x][a.s[1].y][a.s[2].x][a.s[2].y][a.s[3].x][a.s[3].y][a.s[4].x][a.s[4].y]==0)
 52     {
 53         vis[a.s[1].x][a.s[1].y][a.s[2].x][a.s[2].y][a.s[3].x][a.s[3].y][a.s[4].x][a.s[4].y]=a.dir;
 54         return 0;
 55     }
 56     return vis[a.s[1].x][a.s[1].y][a.s[2].x][a.s[2].y][a.s[3].x][a.s[3].y][a.s[4].x][a.s[4].y];
 57 }
 58 void bfs(node a,node b)
 59 {
 60     if(check(a,b))
 61     {
 62         cout<<"YES"<<endl;
 63         return;
 64     }
 65     while(!s.empty())
 66     {
 67         node cur=s.front();
 68         s.pop();
 69         if(cur.dis==4){
 70             cout<<"NO"<<endl;
 71             return;
 72         }
 73         for(int i=1; i<5; i++)
 74             for(int j=0; j<4; j++)
 75             {
 76                 int xx=cur.s[i].x+dx[j];
 77                 int yy=cur.s[i].y+dy[j];
 78                 if(!checkcf(xx,yy,cur))//不能走,就只能跳 
 79                     xx+=dx[j],yy+=dy[j];
 80                 if(xx<0||xx>7||yy<0||yy>7) continue;
 81                 if(checkcf(xx,yy,cur))//检测能否走或者跳 
 82                 {
 83                     node nw;
 84                     copy(cur,nw);
 85                     nw.s[i].x=xx;
 86                     nw.s[i].y=yy;
 87                     nw.dis=cur.dis+1;
 88                     nw.dir=cur.dir;
 89                     sort(nw.s+1,nw.s+5,cmp);
 90                     if(bj(nw)==0)
 91                         s.push(nw);
 92                     else if(bj(nw)==cur.dir) continue;
 93                     else{
 94                         cout<<"YES"<<endl;
 95                         return;
 96                     }
 97                 }
 98             }
 99     }
100 }
101 void init()
102 {
103     while(!s.empty()) s.pop();
104     memset(vis,0,sizeof(vis));
105 }
106 int main()
107 {
108     freopen("cheer.in","r",stdin);
109     freopen("cheer.out","w",stdout);
110     node a;
111     while(scanf("%d%d",&a.s[1].x,&a.s[1].y)!=EOF)
112     {
113         init();
114         a.s[1].x--,a.s[1].y--;
115         for(int i=2; i<5; i++)    cin>>a.s[i].x>>a.s[i].y,a.s[i].x--,a.s[i].y--;
116         a.dis=0;
117         a.dir=1;
118         sort(a.s+1,a.s+5,cmp);
119         s.push(a);bj(a); 
120         
121         node b;
122         b.dis=0;
123         b.dir=2;
124         for(int i=1; i<5; i++)    cin>>b.s[i].x>>b.s[i].y,b.s[i].x--,b.s[i].y--;
125         sort(b.s+1,b.s+5,cmp);
126         s.push(b);bj(b);
127         
128         bfs(a,b);
129     }
130     return 0;
131  } 

 

posted @ 2020-03-27 13:42  nhflsoiers  阅读(113)  评论(0)    收藏  举报