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 ...
随机推荐
- 利用LM神经网络和决策树去分类
# -*- coding: utf-8 -*- import pandas as pd from scipy.interpolate import lagrange from matplotlib i ...
- java之trycatchfinally代码块与return,throw的执行顺序的探索
时光荏苒,转眼间毕业都半年了,java编程也五个月了.写代码的过程中,会经常遇到解决代码抛异常的情况.平时只注重完成功能,也没太注意try_catch_finally的内在执行顺序,只知道表面的现象: ...
- Windows API调用外部程序
要在应用程序中启动其他的应用程序,有3个函数可以使用,下面我一一说说他们(我以打开D:\Program Files\zeecalls\目录下的zeecalls.exe应用程序为例): 1.Winexe ...
- 再谈IT行业工程师文化
为什么是再呢?因为“工程师文化”这种说法网上很多,各种理解,各种版 本,我只是简单说说我的认识,说的不对的地方敬请各位大牛,高手,高高手多多指教,我本身也是个技术人员,不过只是技术不怎么样而已.写这个 ...
- bzoj 1911: [Apio2010]特别行动队
#include<cstdio> #include<iostream> #define M 1000009 #define ll long long using namespa ...
- NOIP 2003解题报告
第一题(神经网络): 题目大意,给出一些点,每个点都有2个值,c和u,给出一些有向边,权值为w.入度为0的点的c已知,其它点的c未知,每个入度不为0的点node的c等于sum(c[k]*w[k][no ...
- NOIP 2013 提高组 day2 积木大赛
积木大赛 描述 春春幼儿园举办了一年一度的“积木大赛”.今年比赛的内容是搭建一座宽度为 n 的大厦,大厦可以看成由 n 块宽度为1的积木组成,第
- C++-Effective C++ Items
Item2:尽量以const,enum,inline替换#define 原因:1, #define ASPECT_RATIO 1.63 编译错误时产生魔数,应以const double Aspect_ ...
- K2任命新的亚太区高级副总裁
K2, 一个屡获殊荣的企业应用软件公司宣布,任命陈光明(Tan Kwang Meng, KM)为亚太区高级副总裁.这次任命是对公司持续发展的肯定,同时也是对将亚太区作为全球扩张战略的关键市场的承诺. ...
- 如何区别PeekMessage&GetMessage SendMessage&PostMessage
转自http://blog.csdn.net/young0325/article/details/6430664 Peekmessage和Getmessage都是向系统的消息队列中取得消息,不过性质不 ...