Solitaire

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

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
 
Source
题意:
问在8步之内能否从前一个状态到后一个状态,棋子只能上下左右走或者如图中的方式走(跳过相邻的一个棋子)。
代码:
 /*
用bool开一个8维数组vis记录状态是否走过,很容易超内存,尽量少开数组减少内存。
*/
#include<iostream>
#include<cstring>
#include<queue>
using namespace std;
bool vis[][][][][][][][];
bool mp[][];
int dir[][]={,,,,-,,,-};
struct Lu
{
int x[],y[],cnt;
}L3;
bool chak(Lu L)
{
for(int i=;i<;i++){
if(!mp[L.x[i]][L.y[i]])
return ;
}
return ;
}
bool nice(Lu L)
{
for(int i=;i<;i++){
if(L.x[i]>||L.x[i]<||L.y[i]>||L.y[i]<)
return ;
}
if(vis[L.x[]][L.y[]][L.x[]][L.y[]][L.x[]][L.y[]][L.x[]][L.y[]])
return ;
return ;
}
bool empt(Lu L,int k)
{
for(int i=;i<;i++){
if(i==k) continue;
if(L.x[k]==L.x[i]&&L.y[k]==L.y[i])
return ;
}
return ;
}
bool bfs()
{
Lu L2;
queue<Lu>q;
q.push(L3);
while(!q.empty()){
L2=q.front();
q.pop();
if(chak(L2)) return ;
if(L2.cnt>=) continue;
for(int i=;i<;i++){
for(int j=;j<;j++){
L3=L2;
L3.x[i]=L2.x[i]+dir[j][];
L3.y[i]=L2.y[i]+dir[j][];
if(!nice(L3)) continue;
if(empt(L3,i)){
vis[L3.x[]][L3.y[]][L3.x[]][L3.y[]][L3.x[]][L3.y[]][L3.x[]][L3.y[]]=true;
L3.cnt++;
q.push(L3);
}
else{
L3.x[i]=L3.x[i]+dir[j][];
L3.y[i]=L3.y[i]+dir[j][];
if(!nice(L3)) continue;
if(empt(L3,i)){
vis[L3.x[]][L3.y[]][L3.x[]][L3.y[]][L3.x[]][L3.y[]][L3.x[]][L3.y[]]=true;
L3.cnt++;
q.push(L3);
}
}
}
}
}
return ;
}
int main()
{
int x,y;
while(cin>>x>>y){
x--;y--;
memset(vis,false,sizeof(vis));
memset(mp,false,sizeof(mp));
L3.x[]=x;L3.y[]=y;
for(int i=;i<;i++){
cin>>x>>y;
x--;y--;
L3.x[i]=x;L3.y[i]=y;
}
vis[L3.x[]][L3.y[]][L3.x[]][L3.y[]][L3.x[]][L3.y[]][L3.x[]][L3.y[]]=true;
for(int i=;i<;i++){
cin>>x>>y;
x--;y--;
mp[x][y]=true;
}
L3.cnt=;
int flag=bfs();
if(flag) cout<<"YES\n";
else cout<<"NO\n";
}
return ;
}

