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. Request 对象

    Request 对象用于检索从浏览器向服务器发送的请求中的信息. 1.使用Request对象的Browser属性,可以访问HttpBrowserCapabilities属性获得当前正在使用哪种类型的浏 ...

  2. OpenGLES入门笔记一

    学习地址:http://www.raywenderlich.com/3664/opengl-tutorial-for-ios-opengl-es-2-0 中文翻译:http://www.cnblogs ...

  3. ADS1.2安装教程

    工具/原料 ADS1.2 ADS1.2安装教程 1 在安装包内找到”Setup“,点击安装. 点击”Next“.然后进入License Agreement ,点击”Yes“. 点击了Yes之后,我们选 ...

  4. C#中字符串的操作

    1.Replace(替换字符):public string Replace(char oldChar,char newChar);在对象中寻找oldChar,如果寻找到,就用newChar将oldCh ...

  5. Yoshua Bengio 2016年5月11日在Twitter Boston的演讲PPT

    Yoshua Bengio最新演讲:Attention 让深度学习取得巨大成功(46ppt) Yoshua Bengio,电脑科学家,毕业于麦吉尔大学,在MIT和AT&T贝尔实验室做过博士后研 ...

  6. Codeforces 697A - Pineapple Incident

    题目链接:http://codeforces.com/problemset/problem/697/A 题目大意: 输入三个数 t,s,x; 判断x是否合适 合适的位置位 t , t+s, t+s+1 ...

  7. Mysql 与 Python socket

    py1.py # -*- coding: utf-8 -*- import sqlalchemy import tushare import pandas import socket import s ...

  8. Java Native Interface Specification

    http://docs.oracle.com/javase/6/docs/technotes/guides/jni/spec/jniTOC.html http://www.ibm.com/develo ...

  9. 移动端a标签点击图片有阴影处理

    移动端我们在点击页面中的一些图片的时候会出现阴影.处理方法只要给a标签加上 a { -webkit-tap-highlight-color: transparent; -webkit-touch-ca ...

  10. Nginx 支持 WAF 防护功能实战

    WAF(Web Application Firewall),中文名称叫做“Web应用防火墙 WAF的定义是这样的:Web应用防火墙是通过执行一系列针对HTTP/HTTPS的安全策略来专门为Web应用提 ...