Solitaire

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 10   Accepted Submission(s) : 5

Font: Times New Roman | Verdana | Georgia

Font Size: ← →

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

Southwestern Europe 2002


——————————————————————————————————————————————

题意:
问在8步之内能否从前一个状态到后一个状态,棋子只能上下左右走或者如图中的方式走(跳过相邻的一个棋子)。

思路:开8维数组记录状态直接bfs


#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
#include<cmath>
using namespace std;
bool vir[8][8][8][8][8][8][8][8];
int ed[8][10];
int dir[4][2]= {{-1,0},{1,0},{0,-1},{0,1}};
struct node
{
int x[4],y[4],cnt;
}; bool check(node d)
{
for(int i=0; i<4; i++)
{
if(d.x[i]<0||d.x[i]>=8||d.y[i]<0||d.y[i]>=8)
return 0;
}
if(vir[d.x[0]][d.y[0]][d.x[1]][d.y[1]][d.x[2]][d.y[2]][d.x[3]][d.y[3]])
{
return 0;
}
return 1;
} int out(node d)
{
for(int i=0; i<4; i++)
{
if(ed[d.x[i]][d.y[i]]==0)
return 0;
}
return 1;
} int pan(int x,int y,node b,int k)
{
for(int i=0; i<4; i++)
{
if(i!=k&&x==b.x[i]&&y==b.y[i])
return 1;
}
return 0;
} void bfs(node a)
{
queue<node>q;
node f,d;
f=a;
vir[f.x[0]][f.y[0]][f.x[1]][f.y[1]][f.x[2]][f.y[2]][f.x[3]][f.y[3]]=1;
q.push(f);
while(!q.empty())
{
f=q.front();
q.pop();
if(f.cnt>=8)
{
printf("NO\n");
return;
}
if(out(f))
{
printf("YES\n");
return;
} for(int i=0; i<4; i++)
{
for(int j=0; j<4; j++)
{
d=f;
d.x[i]=f.x[i]+dir[j][0];
d.y[i]=f.y[i]+dir[j][1];
if(!check(d))
continue; if(!pan(d.x[i],d.y[i],f,i))
{
if(out(d))
{
printf("YES\n");
return;
}
vir[d.x[0]][d.y[0]][d.x[1]][d.y[1]][d.x[2]][d.y[2]][d.x[3]][d.y[3]]=1;
d.cnt=f.cnt+1;
q.push(d);
}
else
{
d.x[i]=d.x[i]+dir[j][0];
d.y[i]=d.y[i]+dir[j][1];
if(!check(d))
continue;
if(!pan(d.x[i],d.y[i],f,i))
{
if(out(d))
{
printf("YES\n");
return;
}
vir[d.x[0]][d.y[0]][d.x[1]][d.y[1]][d.x[2]][d.y[2]][d.x[3]][d.y[3]]=1;
d.cnt=f.cnt+1;
q.push(d);
}
} }
}
}
printf("NO\n");
} int main()
{
node a;
int o,di,dj;
while(~scanf("%d%d",&di,&dj))
{
a.x[0]=di-1;
a.y[0]=dj-1;
memset(ed,0,sizeof(ed));
for(int i=1; i<4; i++)
{
scanf("%d%d",&di,&dj);
a.x[i]=di-1;
a.y[i]=dj-1;
}
for(int i=0; i<4; i++)
{
scanf("%d%d",&di,&dj);
ed[di-1][dj-1]=1;
}
memset (vir,0,sizeof(vir));
a.cnt=0;
bfs(a);
}
return 0;
}

