【题目链接】

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. windows server2003 多用户登陆问题解决办法

    windows server2003 多用户登陆问题解决办法 Windows Server远程登陆默认情况下只允许同时有两个用户登陆,超过两个用户会提示"超出最大连接数". 要解决 ...

  2. which

    功能说明:显示命令的全路径.   参数选项: -a  默认在PATH路径中由前往后查找命令,如果查找到了,就停止匹配.使用-a选项将遍历所有PATH路径,输出所有匹配项.   参数-a把所有匹配命令路 ...

  3. day10-函数基础知识

    函数 什么是函数 把工具事先准备好,然后下次使用的时候,直接使用就行了.我们的函数就是工具 为何用函数 1.遇到重复的功能只能重复编写实现代码,代码冗余 2.功能需要扩展时,需要找出所有实现该功能的地 ...

  4. Vue中this.$router.push参数获取(通过路由传参)【路由跳转的方法】

    传递参数的方法: 1.Params 由于动态路由也是传递params的,所以在 this.$router.push() 方法中 path不能和params一起使用,否则params将无效.需要用nam ...

  5. 用C#在Visual Studio写Javascript单元测试

    1.在vs创建一个标准的单元测试工程 2.引用nuget包:Edge.js 我是用的是6.11.2版本 3.编写JsRunner类 using EdgeJs; using System; using ...

  6. Swift 3到5.1新特性整理

    本文转载自:https://hicc.me/whats-new-in-swift-3-to-5-1/,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有. Hipo 2.0 重写从 Swif ...

  7. Error from server at http://127.0.0.1:8983/solr/xxx: undefined field type

    undefined field type就是说没有定义type类型,这样情况下,可以新建一个带type的索引,比如:{type:1, id:1, name:"张三"}

  8. android studio 创建第一个app之hello world

    android studio 创建第一个app之hello world 想要用studio创建一个简单的app,结果遇到各种问题,application就是允许不起来,后来在专业人的帮助下,删除了一些 ...

  9. Autofac 控制反转

    class Program { static void Main(string[] args) { IContainer container = Init(); Go(container); Cons ...

  10. [SQL Server] 常用sql脚本

    1.添加表 GO IF NOT EXISTS(SELECT * FROM sys.tables WHERE name='table_name') BEGIN CREATE TABLE [dbo].[t ...