博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Hdu 1401 Solitaire 双向bfs
阅读量:3904 次
发布时间:2019-05-23

本文共 3508 字,大约阅读时间需要 11 分钟。

题目:

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

思路:

第一次做的是用的单向bfs,然后简单的剪了一下枝,然后tle。

然后网上的都是用的双向bfs,于是就学了一手双向bfs,建了两个队列和两个hash表,然后从起点和终点开始搜索,双向bfs一次是搜索队列的一层,而不仅仅是一个节点。

当两个hash表中都有那个元素的时候就说明找到了。

hash表的建立是运用了移位运算,因为每一位是0-7,所以只占三位,因此表示每十进制一位数的时候只需要移动三位就可以了。

代码如下:
 

#include 
#include
#include
#include
#include
#include
using namespace std;typedef long long ll;struct pos{ int x,y;};pos a[4],b[4];int loc[4][2]={ {1,0},{-1,0},{0,-1},{0,1}};map
vis[2];struct node{ int step; pos a[4];};int compare(pos a,pos b){ return a.x!=b.x? a.x
=0&&a.x<8&&a.y>=0&&a.y<8) return 1; return 0;}int isSame (pos a[],int x){ for (int i=0;i<4;i++) { if(i!=x&&a[i].x==a[x].x&&a[i].y==a[x].y) return 1; } return 0;}queue
q[2];int bfs (int x){ node now,next; int Size=q[x].size(); while(Size--) { now=q[x].front(); q[x].pop(); ll t=change(now.a); for (int i=0;i<4;i++) { for (int j=0;j<4;j++) { next=now; next.step++; next.a[i].x+=loc[j][0]; next.a[i].y+=loc[j][1]; if(judge(next.a[i])) { if(isSame(next.a,i)) { next.a[i].x+=loc[j][0]; next.a[i].y+=loc[j][1]; if(judge(next.a[i])) { if(!isSame(next.a,i)) { ll t=change(next.a); if(vis[x^1][t]==1) { return 1; } else { vis[x][t]=1; q[x].push(next); } } } } else { ll t=change(next.a); if(vis[x^1][t]==1) { return 1; } else { vis[x][t]=1; q[x].push(next); } } } } } } return 0;}int tbfs (){ node now1,now2; for (int i=0;i<4;i++) { now1.a[i]=a[i]; now2.a[i]=b[i]; } q[0].push(now1); ll t1=change(now1.a); vis[0][t1]=1; q[1].push(now2); ll t2=change(now2.a); vis[1][t2]=1; if(t1==t2) return 1; int step=0; while(!q[0].empty()||!q[1].empty()) { if(step>=4) return 0; step++; if(bfs(0)) return 1; if(bfs(1)) return 1; } return 0;}int main(){ while(scanf("%d%d",&a[0].x,&a[0].y)!=EOF) { vis[0].clear(); vis[1].clear(); while(!q[0].empty()) q[0].pop(); while(!q[1].empty()) q[1].pop(); scanf("%d%d%d%d%d%d",&a[1].x,&a[1].y,&a[2].x,&a[2].y,&a[3].x,&a[3].y); for (int i=0;i<4;i++) a[i].x--,a[i].y--; for (int i=0;i<4;i++) { scanf("%d%d",&b[i].x,&b[i].y); b[i].x--,b[i].y--; } if(tbfs()) printf("YES\n"); else printf("NO\n"); } return 0;}

 

转载地址:http://vwoen.baihongyu.com/

你可能感兴趣的文章
STM32 外部中断
查看>>
STM32 PWM
查看>>
STM32 PWM波驱动模拟舵机(库函数版)
查看>>
STM32——ADC
查看>>
破解百度网盘屏蔽文件分享失效被和谐的独家秘籍
查看>>
STM32F10X_XX宏定义的选择
查看>>
在头文件声明全局变量和创建extern
查看>>
stm32 USART 串口通信[操作寄存器+库函数]
查看>>
MATLAB画图常用调整代码
查看>>
WORD2010加载mathtype6.6
查看>>
TTL电平、CMOS电平、RS232电平的区别
查看>>
c语言那些细节之a+1和&a+1的区别
查看>>
交换两个变量的值,不使用第三个变量的四种法方
查看>>
STM32 产生随机数
查看>>
MFC 动态曲线 支持缩放 显示图例(CStatic派生类)
查看>>
STM32 变量存储问题
查看>>
win7下安装纯净版XP
查看>>
C++矩阵处理工具——Eigen
查看>>
CMake入门指南
查看>>
QT5.2新版本+VS2010平台搭建图文教程
查看>>