题目大意

人推箱子从起点到终点,要求推箱子的次数最少,并打印出来人移动的路径。

题目分析

对于箱子进行宽搜的同时,要兼顾人是否能够把箱子推到相应的位置

每一次对箱子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的更多相关文章

  1. poj 1475 || zoj 249 Pushing Boxes

    http://poj.org/problem?id=1475 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=249 Pushin ...

  2. [poj P1475] Pushing Boxes

    [poj P1475] Pushing Boxes Time Limit: 2000MS   Memory Limit: 131072K   Special Judge Description Ima ...

  3. HDU 1475 Pushing Boxes

    Pushing Boxes Time Limit: 2000ms Memory Limit: 131072KB This problem will be judged on PKU. Original ...

  4. Pushing Boxes(广度优先搜索)

    题目传送门 首先说明我这个代码和lyd的有点不同:可能更加复杂 既然要求以箱子步数为第一关键字,人的步数为第二关键字,那么我们可以想先找到箱子的最短路径.但单单找到箱子的最短路肯定不行啊,因为有时候不 ...

  5. UVa 103 Stacking Boxes --- DAG上的动态规划

    UVa 103 题目大意:给定n个箱子,每个箱子有m个维度, 一个箱子可以嵌套在另一个箱子中当且仅当该箱子的所有的维度大小全部小于另一个箱子的相应维度, (注意箱子可以旋转,即箱子维度可以互换),求最 ...

  6. 『Pushing Boxes 双重bfs』

    Pushing Boxes Description Imagine you are standing inside a two-dimensional maze composed of square ...

  7. POJ1475 Pushing Boxes(双搜索)

    POJ1475 Pushing Boxes  推箱子,#表示墙,B表示箱子的起点,T表示箱子的目标位置,S表示人的起点 本题没有 Special Judge,多解时,先最小化箱子被推动的次数,再最小化 ...

  8. 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 ...

  9. POJ 1475 Pushing Boxes 搜索- 两重BFS

    题目地址: http://poj.org/problem?id=1475 两重BFS就行了,第一重是搜索箱子,第二重搜索人能不能到达推箱子的地方. AC代码: #include <iostrea ...

随机推荐

  1. 最新版Intel HD4000 桌面右键菜单去除方法

    网上找了一圈都提示找不到指定模块,后来发现它换dll了 regsvr32 /u igfxDTCM.dll

  2. uva 1428

    https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  3. model.addAttribute("student",student)——渲染

    往前台视图传参数,(将在处理器传入的模型数据在视图jsp中展示出来,即为渲染) 按照spring一般的编码习惯,model 应该是contrller里面的Map结构吧.Map里面添加key=" ...

  4. 第一次使用Git心得体会

    用书本上的概念讲,Git是一个分布式的版本控制工具,每一个Git的工作目录都是一个完全独立的代码库,并拥有完整的历史记录和版本追踪能力,能够不依赖于网络和中心服务器.也就是说Git能够不需要服务器而在 ...

  5. 开发基于Handoff的App(Swift)

            iOS8推出一个新特性,叫做Handoff.Handoff中文含义为换手(把接力棒传给下一个人),可以在一台Mac和iOS设备上开始工作,中途将工作交换到另一个Mac或iOS设备中进行 ...

  6. Repeater控件的分页效果

    <webdiyer:AspNetPager ID="AspNetPager1" runat="server" HorizontalAlign=" ...

  7. iOS:图片拉伸不变形技巧

    方法: 假设图片为60*24 CGFloat top = image.height*0.5-1; // 顶端盖高度 CGFloat bottom = top ; // 底端盖高度 CGFloat le ...

  8. CSS网页布局:网站整体居中 转

    body{    margin:0 auto;    padding:0 auto; text-align:center;} 只设置body属性对ie浏览器有效果,但是对火狐等不起作用 body{   ...

  9. 从对SAE的一次授权安全评估浅谈云安全

      EMail: jianxin#80sec.comSite: http://www.80sec.comDate: 2011-12-20From: http://www.80sec.com/ [ 目录 ...

  10. hdu 2029

    PS:  逻辑问题... 代码: #include "stdio.h"#include "string.h"int main(){ char a[110]; i ...