中国象棋(计蒜客)

 

 

 

 

 

  1 #include<iostream> 
  2 #include<string>
  3 #include<queue>
  4 using namespace std;
  5 char a[11][10];
  6 int state[11][10];
  7 struct node
  8 {
  9     int x;
 10     int y;
 11     char s;
 12     node(int x,int y,char s)
 13     {
 14         this->x=x;
 15         this->y=y;
 16         this->s=s;
 17     }
 18 };
 19 bool next(int x,int y)
 20 {
 21     if(x>=1&&y>=1&&x<=10&&y<=9)
 22     {
 23         if(state[x][y]==0&&(a[x][y]=='.'||a[x][y]=='T'))
 24             return true;
 25         else
 26             return false;
 27     }
 28     else
 29         return false;
 30 }
 31 int main()
 32 {
 33     int flag=0;
 34     queue<node> q;
 35     for(int i=1;i<=10;i++)
 36         for(int j=1;j<=9;j++)
 37         {
 38             cin>>a[i][j];
 39             if(a[i][j]=='S')
 40                 q.push(node(i,j,'S'));
 41         }
 42     while(!q.empty())
 43     {
 44         node now = q.front();
 45         if(now.s=='T')
 46         {
 47             cout<<"Yes";
 48             flag=1;
 49             break;
 50         }
 51         for(int i=0;i<8;i++)
 52         {
 53             if(next(now.x-1,now.y-2))
 54             {
 55                 node next_step(now.x-1,now.y-2,a[now.x-1][now.y-2]);
 56                 q.push(next_step);
 57                 state[now.x-1][now.y-2]=1;
 58             }
 59             if(next(now.x-2,now.y-1))
 60             {
 61                 node next_step(now.x-2,now.y-1,a[now.x-2][now.y-1]);
 62                 q.push(next_step);
 63                 state[now.x-2][now.y-1]=1;
 64             }
 65             if(next(now.x-2,now.y+1))
 66             {
 67                 node next_step(now.x-2,now.y+1,a[now.x-2][now.y+1]);
 68                 q.push(next_step);
 69                 state[now.x-2][now.y+1]=1;
 70             }
 71             if(next(now.x-1,now.y+2))
 72             {
 73                 node next_step(now.x-1,now.y+2,a[now.x-1][now.y+2]);
 74                 q.push(next_step);
 75                 state[now.x-1][now.y+2]=1;
 76             }
 77             if(next(now.x+1,now.y+2))
 78             {
 79                 node next_step(now.x+1,now.y+2,a[now.x+1][now.y+2]);
 80                 q.push(next_step);
 81                 state[now.x+1][now.y+2]=1;
 82             }
 83             if(next(now.x+2,now.y+1))
 84             {
 85                 node next_step(now.x+2,now.y+1,a[now.x+2][now.y+1]);
 86                 q.push(next_step);
 87                 state[now.x+2][now.y+1]=1;
 88             }
 89             if(next(now.x+2,now.y-1))
 90             {
 91                 node next_step(now.x+2,now.y-1,a[now.x+2][now.y-1]);
 92                 q.push(next_step);
 93                 state[now.x+2][now.y-1]=1;
 94             }
 95             if(next(now.x+1,now.y-2))
 96             {
 97                 node next_step(now.x+1,now.y-2,a[now.x+1][now.y-2]);
 98                 q.push(next_step);
 99                 state[now.x+1][now.y-2]=1;
100             }
101         }
102         q.pop();
103     }
104     if(flag==0)
105     {
106         cout<<"No";
107     }
108     return 0;
109 }

 

posted @ 2020-03-18 15:46  caxi  阅读(383)  评论(0编辑  收藏  举报