Solitaire(单向BFS||双向BFS)

Link:http://acm.hdu.edu.cn/showproblem.php?pid=1401

Problem:

Solitaire

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


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 5 2 4 3 3 3 6 4 6
 

Sample Output
  
  
YES
 

Source
 

题意就是有四颗棋子,能否在8步内走到目标状态

开个8维数组记录状态即可

 单向BFS:

[cpp]  view plain copy
  1. #include <stdio.h>  
  2. #include <string.h>  
  3. #include <algorithm>  
  4. #include <queue>  
  5. using namespace std;  
  6.   
  7. bool vis[8][8][8][8][8][8][8][8];  
  8. bool map[10][10];  
  9. int to[4][2] = {1,0,-1,0,0,1,0,-1};  
  10.   
  11. struct point  
  12. {  
  13.     int x[4],y[4],step;  
  14. } s,e;  
  15.   
  16. int check(point a)//判断是否为最终态  
  17. {  
  18.     for(int i = 0; i<4; i++)  
  19.     {  
  20.         if(!map[a.x[i]][a.y[i]])  
  21.             return 0;  
  22.     }  
  23.     return 1;  
  24. }  
  25.   
  26. int empty(point a,int k)//看要将要到达的那格是否为空  
  27. {  
  28.     for(int i = 0; i<4; i++)  
  29.     {  
  30.         if(i!=k && a.x[i] == a.x[k] && a.y[i] == a.y[k])  
  31.             return 0;  
  32.     }  
  33.     return 1;  
  34. }  
  35.   
  36. int judge(point next)//判断是否符合要求  
  37. {  
  38.     int i;  
  39.     for(i = 0; i<4; i++)  
  40.     {  
  41.         if(next.x[i]<0 || next.x[i]>=8 || next.y[i]<0 || next.y[i]>=8)  
  42.             return 1;  
  43.     }  
  44.     if(vis[next.x[0]][next.y[0]][next.x[1]][next.y[1]][next.x[2]][next.y[2]][next.x[3]][next.y[3]])  
  45.         return 1;  
  46.     return 0;  
  47. }  
  48.   
  49. int bfs()  
  50. {  
  51.     memset(vis,false,sizeof(vis));  
  52.     int i,j;  
  53.     queue<point> Q;  
  54.     point a,next;  
  55.     a.step = 0;  
  56.     for(i = 0; i<4; i++)  
  57.     {  
  58.         a.x[i] = s.x[i];  
  59.         a.y[i] = s.y[i];  
  60.     }  
  61.     Q.push(a);  
  62.     vis[a.x[0]][a.y[0]][a.x[1]][a.y[1]][a.x[2]][a.y[2]][a.x[3]][a.y[3]] = true;  
  63.     while(!Q.empty())  
  64.     {  
  65.         a = Q.front();  
  66.         Q.pop();  
  67.         if(a.step>=8)//因为后面循环有判断减枝,所以这里要包括8步  
  68.             return 0;  
  69.         if(check(a))  
  70.             return 1;  
  71.         for(i = 0; i<4; i++)  
  72.         {  
  73.             for(j = 0; j<4; j++)  
  74.             {  
  75.                 next = a;  
  76.                 next.x[i]+=to[j][0];  
  77.                 next.y[i]+=to[j][1];  
  78.                 next.step++;  
  79.                 if(judge(next))  
  80.                     continue;  
  81.                 if(empty(next,i))//要去的那一格是空  
  82.                 {  
  83.                     if(check(next))  
  84.                         return 1;  
  85.                     vis[next.x[0]][next.y[0]][next.x[1]][next.y[1]][next.x[2]][next.y[2]][next.x[3]][next.y[3]] = true;  
  86.                     Q.push(next);  
  87.                 }  
  88.                 else//非空则继续往前  
  89.                 {  
  90.                     next.x[i]+=to[j][0];  
  91.                     next.y[i]+=to[j][1];  
  92.                     if(judge(next) || !empty(next,i))//继续往前也要满足要求且是空格  
  93.                         continue;  
  94.                     if(check(next))  
  95.                         return 1;  
  96.                     vis[next.x[0]][next.y[0]][next.x[1]][next.y[1]][next.x[2]][next.y[2]][next.x[3]][next.y[3]] = true;  
  97.                     Q.push(next);  
  98.                 }  
  99.             }  
  100.         }  
  101.     }  
  102.     return 0;  
  103. }  
  104.   
  105. int main()  
  106. {  
  107.     int i;  
  108.     while(~scanf("%d%d",&s.x[0],&s.y[0]))  
  109.     {  
  110.         s.x[0]--;  
  111.         s.y[0]--;  
  112.         for(i = 1; i<4; i++)  
  113.         {  
  114.             scanf("%d%d",&s.x[i],&s.y[i]);  
  115.             s.x[i]--;  
  116.             s.y[i]--;  
  117.         }  
  118.         memset(map,false,sizeof(map));  
  119.         for(i = 0; i<4; i++)  
  120.         {  
  121.             scanf("%d%d",&e.x[i],&e.y[i]);  
  122.             e.x[i]--;  
  123.             e.y[i]--;  
  124.             map[e.x[i]][e.y[i]] = true;  
  125.         }  
  126.         int flag = bfs();  
  127.         if(flag)  
  128.             printf("YES\n");  
  129.         else  
  130.             printf("NO\n");  
  131.     }  
  132.   
  133.     return 0;  
  134. }  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值