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 命令将所有的驱动程序和模块中的 ...
随机推荐
- ubuntu 16.04 完整安装 phantomjs
摘自 stackoverflow sudo apt-get install nodejssudo apt-get install nodejs-legacysudo apt-get install n ...
- Java web struct入门基础知识
1.Struts2的前身是Opensymphony的Webwork2,实际上Strut和Webwork2合并后形成Struts2. 2.一个HelloWord示例 1)创建Web应用,所需要的Ja ...
- Ubuntu 安装 Zabbix 3.2详细步骤
创建 zabbix 用户 因为zabbix 程序的守护进程需要非特权用户,所以需要创建一个 zabbix 用户,即使从 root 用户启动 zabbix 程序,也会自动切换到 zabbix 用户,所以 ...
- Java8 lambda表达式语法 1
本文主要记录自己学习Java8的历程,方便大家一起探讨和自己的备忘.因为本人也是刚刚开始学习Java8,所以文中肯定有错误和理解偏差的地方,希望大家帮忙指出,我会持续修改和优化.本文是该系列的第一篇, ...
- Microsoft SQL Server, 错误:4064的解决方法 (转载)
SQL SERVER – Fix : Error: 4064 – Cannot open user default database. Login failed. Login failed for u ...
- catkin_make 与cmake
http://blog.csdn.net/zyh821351004/article/details/50388429 1. catkin_make 与cmake的关系 程序在cmake编译的流程: ...
- 创建和修改主键 (SQL)
添加主键, ALTER TABLE [表名:OrderInfo] Add PRIMARY KEY ([列名:ProductID, UserID...]) 多个列则是组合主键 删除主键, ALTER ...
- geoserver的rest服务介绍,搭建java程序
在geoserver中使用 Restlet 来提供所有的rest服务,并且geoserver中所有的在/rest目录下的请求都被看作为一个restful server,下图就是rest服务的调用过程 ...
- 局域网代理通过wget下载
下载方法: wget -r -p -np -k http://ftp.loongnix.org/os/Fedora13-o32/RPMS/mipsel/ -r, --recursive(递归) ...
- UI设计不就是画线框,凭什么年薪30W?
作为一枚界面设计师 我真的很想为UI设计抱不平啊!! UI设计真是一个备受不解的职业 常会被误解,然后出现以下场景 程序欧巴: 界面画好没?按钮圆的方的不都能用吗?纠结那多干嘛? 产品经理: 这次我们 ...