HDU1401 BFS
Solitaire
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 4368 Accepted Submission(s): 1324
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.
/*
用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的更多相关文章
- HDU1401(双向BFS)
题意:http://acm.hdu.edu.cn/showproblem.php?pid=1401 给你8*8的棋盘和4个棋子初始位置.最终位置,问你能否在8次操作后达到该状态. 思路: 双向BFS, ...
- 图的遍历(搜索)算法(深度优先算法DFS和广度优先算法BFS)
图的遍历的定义: 从图的某个顶点出发访问遍图中所有顶点,且每个顶点仅被访问一次.(连通图与非连通图) 深度优先遍历(DFS): 1.访问指定的起始顶点: 2.若当前访问的顶点的邻接顶点有未被访问的,则 ...
- 【BZOJ-1656】The Grove 树木 BFS + 射线法
1656: [Usaco2006 Jan] The Grove 树木 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 186 Solved: 118[Su ...
- POJ 3278 Catch That Cow(bfs)
传送门 Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 80273 Accepted: 25 ...
- POJ 2251 Dungeon Master(3D迷宫 bfs)
传送门 Dungeon Master Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 28416 Accepted: 11 ...
- Sicily 1215: 脱离地牢(BFS)
这道题按照题意直接BFS即可,主要要注意题意中的相遇是指两种情况:一种是同时到达同一格子,另一种是在移动时相遇,如Paris在(1,2),而Helen在(1,2),若下一步Paris到达(1,1),而 ...
- Sicily 1048: Inverso(BFS)
题意是给出一个3*3的黑白网格,每点击其中一格就会使某些格子的颜色发生转变,求达到目标状态网格的操作.可用BFS搜索解答,用vector储存每次的操作 #include<bits/stdc++. ...
- Sicily 1444: Prime Path(BFS)
题意为给出两个四位素数A.B,每次只能对A的某一位数字进行修改,使它成为另一个四位的素数,问最少经过多少操作,能使A变到B.可以直接进行BFS搜索 #include<bits/stdc++.h& ...
- Sicily 1051: 魔板(BFS+排重)
相对1150题来说,这道题的N可能超过10,所以需要进行排重,即相同状态的魔板不要重复压倒队列里,这里我用map储存操作过的状态,也可以用康托编码来储存状态,这样时间缩短为0.03秒.关于康托展开可以 ...
随机推荐
- Android源码阅读 – Zygote
@Dlive 本文档: 使用的Android源码版本为:Android-4.4.3_r1 kitkat (源码下载: http://source.android.com/source/index.ht ...
- mysql查询时强制区分大小写
转载自:http://snowolf.iteye.com/blog/1681944 平时很少会考虑数据存储需要明确字符串类型字段的大小写,MySQL默认的查询也不区分大小写.但作为用户信息,一旦用户名 ...
- php学习中——知识点(1)
php是嵌入式脚本语言(意义也就不言而喻) 标识:<?php .... ?> 输出:echo "**"; 使用美元符号($)后跟变量名表示变量,区分大 ...
- 【jquery】基础知识
jquery简介 1 jquery是什么 jquery由美国人John Resig创建,至今已吸引了来自世界各地的众多 javascript高手加入其team. jQuery是继prototype之后 ...
- 修改linux的系统时间和时区
时间: date命令将日期设置为2016年12月16日 ---- date -s 12/16/16 将时间设置为9点28分50秒 ---- date -s 09:28:50 时区: tzsel ...
- monkey之三:monkey测试测略(摘抄)
一.分类 Monkey测试针对不同的对象,不同的目的,采用不同的测略方案. 测试类型分为: 应用程序的稳定性测试和压力测试 测试对象分为: 单个APK和多个APK集合 测试目的分为: 解决问题的测试( ...
- SpringBoot前世今生
序 本文主要讲述spring boot的由来,即其它诞生的背景,初衷,现状,及对未来的展望. 背景 在很早的年代,J2EE还是java企业级应用的王者规范,EJB风行其道.后来有一个叫Rod John ...
- zookeeper3.3.6 伪分布式安装
下载地址(http://zookeeper.apache.org/releases.html#download) 一:下载zookeeper的安装包,解压,进入到zk的目录文件,进入conf目录 ...
- Linux更改计算机名称
1.修改:vim /etc/hosts 2.修改:vim /etc/sysconfig/network 3.重启:reboot 如不重启可以输入:hostname 新改的计算机名称,然后su
- 【JavaScript】 Webpack安装及文件打包
背景 最近开启一个新项目,需要对前端的各类资源文件进行打包,经过多方调研后,决定使用webpack工具.但是网上的教程多是将webpack直接作为服务器使用,而我只是想将其作为单纯的资源打包工具而已. ...