poj 1475 uva 589 - Pushing Boxes
题目大意
人推箱子从起点到终点,要求推箱子的次数最少,并打印出来人移动的路径。
题目分析
对于箱子进行宽搜的同时,要兼顾人是否能够把箱子推到相应的位置
每一次对箱子bfs 然后对人再bfs
#include<cstdio>
#include<cstring>
#include<queue>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<string>
using namespace std;
const int dir[4][2] = {{-1,0},{1,0},{0,1},{0,-1}};
const char dirman[4] = {'n','s','e','w'};
const char dirbox[4] = {'N','S','E','W'};
char a[21][21];
int r, c;
struct node
{
int manx,many;
int boxx,boxy;
string path;
node():path("") {}
node(int _manx,int _many,int _boxx,int _boxy ,string &_path):manx(_manx),many(_many),boxx(_boxx),boxy(_boxy),path(_path) {}
};
struct pos
{
int x,y;
string path;
pos():path("") {}
pos(int _x, int _y) : x(_x), y(_y), path("") {}
pos(int _x,int _y,string &_path):x(_x),y(_y),path(_path) {}
};
bool judge(int x,int y)
{
if(x>0&&x<=r&&y>0&&y<=c&&a[x][y]!='#')
return true ;
return false;
}
bool bfsman(string &path ,pos st,pos ed)
{
bool vis[21][21];
memset(vis,false,sizeof(vis));
queue<pos> q;
q.push(st);
vis[st.x][st.y]=true;
while(!q.empty())
{
pos s=q.front();
q.pop();
if(s.x==ed.x&&s.y==ed.y)
{
path=s.path;
return true;
}
for(int i=0; i<4; i++)
{
int x=s.x+dir[i][0];
int y=s.y+dir[i][1];
string p = s.path + dirman[i];
if(judge(x,y)&&!vis[x][y])
{
q.push(pos(x,y,p));
vis[x][y]=true;
}
}
}
return false;
}
bool bfsbox(string &path,node s,pos t)
{
bool vist[23][23];
memset(vist,false,sizeof(vist));
vist[s.boxx][s.boxy]=true;
queue<node> Q;
Q.push(s);
while(!Q.empty())
{
//printf("==\n");
node st=Q.front();
Q.pop();
if(st.boxx==t.x&&st.boxy==t.y)
{
path=st.path;
//cout<<path<<endl;
return true;
}
a[st.boxx][st.boxy]='#';
for(int i=0; i<4; i++)
{
int x=st.boxx+dir[i][0];
int y=st.boxy+dir[i][1];
int x_=st.boxx-dir[i][0];
int y_=st.boxy-dir[i][1];
if(!vist[x][y]&&judge(x,y)&&judge(x_,y_))
{
string pathman="";
if(bfsman(pathman,pos(st.manx,st.many),pos(x_,y_)))
{
vist[x][y]=true;
string p=st.path+pathman+dirbox[i];
Q.push(node(st.boxx, st.boxy, x, y, p));
}
}
}
a[st.boxx][st.boxy]='.';
}
return false;
}
int main()
{
int cases=0;
while(scanf("%d%d",&r,&c)!=EOF)
{
if(c==0&r==0)
break;
node s;
pos t;
for(int i=1; i<=r; i++)
{
scanf("%s",&a[i][1]);
for(int j=1; j<=c; j++)
{
if(a[i][j]=='T')
{
t.x=i;
t.y=j;
}
if(a[i][j]=='B')
{
s.boxx=i;
s.boxy=j;
}
if(a[i][j]=='S')
{
s.manx=i;
s.many=j;
}
}
}
string path="";
if (bfsbox(path, s, t))
printf("Maze #%d\n%s\n\n", ++cases, path.c_str());
else
printf("Maze #%d\nImpossible.\n\n", ++cases);
}
return 0;
}
poj 1475 uva 589 - Pushing Boxes的更多相关文章
- poj 1475 || zoj 249 Pushing Boxes
http://poj.org/problem?id=1475 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=249 Pushin ...
- [poj P1475] Pushing Boxes
[poj P1475] Pushing Boxes Time Limit: 2000MS Memory Limit: 131072K Special Judge Description Ima ...
- HDU 1475 Pushing Boxes
Pushing Boxes Time Limit: 2000ms Memory Limit: 131072KB This problem will be judged on PKU. Original ...
- Pushing Boxes(广度优先搜索)
题目传送门 首先说明我这个代码和lyd的有点不同:可能更加复杂 既然要求以箱子步数为第一关键字,人的步数为第二关键字,那么我们可以想先找到箱子的最短路径.但单单找到箱子的最短路肯定不行啊,因为有时候不 ...
- UVa 103 Stacking Boxes --- DAG上的动态规划
UVa 103 题目大意:给定n个箱子,每个箱子有m个维度, 一个箱子可以嵌套在另一个箱子中当且仅当该箱子的所有的维度大小全部小于另一个箱子的相应维度, (注意箱子可以旋转,即箱子维度可以互换),求最 ...
- 『Pushing Boxes 双重bfs』
Pushing Boxes Description Imagine you are standing inside a two-dimensional maze composed of square ...
- POJ1475 Pushing Boxes(双搜索)
POJ1475 Pushing Boxes 推箱子,#表示墙,B表示箱子的起点,T表示箱子的目标位置,S表示人的起点 本题没有 Special Judge,多解时,先最小化箱子被推动的次数,再最小化 ...
- POJ 3525/UVA 1396 Most Distant Point from the Sea(二分+半平面交)
Description The main land of Japan called Honshu is an island surrounded by the sea. In such an isla ...
- POJ 1475 Pushing Boxes 搜索- 两重BFS
题目地址: http://poj.org/problem?id=1475 两重BFS就行了,第一重是搜索箱子,第二重搜索人能不能到达推箱子的地方. AC代码: #include <iostrea ...
随机推荐
- 告别硬编码-发个获取未导出函数地址的Dll及源码
还在为找内核未导出函数地址而苦恼嘛? 还在为硬编码通用性差而不爽吗? 还在为暴搜内核老蓝屏而痛苦吗? 请看这里: 最近老要用到内核未导出的函数及一些结构,不想再找特征码了,准备到网上找点符号文件解析的 ...
- 百度上传android包:应用名解析失败!
manifest 里面<application增加android:label="@string/app_name"
- Linux - gcc和g++的区别
一般linux系统都自带了gcc编译器的,你可以用你的安装光盘去安装,如果你是觉得自带的gcc版本太低了,可以去gcc的官方网站可以下载到,编译需要很长的时间,如果你只编译C或者C++可以只下载gcc ...
- Linux-编译器gcc/g++编译步骤
gcc和g++现在是gnu中最主要和最流行的c&c++编译器.g++是c++的命令,以.cpp为主:对于c语言后缀名一般为.c,这时候命令换做gcc即可.编译器是根据gcc还是g++来确定是按 ...
- 读者写者问题继 读写锁SRWLock
在<秒杀多线程第十一篇读者写者问题>文章中我们使用事件和一个记录读者个数的变量来解决读者写者问题.问题虽然得到了解决,但代码有点复杂.本篇将介绍一种新方法--读写锁SRWLock来解决这一 ...
- Android google map 两点之间的距离
在Android google map中,有时候会碰到计算两地的距离,下面的辅助类就可以帮助你计算距离: public class DistanceHelper { /** Names for the ...
- 有关PHP安装,基础学习
首先要安装 wamp 和 NavicatMySQLFront (要在非中文目录下) 打开DW 点击站点 ——新建站点:设置站点名称,选择本地站点文件夹:wap\www 服务器:添加 + 服务器名 ...
- poj2392 多重背包
//Accepted 868 KB 188 ms //多重背包 #include <cstdio> #include <cstring> #include <iostre ...
- ubuntu下openoffice开发环境配置
安装openoffice或者liboffice 路径:/usr/lib/openoffice/program ,soffice 开启服务: 安装JDK 其默认路径:jdk7 版本号:1.7...,jd ...
- Android Context
http://www.cnblogs.com/android100/p/Android-Context.html