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).

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 中的map(), reduce(), filter
据说是函数式编程的一个函数(然后也有人tucao py不太适合干这个),在我看来算是pythonic的一种写法. 简化了我们的操作,比方我们想将list中的数字都加1,最基本的可能是编写一个函数: I ...
- linux下svn 搭建
linux 下SVN搭建加自动提交更新到指定目录一.安装#yum install subversion yum安装SVN#mkdir -p /opt/svn/repos 创建s ...
- zabbix监控系列(1)之zabbix-server安装
推荐使用yum来安装 第一步:LAMP平台 zabbix使用php开发的,所以依赖于LAMP或者LNMP平台,由于http+mysql用yum安装及其方便,所以我在这里使用yum安装. yum -y ...
- python requests
快速上手http://docs.python-requests.org/zh_CN/latest/user/quickstart.html Requests 是使用 Apache2 Licensed ...
- css属性设置
css在线编辑工具地址:http://tool.chinaz.com/Tools/CssDesigner.aspx 案例详情: http://dongtianee.sinaapp.com/index. ...
- OC之NSString、NSMutableString学习笔记 常用方法
NSString篇: 1.字符串连接 NSString *beijing = @"北京"; NSString *welcome = [beijing stringByAppendi ...
- stamp-po的作用
stamp-po是表示po文件是否有更新,有更新,则重新编译一次
- join的理解
thread.Join把指定的线程加入到当前线程,可以将两个交替执行的线程合并为顺序执行的线程.比如在线程B中调用了线程A的Join()方法,直到线程A执行完毕后,才会继续执行线程B. t.join( ...
- System类
System类是一些与系统相关属性和方法的集合,而且System类中所有的属性都是静态的,要想引用这些属性和方法,直接使用System类调用即可. //======================== ...
- Hash Table in C
http://www.sparknotes.com/cs/searching/hashtables/section3.rhtml http://en.literateprograms.org/Spec ...