HDU1401 BFS的更多相关文章

  1. HDU1401(双向BFS)

    题意:http://acm.hdu.edu.cn/showproblem.php?pid=1401 给你8*8的棋盘和4个棋子初始位置.最终位置,问你能否在8次操作后达到该状态. 思路: 双向BFS, ...

  2. 图的遍历(搜索)算法(深度优先算法DFS和广度优先算法BFS)

    图的遍历的定义: 从图的某个顶点出发访问遍图中所有顶点,且每个顶点仅被访问一次.(连通图与非连通图) 深度优先遍历(DFS): 1.访问指定的起始顶点: 2.若当前访问的顶点的邻接顶点有未被访问的,则 ...

  3. 【BZOJ-1656】The Grove 树木 BFS + 射线法

    1656: [Usaco2006 Jan] The Grove 树木 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 186  Solved: 118[Su ...

  4. POJ 3278 Catch That Cow(bfs)

    传送门 Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 80273   Accepted: 25 ...

  5. POJ 2251 Dungeon Master(3D迷宫 bfs)

    传送门 Dungeon Master Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 28416   Accepted: 11 ...

  6. Sicily 1215: 脱离地牢(BFS)

    这道题按照题意直接BFS即可,主要要注意题意中的相遇是指两种情况:一种是同时到达同一格子,另一种是在移动时相遇,如Paris在(1,2),而Helen在(1,2),若下一步Paris到达(1,1),而 ...

  7. Sicily 1048: Inverso(BFS)

    题意是给出一个3*3的黑白网格,每点击其中一格就会使某些格子的颜色发生转变,求达到目标状态网格的操作.可用BFS搜索解答,用vector储存每次的操作 #include<bits/stdc++. ...

  8. Sicily 1444: Prime Path(BFS)

    题意为给出两个四位素数A.B,每次只能对A的某一位数字进行修改,使它成为另一个四位的素数,问最少经过多少操作,能使A变到B.可以直接进行BFS搜索 #include<bits/stdc++.h& ...

  9. Sicily 1051: 魔板(BFS+排重)

    相对1150题来说,这道题的N可能超过10,所以需要进行排重,即相同状态的魔板不要重复压倒队列里,这里我用map储存操作过的状态,也可以用康托编码来储存状态,这样时间缩短为0.03秒.关于康托展开可以 ...

随机推荐

  1. 解决Mysql连接池被关闭 ,hibernate尝试连接不能连接的问题。 (默认mysql连接池可以访问的时间为8小时,如果超过8小时没有连接,mysql会自动关闭连接池。系统发布第二天访问链接关闭问题。

    解决Mysql连接池被关闭  ,hibernate尝试连接不能连接的问题. (默认MySQL连接池可以访问的时间为8小时,如果超过8小时没有连接,mysql会自动关闭连接池. 所以系统发布第二天访问会 ...

  2. PLSQLDeveloper 常用设置

    PLSQL Developer常用设置及快捷键   1.登录后默认自动选中My Objects (已验证可用)   默认情况下,PLSQL Developer登录后,Brower里会选择All obj ...

  3. Hadoop单机模式安装-(1)安装设置虚拟环境

    网络上关于如何单机模式安装Hadoop的文章很多,按照其步骤走下来多数都失败,按照其操作弯路走过了不少但终究还是把问题都解决了,所以顺便自己详细记录下完整的安装过程. 此篇主要介绍如何在Windows ...

  4. Error staring Tomcat Cannot connect to VM错误解决办法

    最近经常遇myEclipse以debug方式启动tomcat的错误提示如下: 直接run方式启动没有问题. 一般这个问题等一会就不再出现,如果有耐心的话,就等几分钟再启动.如果没有耐心,可以试试下面的 ...

  5. 听大神说:https和http有何区别?(转)

    在做雅虎的时候,发现用第三方工具截取不到客户端与服务端的通讯,以前重来没碰到过这种情况,仔细看了看,它的url请求时基于https的,gg了下发现原来https协议和http有着很大的区别.总的来说, ...

  6. 如何在Meteor中使用npm模块?

    首先,请在AtmosphereJs上搜索有无相关的封装包.尽量采用已有的封装包,而不是自己封装. 有两种方法在项目中使用来自npm的模块. 封装为Meteor包并在项目中添加包.使用meteor cr ...

  7. Open 语法的使用

    我们通常会需要在命令中,打开文件输入信息,在python中我们就会使用open语法,进行此方面的操作.详细方式如下:#Python open 函数# 作用:打开一个文件# 语法:open(file[, ...

  8. 对html与body的一些研究与理解

    by zhangxinxu from http://www.zhangxinxu.com本文地址:http://www.zhangxinxu.com/wordpress/?p=259 一.写在前面的最 ...

  9. Struts 2

    <默认>execute -- 默认方法success -- 默认返回值 <配置><package><package name="default&qu ...

  10. Android中ListView的几种常见的优化方法

    Android中的ListView应该算是布局中几种最常用的组件之一了,使用也十分方便,下面将介绍ListView几种比较常见的优化方法: 首先我们给出一个没有任何优化的Listview的Adapte ...