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秒.关于康托展开可以 ...
随机推荐
- Mybatis 总结
1.如何传递多个参数 mybatis中,如果接口有多个参数,那么在mapper.xml中,可以通过#{0,VARCHAR},#{1,VARCHAR}或#{param1,VARCHAR},#{param ...
- Java 中使用javah编译头文件出现找不到类的情况
在工程的bin目录下,输入命令: javah -classpath . -jni 类路径.JNI类
- Linux时间同步,ntpdate命令、ntpd服务详解
声明:以下内容来自网友整理(http://blog.sina.com.cn/s/blog_636a55070101u1mg.html),为便于以后学习暂时收录,请不要随意转载 Linux默认情况下,系 ...
- 获取json数据
通过异步获取json来展示数据表格,性能提高不少.实例如下: 前台: <!DOCTYPE html> <html xmlns="http://www.w3.org/1999 ...
- 【JavaScript】 Webpack安装及文件打包
背景 最近开启一个新项目,需要对前端的各类资源文件进行打包,经过多方调研后,决定使用webpack工具.但是网上的教程多是将webpack直接作为服务器使用,而我只是想将其作为单纯的资源打包工具而已. ...
- fatal error LNK1169: 找到一个或多个多重定义的符号
申明全局变量,全局函数一定要在cpp中申明,其他类引用该全局变量就include该cpp的h文件,然后extern一下就好了.否则容易出现该重复定义错误. 这个"容易"是如何解释的 ...
- 各大IT技术博客排行榜
cnblogs 积分排名前3000名 http://www.cnblogs.com/ 左侧有推荐博客排行 cppblog http://www.cppblog.com/AllBloggers.aspx ...
- .NET 读取本地文件绑定到GridViewRow
wjgl.aspx.cs: using System; using System.Collections; using System.Configuration; using System.Data; ...
- 动作手游实时PVP帧同步方案(客户端)
1.概述 1.1.基于UDP的帧同步方案 在技术选型方面,之所以选择帧同步方案,在Kevin的一篇介绍PVP帧同步后台实现的文章中已经做了详细叙述,这里简单摘要如下: 高一致性.如果每一帧的输入都同步 ...
- C# DataGridView绑定数据源
第一种: DataSet ds=new DataSet (); ]; 第二种: DataTable dt=new DataTable(); this.dataGridView1.DataSource= ...