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,最后一维表示拿到钥匙的状态 ...
随机推荐
- Java线程池的那些事
熟悉java多线程的朋友一定十分了解java的线程池,jdk中的核心实现类为java.util.concurrent.ThreadPoolExecutor.大家可能了解到它的原理,甚至看过它的源码:但 ...
- BZOJ2152:聪聪可可
传送门 点分治常规题.练习模板 //OJ 2077 //by Cydiater //2016.9.23 #include <iostream> #include <cstdio> ...
- javamail实践
public static void main(String[] args) throws Exception, Exception { MimeMessage message=new MimeMes ...
- JAVA通过md5方法进行加密
import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; /* * MD5 算法 * */ ...
- 【原】js获取height为auto的高度问题
今天用react写一个页面,需要获取一个img高度设置为auto的高度,可是一直获取到的要么是0,要么是null,因为页面加载完了图片不一定加载完. 当我把高度由 auto 设置为固定值之后,又可以获 ...
- DS18B20函数库建立实验
1.主代码: /* 温度传感器 */#include "DS18B20.h"#include"def.h"u16 get_temp (void){ fl ...
- parted命令详解
parted命令详解 用法:parted [选项]... [设备 [命令 [参数]...]...] 将带有“参数”的命令应用于“设备”.如果没有给出“命令”,则以交互模式运行. 帮助选项: ...
- JavaWeb学习笔记——XML和SAX解析区别
- Android学习笔记——xml
转自http://blog.csdn.net/sam_zhang1984 解析 XML 有两种方式: SAX 和 DOM .它们各有利弊. DOM 是把 XML 文档全部装载到内存中,然后当成一树进行 ...
- Python in Unity
http://stackoverflow.com/questions/11766181/ironpython-in-unity3d