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 命令将所有的驱动程序和模块中的 ...
随机推荐
- HDFS 好的文章链接
http://www.cnblogs.com/linuxprobe/p/5594431.html http://www.daniubiji.cn/archives/596 http://blog.cs ...
- textbox显示定位到最后一行(最新一行)
this.textBox1.Select(this.txtMsgInfo.TextLength, 0); this.textBox1.ScrollToCaret();
- conductor 事件处理程序
Introduction conductor中的事件提供工作流之间的松散耦合,并支持从外部系统生成和消耗事件. 包括: 1. 能够在外部系统像SQS或Conductor内部生成一个事件(消息). 2. ...
- destoon 分页
php: global $pagesize,$page; $pagesize = 10;//分页改为10条一页 $offset or $offset = ($page-1)*$pagesize; $t ...
- shell中交互输入自动化
shell中交互输入自动化 shell中有时我们需要交互,但是呢我们又不想每次从stdin输入,想让其自动化,这时我们就要使shell交互输入自动化了.这个功能很有用的哟.好好学习. 1 利用重 ...
- LINUX 修复relocation error: /lib/tls/libc.so.6
错误信息: relocation error: /lib/tls/libc.so.6: symbol _dl_tls_get_addr_soft, version GLIBC_PRIVATE not ...
- drupal 用法小结,drupal select ,query ,distinct
https://api.drupal.org/api/drupal/includes%21actions.inc/function/actions_do/7.x addFileds : 这个更全点: ...
- 第八章 高级搜索树 (a2)伸展树:双层伸展
- subprocess 粘包问题
1.执行命令: 在py代码中去如何调用操作系统的命令 新模块:subprocess r = subprocess.Popen('ls',shell=True,stdout=subprocess.PIP ...
- day5:vcp考试
Q81. An administrator needs to recover disk space on a previously-used thin provisioned virtual disk ...