Solitaire

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

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

 #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状态混摇)的更多相关文章

  1. ACM/ICPC 之 BFS+状态压缩(POJ1324(ZOJ1361))

    求一条蛇到(1,1)的最短路长,题目不简单,状态较多,需要考虑状态压缩,ZOJ的数据似乎比POj弱一些 POJ1324(ZOJ1361)-Holedox Moving 题意:一条已知初始状态的蛇,求其 ...

  2. HDU1429+bfs+状态压缩

    bfs+状态压缩思路:用2进制表示每个钥匙是否已经被找到.. /* bfs+状态压缩 思路:用2进制表示每个钥匙是否已经被找到. */ #include<algorithm> #inclu ...

  3. BFS+状态压缩 hdu-1885-Key Task

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1885 题目意思: 给一个矩阵,给一个起点多个终点,有些点有墙不能通过,有些点的位置有门,需要拿到相应 ...

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

  5. BFS+状态压缩 HDU1429

    胜利大逃亡(续) Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total S ...

  6. hdoj 5094 Maze 【BFS + 状态压缩】 【好多坑】

    Maze Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 100000/100000 K (Java/Others) Total Sub ...

  7. HDU 3247 Resource Archiver (AC自己主动机 + BFS + 状态压缩DP)

    题目链接:Resource Archiver 解析:n个正常的串.m个病毒串,问包括全部正常串(可重叠)且不包括不论什么病毒串的字符串的最小长度为多少. AC自己主动机 + bfs + 状态压缩DP ...

  8. UVA 10651 Pebble Solitaire(bfs + 哈希判重(记忆化搜索?))

    Problem A Pebble Solitaire Input: standard input Output: standard output Time Limit: 1 second Pebble ...

  9. HDU 1885 Key Task (BFS + 状态压缩)

    题意:给定一个n*m的矩阵,里面有门,有钥匙,有出口,问你逃出去的最短路径是多少. 析:这很明显是一个BFS,但是,里面又有其他的东西,所以我们考虑状态压缩,定义三维BFS,最后一维表示拿到钥匙的状态 ...

随机推荐

  1. AngularJs ngCloak、ngController、ngInit、ngModel

    ngCloak ngCloak指令是为了防止Angular应用在启动加载的时候html模板将会被短暂性的展示.这个指令可以用来避免由HTML模板显示造成不良的闪烁效果. 格式: ng-cloak   ...

  2. pyenv的使用

    开始想使用virtual实现不同的版本的py隔离,然后发现不太方便,然后发现了这货. pyenv安装(ubuntu环境 ➜ ~ git clone git://github.com/yyuu/pyen ...

  3. NOIp2010 关押罪犯

    二分+2-SAT 先预处理出所有的v,然后离散化一下,在那个的基础上二分,对于每次二分出的值约束边权超过所二分出的边权的两点. //OJ 1322 //by Cydiater //2015.8.26 ...

  4. 网站缓存数据到tomcat服务器

    通过缓存使相同的数据不用重复加载,降低数据库的访问 public class CacheFilter implements Filter { //实例变量[每线程共享] private Map< ...

  5. Django_collections01

    python manage.py flush python manage.py changepassword username python manage.py createsuperuser pyt ...

  6. Saltstack之SSH(十一)

    Saltstack之SSH 安装 yum install -y salt-ssh 官方文档  https://docs.saltstack.com/en/latest/topics/ssh/index ...

  7. 从Paxos到ZooKeeper-二、ZooKeeper和Paxos

    ZooKeeper为分布式应用提供了高效且可靠的分布式协调服务,提供了诸如tong'yi统一命名服务.配置管理和分布式锁等分布式的基础服务.在解决分布式数据一致性方面,ZooKeeper并没有直接采用 ...

  8. apache安装错误error: APR not found解决办法

    linux安装时安装种类不同,一些组件包可能不会被安装,导致linux下安装软件的时候缺这个缺那个,今天为大家介绍linux安装apache时报apr找不到的解决办法 方法/步骤   下载依赖包 wg ...

  9. JavaScript学习笔记——DOM_document对象

    javascript-document对象详解 DOM document(html xml) object modledocument对象(DOM核心对象) 作用: 1.内容 innerHTML 2. ...

  10. 《深入理解bootstrap》读书笔记:第4章 CSS组件(下)

    十. 标签(.label类,label-xxx) 高亮一些标题部分. 1 2 3 4 5 6 <h1>HELLO<span class="label label-defau ...