题目大意

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

题目分析

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

每一次对箱子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. 可伸缩的textview。

    在一些应用中,比如腾讯的应用市场APP应用宝,关于某款应用的介绍文字,如果介绍文字过长,那么不是全部展现出来,而是显示三四行的开始部分(摘要),预知全部的内容,用户点击展开按钮即可查阅全部内容. 这样 ...

  2. webApi跨域

    <system.webServer> <httpProtocol> <customHeaders> <add name="Access-Contro ...

  3. UVa 11762 - Race to 1

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

  4. POJ 2104 静态找区间第k大

    静态区间第k大的问题,往往可以利用主席树来解决 这是主席树的第一道题 主席树大概可以理解为在n个节点上都建立一棵线段树,但是想想会超出内存 每一个节点保存的线段树都记录当前整段前缀区间的信息 但是因为 ...

  5. 多线程同步内功心法——PV操作上(未完待续。。。)

    阅读本篇之前推荐阅读以下姊妹篇: <秒杀多线程第四篇一个经典的多线程同步问题> <秒杀多线程第五篇经典线程同步关键段CS> <秒杀多线程第六篇经典线程同步事件Event& ...

  6. APP store 官方统计工具的常见的Q&A

    Apple最近在iTunesConnect里最新发布了官方统计工具,提供了现有友盟统计平台和自有统计平台无法统计的数据,具有自己的独有特点,尤其是下面几个最让人头疼的流量分析转化,可以在App Ana ...

  7. IOS开发中--点击imageView上的Button没有任何反应

    点击imageView上的Button没有任何反应:    解决方法:设置图片的userInteractionEnabled为YES,使该imageView可以与用户进行交互

  8. 毕向东Java基础:day09_3-4

    day09-03 1.匿名内部类的使用场景. Body{  //身体 包括心脏 private  class XinZhang{  // 心脏 可以body的成员变量.如果不想被别人访问,加上priv ...

  9. 简单的php Mysql类(查询 删除 更新)

    php Mysql类一般都包括了几乎我们常用的数据库操作方法,这里只提供了查询 删除 更新三种操作,算不是很全只是一个简单的数据库查询类了.      代码如下 复制代码 class mysql { ...

  10. form表单select联动

    下拉列表:二级联动菜单 Select对象的常用属性 options[]:返回所有option组成的一个数组: name:名称 value:option的value的值 length:设置或读取opti ...