Hdu1401 Solitaire 2017-01-18 17:21 33人阅读 评论(0) 收藏的更多相关文章

  1. 团体程序设计天梯赛L1-019 谁先倒 2017-03-22 17:35 33人阅读 评论(0) 收藏

    L1-019. 谁先倒 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 划拳是古老中国酒文化的一个有趣的组成部分.酒桌上两人划拳 ...

  2. hdu 1030 Delta-wave (C++, 0ms, explanatory comments.) 分类: hdoj 2015-06-15 12:21 45人阅读 评论(0) 收藏

    problem description http://acm.hdu.edu.cn/showproblem.php?pid=1030 #include <cstdio> #include ...

  3. 搭建hadoop2.6.0集群环境 分类: A1_HADOOP 2015-04-20 07:21 459人阅读 评论(0) 收藏

    一.规划 (一)硬件资源 10.171.29.191 master 10.171.94.155  slave1 10.251.0.197 slave3 (二)基本资料 用户:  jediael 目录: ...

  4. APP被苹果APPStore拒绝的各种原因 分类: ios相关 app相关 2015-06-25 17:27 200人阅读 评论(0) 收藏

    APP被苹果APPStore拒绝的各种原因 1.程序有重大bug,程序不能启动,或者中途退出. 2.绕过苹果的付费渠道,我们之前游戏里的用兑换码兑换金币. 3.游戏里有实物奖励的话,一定要说清楚,奖励 ...

  5. Java中的日期操作 分类: B1_JAVA 2015-02-16 17:55 6014人阅读 评论(0) 收藏

    在日志中常用的记录当前时间及程序运行时长的方法: public void inject(Path urlDir) throws Exception { SimpleDateFormat sdf = n ...

  6. POJ3258 River Hopscotch 2017-05-11 17:58 36人阅读 评论(0) 收藏

    River Hopscotch Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 13598   Accepted: 5791 ...

  7. Hdu428 漫步校园 2017-01-18 17:43 88人阅读 评论(0) 收藏

    漫步校园 Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Submissi ...

  8. The 3n + 1 problem 分类: POJ 2015-06-12 17:50 11人阅读 评论(0) 收藏

    The 3n + 1 problem Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 53927   Accepted: 17 ...

  9. strace使用详解(转) 分类: shell ubuntu 2014-11-27 17:48 134人阅读 评论(0) 收藏

    (一) strace 命令    用途:打印 STREAMS 跟踪消息. 语法:strace [ mid sid level ] ... 描述:没有参数的 strace 命令将所有的驱动程序和模块中的 ...

随机推荐

  1. 创建jsp+Servlet+JavaBean+JDBC+MySQL项目的过程

    1 根据需求建立Mysql数据,确立数据库的表的字段.属性.主键,外键等.下面我使用的数据库名dev ,表名user,字段  name,设置为主键.用户名不能为空,字段password,密码 2 在E ...

  2. gerp , sed , awk

    gerp 查找, sed 编辑, awk 根据内容分析并处理. awk(关键字:分析&处理) 一行一行的分析处理 awk '条件类型1{动作1}条件类型2{动作2}' filename, aw ...

  3. Validate the date format

    Validate the date format function checkdate(input) { var validformat = /^\d{2}\/\d{2}\/\d{4}$/; //Ba ...

  4. python处理分隔大文件

    4个.sql格式的文件,2G大小,直接插入mysql数据中,文件太大了,导入不进去. 太大的文件用python处理也很麻烦,处理不了,只能先分隔成小文件处理. 文件中数据格式:其中values里面的数 ...

  5. https://developer.mozilla.org/

    Document/querySelector      https://developer.mozilla.org/zh-CN/docs/Web/API/Document/querySelector

  6. java并发:AtomicInteger 以及CAS无锁算法【转载】

    1 AtomicInteger解析 众所周知,在多线程并发的情况下,对于成员变量,可能是线程不安全的: 一个很简单的例子,假设我存在两个线程,让一个整数自增1000次,那么最终的值应该是1000:但是 ...

  7. 【校招面试 之 C/C++】第8题 C++中的静态绑定与动态绑定

    转自:https://blog.csdn.net/chgaowei/article/details/6427731   做了部分修改 为了支持c++的多态性,才用了动态绑定和静态绑定.理解他们的区别有 ...

  8. Mysql优化性能优化21条

    今天,数据库的操作越来越成为整个应用的性能瓶颈了,这点对于Web应用尤其明显.关于数据库的性能,这并不只是DBA才需要担心的事,而这更是我们程序员需要去关注的事情.当我们去设计数据库表结构,对操作数据 ...

  9. Linux下Google Test (GTest)测试环境搭建步骤

    1.下载GTEST 下载链接为:https://code.google.com/p/googletest/downloads/list 目前GTEST的最新版本为gtest-1.7.0.zip,因此我 ...

  10. jq给动态生成的标签绑定事件的几种方法

    经常遇到给动态生成的标签绑定事件不好用,自己简单测试总结了下,结论如下了: body> <!-- 下面是用纯动态方式生成标签 --> <div id="d2" ...