Hdu1401 Solitaire 2017-01-18 17:21 33人阅读 评论(0) 收藏
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
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
respectively. Process to the end of file.
Output
Sample Input
4 4 4 5 5 4 6 5
2 4 3 3 3 6 4 6
Sample Output
YES
Source
思路:开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) 收藏的更多相关文章
- 团体程序设计天梯赛L1-019 谁先倒 2017-03-22 17:35 33人阅读 评论(0) 收藏
L1-019. 谁先倒 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 划拳是古老中国酒文化的一个有趣的组成部分.酒桌上两人划拳 ...
- 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 ...
- 搭建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 目录: ...
- APP被苹果APPStore拒绝的各种原因 分类: ios相关 app相关 2015-06-25 17:27 200人阅读 评论(0) 收藏
APP被苹果APPStore拒绝的各种原因 1.程序有重大bug,程序不能启动,或者中途退出. 2.绕过苹果的付费渠道,我们之前游戏里的用兑换码兑换金币. 3.游戏里有实物奖励的话,一定要说清楚,奖励 ...
- Java中的日期操作 分类: B1_JAVA 2015-02-16 17:55 6014人阅读 评论(0) 收藏
在日志中常用的记录当前时间及程序运行时长的方法: public void inject(Path urlDir) throws Exception { SimpleDateFormat sdf = n ...
- POJ3258 River Hopscotch 2017-05-11 17:58 36人阅读 评论(0) 收藏
River Hopscotch Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 13598 Accepted: 5791 ...
- Hdu428 漫步校园 2017-01-18 17:43 88人阅读 评论(0) 收藏
漫步校园 Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Total Submissi ...
- 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 ...
- strace使用详解(转) 分类: shell ubuntu 2014-11-27 17:48 134人阅读 评论(0) 收藏
(一) strace 命令 用途:打印 STREAMS 跟踪消息. 语法:strace [ mid sid level ] ... 描述:没有参数的 strace 命令将所有的驱动程序和模块中的 ...
随机推荐
- hive sql split 分隔符
Hive字符串分割函数 split(str, regex) - Splits str around occurances that match regexTime taken: 0.769 secon ...
- 出现了npm ERR! cb() never called!错误
执行npm i 命令时,出现了npm ERR! cb() never called!错误 解决方案: 1.首先清除你的npm缓存: sudo npm cache clean -f 一般情况执行完后再试 ...
- python 解析 yaml文件
import yaml with open("./test.yaml") as f: x = yaml.load(f) print(x) [{'tasks': [{'yum': { ...
- Linux 软件 安装到 /usr,/usr/local/ 还是 /opt 目录
Linux 的软件安装目录是也是有讲究的,理解这一点,在对系统管理是有益的 /usr:系统级的目录,可以理解为C:/Windows/,/usr/lib理解为C:/Windows/System32./u ...
- webpack 构建同时适用于手机和电脑的调试服务器
plugins plugins: [ new HtmlWebpackPlugin({ // 使用模板同时生成 pc.html和mobile.html title: 'pc', filename: 'p ...
- 重新认识trim,ltrim,rtrim,trailing和leading。
trim经常用来去除一个字符串的空格,select trim(' dhajkjwa ') from dual; 在上面的语句中,trim的前面也可以加r或者l,表示去掉前面或者后面的空格,r和l代表左 ...
- Eclipse中的SVN操作
--------------------siwuxie095 Eclipse 中的 SVN 操作 (一)发布项目 ...
- 53. Maximum Subarray最大求和子数组12 3(dp)
[抄题]: Find the contiguous subarray within an array (containing at least one number) which has the la ...
- 『SpringMVC』<context:include-filter>&<context:exclude-filter>爬坑
原文地址:https://segmentfault.com/a/1190000012972619 大家好,我是猪弟,猪在我心中从来不是蠢的代名词,而是懒的代名词,本次准备记录一个在开发测试过程中遇到的 ...
- Phalcon Framework的MVC结构及启动流程分析
目前的项目中选择了Phalcon Framework作为未来一段时间的核心框架.技术选型的原因会单开一篇Blog另说,本次优先对Phalcon的MVC架构与启动流程进行分析说明,如有遗漏还望指出. P ...