【题目链接】

http://poj.org/problem?id=1475

【算法】

双重BFS

【代码】

#include <algorithm>
#include <bitset>
#include <cctype>
#include <cerrno>
#include <clocale>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <limits>
#include <list>
#include <map>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <utility>
#include <vector>
#include <cwchar>
#include <cwctype>
#include <stack>
#include <limits.h>
using namespace std;
#define MAXN 30 int n,m,i,j,px,py,bx,by,ex,ey,TC;
string t;
char mp[MAXN][MAXN];
bool visited1[MAXN][MAXN],visited2[MAXN][MAXN]; const int dx[] = {,,-,};
const int dy[] = {-,,,};
const char box_path[] = "WENS";
const char people_path[] = "wens"; struct Box
{
int bx,by,px,py;
string path;
};
struct People
{
int x,y;
string path;
};
inline bool ok(int x,int y)
{
return x >= && x <= n && y >= && y <= m;
}
inline bool bfs2(int sx,int sy,int ex,int ey,int nx,int ny)
{
int i,tx,ty;
People cur;
queue< People > q;
while (!q.empty()) q.pop();
memset(visited2,false,sizeof(visited2));
q.push((People){sx,sy,""});
while (!q.empty())
{
cur = q.front();
q.pop();
if (cur.x == ex && cur.y == ey)
{
t = cur.path;
return true;
}
for (i = ; i < ; i++)
{
tx = cur.x + dx[i];
ty = cur.y + dy[i];
if (ok(tx,ty) && !visited2[tx][ty] && mp[tx][ty] != '#')
{
if (tx == nx && ty == ny) continue;
visited2[tx][ty] = true;
q.push((People){tx,ty,cur.path+people_path[i]});
}
}
}
return false;
}
inline void bfs1()
{
int i,tx,ty;
queue<Box> q;
Box cur;
memset(visited1,false,sizeof(visited1));
while (!q.empty()) q.pop();
q.push((Box){bx,by,px,py,""});
while (!q.empty())
{
cur = q.front();
q.pop();
if (cur.bx == ex && cur.by == ey)
{
cout<< cur.path << endl;
return;
}
for (i = ; i < ; i++)
{
tx = cur.bx + dx[i];
ty = cur.by + dy[i];
if (ok(tx,ty) && !visited1[tx][ty] && mp[tx][ty] != '#')
{
if (bfs2(cur.px,cur.py,cur.bx-dx[i],cur.by-dy[i],cur.bx,cur.by))
{
visited1[tx][ty] = true;
q.push((Box){tx,ty,cur.bx,cur.by,cur.path+t+box_path[i]});
}
}
}
}
printf("Impossible.\n");
} int main()
{ while (scanf("%d%d",&n,&m) != EOF && n && m)
{
getchar();
for (i = ; i <= n; i++)
{
for (j = ; j <= m; j++)
{
mp[i][j] = getchar();
if (mp[i][j] == 'S')
{
px = i;
py = j;
}
if (mp[i][j] == 'B')
{
bx = i;
by = j;
}
if (mp[i][j] == 'T')
{
ex = i;
ey = j;
}
}
getchar();
}
printf("Maze #%d\n",++TC);
bfs1();
printf("\n");
} return ; }

【POJ 1475】 Pushing Boxes的更多相关文章

  1. bzoj 2295: 【POJ Challenge】我爱你啊

    2295: [POJ Challenge]我爱你啊 Time Limit: 1 Sec  Memory Limit: 128 MB Description ftiasch是个十分受女生欢迎的同学,所以 ...

  2. 【链表】BZOJ 2288: 【POJ Challenge】生日礼物

    2288: [POJ Challenge]生日礼物 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 382  Solved: 111[Submit][S ...

  3. BZOJ2288: 【POJ Challenge】生日礼物

    2288: [POJ Challenge]生日礼物 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 284  Solved: 82[Submit][St ...

  4. BZOJ2293: 【POJ Challenge】吉他英雄

    2293: [POJ Challenge]吉他英雄 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 80  Solved: 59[Submit][Stat ...

  5. BZOJ2287: 【POJ Challenge】消失之物

    2287: [POJ Challenge]消失之物 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 254  Solved: 140[Submit][S ...

  6. BZOJ2295: 【POJ Challenge】我爱你啊

    2295: [POJ Challenge]我爱你啊 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 126  Solved: 90[Submit][Sta ...

  7. BZOJ2296: 【POJ Challenge】随机种子

    2296: [POJ Challenge]随机种子 Time Limit: 1 Sec  Memory Limit: 128 MBSec  Special JudgeSubmit: 114  Solv ...

  8. BZOJ2292: 【POJ Challenge 】永远挑战

    2292: [POJ Challenge ]永远挑战 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 513  Solved: 201[Submit][ ...

  9. 【POJ 1125】Stockbroker Grapevine

    id=1125">[POJ 1125]Stockbroker Grapevine 最短路 只是这题数据非常水. . 主要想大牛们试试南阳OJ同题 链接例如以下: http://acm. ...

随机推荐

  1. 移动web——touch事件介绍

    基本概念 1.在移动web端点击事件或者滑动屏幕.捏合等动作都是由touchstar.touchmove.touchend这三个事件组合在一起使用的 2.click事件在移动端会有0.2秒的延迟,下面 ...

  2. pengyue-form 模块 dropdown 关系联动

    <script> window.onload=function() { var school= document.getElementById("dnn_ctr5973_View ...

  3. vim之补全1(完全个人定制版)

    关于vim的补全最初的感觉是蛋疼, 真正的蛋疼! 由于在接触linux之前曾经在windows下面学过一段时间软件开发, 那时使用的是vs2010, 现在看来虽然vs启动相当的慢, 编辑器的定制和配置 ...

  4. 解决Mysql Workbench的Error Code: 1175错误

    错误: Error Code: 1175. You are using safe update mode and you tried to update a table without a WHERE ...

  5. .net core发布程序

    这里说的是,和.net core运行库一起发布,所以,目标运行系统,可以不安装.net core也能运行 1.project.json 把dependencies里面的type删除掉,后面加入&quo ...

  6. Python 函数 day3

    函数是组织好的,可重复使用的,用来实现单一,或相关联功能的代码段. 函数能提高应用的模块性,和代码的重复利用率.你已经知道Python提供了许多内建函数,比如print().但你也可以自己创建函数,这 ...

  7. dp有哪些种类

    dp有哪些种类 一.总结 一句话总结: 二.dp动态规划分类详解 动态规划一直是ACM竞赛中的重点,同时又是难点,因为该算法时间效率高,代码量少,多元性强,主要考察思维能力.建模抽象能力.灵活度. * ...

  8. 面试题:你能写一个Vue的双向数据绑定吗?

    在目前的前端面试中,vue的双向数据绑定已经成为了一个非常容易考到的点,即使不能当场写出来,至少也要能说出原理.本篇文章中我将会仿照vue写一个双向数据绑定的实例,名字就叫myVue吧.结合注释,希望 ...

  9. shell 读取目录指定文件并截取拼接

    shell脚本读取指定文件并拼接成指定的版本信息

  10. 《hello-world》第八次团队作业:Alpha冲刺-Scrum Meeting 2

    项目 内容 这个作业属于哪个课程 2016级计算机科学与工程学院软件工程(西北师范大学) 这个作业的要求在哪里 实验十二 团队作业8:软件测试与Alpha冲刺 团队名称 <hello--worl ...