1455.Solitaire(bfs状态混摇)
Solitaire
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 3570 Accepted Submission(s): 1098
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.
#include<stdio.h>
#include<queue>
#include<string.h>
#include<math.h>
#include<algorithm>
typedef long long ll ;
bool vis[][][][][][][][] ;
int move[][] = {{,} , {-,} , {,} , {,-}} ;
struct no
{
int x , y ;
} ee[] ; bool cmp (const no a , const no b)
{
if (a.x < b.x) {
return true ;
}
else if (a.x == b.x) {
if (a.y < b.y) return true ;
else return false ;
}
else return false ;
} struct node
{
int a[][] ;
int step ;
};
node st , end ; bool bfs ()
{
node ans , tmp ;
no rr[] ;
std::queue <node> q ;
while ( !q.empty ()) q.pop () ;
st.step = ;
q.push (st) ;
while ( !q.empty ()) {
ans = q.front () ; q.pop () ;
for (int i = ; i < ; i ++) {
for (int j = ; j < ; j ++) {
tmp = ans ;
int x = tmp.a[i][] + move[j][] , y = tmp.a[i][] + move[j][] ;
if (x < || y < || x >= || y >= ) continue ;
bool flag = ;
for (int i = ; i < ; i ++ ) {
if (x == tmp.a[i][] && y == tmp.a[i][])
flag = ;
}
if (flag ) {
x += move[j][] , y += move[j][] ;
if (x < || y < || x >= || y >= ) continue ;
for (int i = ; i < ; i ++) {
if (x == tmp.a[i][] && y == tmp.a[i][])
flag = ;
}
if ( !flag ) continue ;
}
tmp.step ++ ;
if (tmp.step > ) continue ;
tmp.a[i][] = x , tmp.a[i][] = y ;
if ( vis[tmp.a[][]] [tmp.a[][]] [tmp.a[][]] [tmp.a[][]] [tmp.a[][]] [tmp.a[][]] [tmp.a[][]] [tmp.a[][]]) continue;
vis[tmp.a[][]] [tmp.a[][]] [tmp.a[][]] [tmp.a[][]] [tmp.a[][]] [tmp.a[][]] [tmp.a[][]] [tmp.a[][]] = ;
int cnt = ;
for (int i = ; i < ; i ++) {
rr[i].x = tmp.a[i][] ; rr[i].y = tmp.a[i][] ;
}
std::sort (rr , rr + , cmp ) ;
/* for (int i = 0 ; i < 4 ; i ++) {
printf ("(%d , %d) , " , rr[i].x , rr[i].y ) ;
} puts ("") ; */
for (int i = ; i < ; i ++) {
if (rr[i].x != ee[i].x || rr[i].y != ee[i].y )
cnt ++ ;
}
if ( ! cnt ) return true ;
if (tmp.step + cnt > ) continue ;
q.push (tmp) ;
// printf ("step = %d\n" , tmp.step ) ;
}
}
}
return false ;
} int main ()
{
//freopen ("a.txt" , "r" , stdin ) ;
while ( ~ scanf ("%d%d" , &st.a[][] , &st.a[][])) {
st.a[][] -- ; st.a[][] -- ;
memset (vis , , sizeof(vis)) ;
for (int i = ; i < ; i ++) for (int j = ; j < ; j ++) { scanf ("%d" , &st.a[i][j]) ; st.a[i][j] -- ;}
for (int i = ; i < ; i ++) for (int j = ; j < ; j ++) { scanf ("%d" , &end.a[i][j]) ; end.a[i][j] -- ;}
for (int i = ; i < ; i ++) {
ee[i].x = end.a[i][] , ee[i].y = end.a[i][] ;
}
// std::sort (ss , ss + 4 , cmp ) ;
std::sort (ee , ee + , cmp ) ;
if ( bfs ()) puts ("YES") ;
else puts ("NO") ;
}
return ;
}
四枚棋子彼此不可分,所以对每次个状态因进行一下操作,比如说排序。
1455.Solitaire(bfs状态混摇)的更多相关文章
- ACM/ICPC 之 BFS+状态压缩(POJ1324(ZOJ1361))
求一条蛇到(1,1)的最短路长,题目不简单,状态较多,需要考虑状态压缩,ZOJ的数据似乎比POj弱一些 POJ1324(ZOJ1361)-Holedox Moving 题意:一条已知初始状态的蛇,求其 ...
- HDU1429+bfs+状态压缩
bfs+状态压缩思路:用2进制表示每个钥匙是否已经被找到.. /* bfs+状态压缩 思路:用2进制表示每个钥匙是否已经被找到. */ #include<algorithm> #inclu ...
- BFS+状态压缩 hdu-1885-Key Task
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1885 题目意思: 给一个矩阵,给一个起点多个终点,有些点有墙不能通过,有些点的位置有门,需要拿到相应 ...
- poj 1753 Flip Game(bfs状态压缩 或 dfs枚举)
Description Flip game squares. One side of each piece is white and the other one is black and each p ...
- BFS+状态压缩 HDU1429
胜利大逃亡(续) Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total S ...
- hdoj 5094 Maze 【BFS + 状态压缩】 【好多坑】
Maze Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 100000/100000 K (Java/Others) Total Sub ...
- HDU 3247 Resource Archiver (AC自己主动机 + BFS + 状态压缩DP)
题目链接:Resource Archiver 解析:n个正常的串.m个病毒串,问包括全部正常串(可重叠)且不包括不论什么病毒串的字符串的最小长度为多少. AC自己主动机 + bfs + 状态压缩DP ...
- UVA 10651 Pebble Solitaire(bfs + 哈希判重(记忆化搜索?))
Problem A Pebble Solitaire Input: standard input Output: standard output Time Limit: 1 second Pebble ...
- HDU 1885 Key Task (BFS + 状态压缩)
题意:给定一个n*m的矩阵,里面有门,有钥匙,有出口,问你逃出去的最短路径是多少. 析:这很明显是一个BFS,但是,里面又有其他的东西,所以我们考虑状态压缩,定义三维BFS,最后一维表示拿到钥匙的状态 ...
随机推荐
- python使用cookielib库示例分享
Python中cookielib库(python3中为http.cookiejar)为存储和管理cookie提供客户端支持,下面是使用示例 该模块主要功能是提供可存储cookie的对象.使用此模块捕获 ...
- Spring--PropertyPlaceholderConfigurer
1. PropertyPlaceholderConfigurer是个bean工厂后置处理器的实现,也就是 BeanFactoryPostProcessor接口的一个实现.PropertyPlaceho ...
- iOS - 基础面试知识
1.arc(automatic reference counting) OC对象被创建时引用计数从默认值0加1,当它被释放时候引用计数减1,引用计数减0时autorelease方法,销毁OC对象. 自 ...
- UVA11136Hoax or what( multiset的应用)
题目链接 题意:n天,每天往一个箱子里放m个数,放完之后取最大的Max和最小的min做差,并把这两个数去掉,求n天之后的和 multiset 和 set的原理是相似的,multiset可以存多个相同的 ...
- java编程思想-java 异常使用指南
应该在以下情况下使用异常: 在恰当的级别处理问题(在知道该如何处理的情况下才捕获异常). 解决问题并且重新调用产生异常的方法. 进行少许修补,然后绕过异常发生的地方继续执行. 用别的数据进行计算,以代 ...
- 申请https证书需要注意的4大问题
HTTPS证书是什么 https证书是数字证书中的一种,由受信任的数字证书颁发机构CA如[沃通CA]在验证服务器身份后颁发,具有服务器身份验证和数据传输加密 功能,因其要配置在服务器上,所以也称SSL ...
- 英文分词算法(Porter stemmer)
http://blog.csdn.net/whuslei/article/details/7398443 最近需要对英文进行分词处理,希望能够实现还原英文单词原型,比如 boys 变为 boy 等. ...
- UIProgressView(进度条控件)
UIProgressView *pr=[[UIProgressView alloc]init]; pr.frame=CGRectMake(150.0, 190.0, 130.0, 30.0);//进度 ...
- Unity逻辑热更新
http://www.xuanyusong.com/archives/3075 http://www.unitymanual.com/thread-36503-1-1.html http://www. ...
- 几个有用的jQuery代码片段
1.检测Internet Explorer版本 $(document).ready(function() { if (navigator.userAgent.match(/msie/i) ){ ale ...