POJ1475(Pushing Boxes)--bbffss

题目一看完就忙着回忆童年了。推箱子的游戏。

#include <iostream>
#include <cmath>
#include <queue>
#include <cstdio>
#include <string>
#include <algorithm>
#include<functional>
using namespace std; struct Position // 位置类型
{
int r;
int c;
bool operator ==(const Position& rhs) const;
}; bool Position::operator ==(const Position& rhs) const
{
return r == rhs.r && c == rhs.c;
} struct Box // 箱子状态
{
Position currPos; // 箱子的当前位置
Position personPos; // 人的位置(人在箱子相邻的某一侧)
string ans; // 箱子和人沿途走过的路径
}; struct Person // 人的状态
{
Position currPos; // 人的位置
string ans; // 人沿途走过的路径
}; struct PushingBoxes // 解题类
{
protected:
int m; // 地图行数
int n; // 地图列数
char** arr; // arr[i][j]表示第i行第j列是空地还是障碍物
bool** vis; // vis[i][j]表示在搜索人走向箱子的过程中,(i,j)这个位置是否走到过
bool*** mark; // mark[i][j][k]表示人推箱子的过程中,箱子沿着方向k到达位置(i,j),这个状态是否搜索过
static Position dirs[]; // 上下左右方向数组
static char op[]; // 方向对应的字符串,上北下南左西右东
void GetMemory(); // 动态分配内存
void ReleaseMemory(); // 动态释放内存
bool InFreeSpace(const Position& pos) const; // 判断参数所表示的位置是否是可以到达的无障碍的位置
bool BfsFromPersonToPos(const Position& personPos, const Position& boxPos, const Position& pos, string& path); // 搜索人朝位置pos移动的过程
bool BfsFromBoxToTarget(const Position& personPos, const Position& boxPos, const Position& target, string& path); // 搜索箱子朝目标位置移动的过程
public:
void Solve();
}; Position PushingBoxes::dirs[] = { -,,,,,-,, };//注意题目要求的是n、s、w、e的顺序
char PushingBoxes::op[] = { 'n','s','w','e' };//注意题目要求的是n、s、w、e的顺序 void PushingBoxes::GetMemory()
{
arr = new char*[m];
for (int i = ; i < m; i++)
{
arr[i] = new char[n];
}
vis = new bool*[m];
for (int i = ; i < m; i++)
{
vis[i] = new bool[n];
}
mark = new bool**[m];
for (int i = ; i < m; i++)
{
mark[i] = new bool*[n];
for (int j = ; j < n; j++)
{
mark[i][j] = new bool[];
}
}
} void PushingBoxes::ReleaseMemory()
{
for (int i = ; i < m; i++)
{
for (int j = ; j < n; j++)
{
delete[] mark[i][j];
}
delete[] mark[i];
}
delete[] mark; for (int i = ; i < m; i++)
{
delete[] vis[i];
}
delete[] vis; for (int i = ; i < m; i++)
{
delete[] arr[i];
}
delete[] arr;
} bool PushingBoxes::InFreeSpace(const Position& pos) const
{
if (pos.r >= && pos.r < m && pos.c >= && pos.c < n)
{
if (arr[pos.r][pos.c] != '#')
{
return true;
}
}
return false;
} // 搜索人朝位置pos移动的过程
bool PushingBoxes::BfsFromPersonToPos(const Position& personPos, const Position& boxPos, const Position& pos, string& path)
{
path = "";
if (!InFreeSpace(personPos) || !InFreeSpace(pos))
{
return false;
}
queue<Person> q;
Person currNode;
currNode.currPos = personPos;
currNode.ans = "";
for (int i = ; i < m; i++)
{
fill(vis[i], vis[i] + n, false);
}
vis[boxPos.r][boxPos.c] = true; // 人不能和箱子重合,避免人走到箱子的位置
vis[currNode.currPos.r][currNode.currPos.c] = true;
q.push(currNode);
while (!q.empty())
{
currNode = q.front();
q.pop();
if (currNode.currPos == pos)
{
path = currNode.ans;
return true;
}
for (int i = ; i < ; i++)
{
Person nextNode;
nextNode.currPos.r = currNode.currPos.r + dirs[i].r;
nextNode.currPos.c = currNode.currPos.c + dirs[i].c;
nextNode.ans = currNode.ans + op[i];
if (InFreeSpace(nextNode.currPos) && !vis[nextNode.currPos.r][nextNode.currPos.c])
{
vis[nextNode.currPos.r][nextNode.currPos.c] = true;
q.push(nextNode);
}
}
}
return false;
} // 搜索箱子朝目标位置移动的过程
bool PushingBoxes::BfsFromBoxToTarget(const Position& personPos, const Position& boxPos, const Position& target, string& path)
{
path = "";
if (!InFreeSpace(personPos) || !InFreeSpace(target))
{
return false;
}
for (int i = ; i < m; i++)
{
for (int j = ; j < n; j++)
{
fill(mark[i][j], mark[i][j] + , false);
}
}
Box currNode;
currNode.currPos = boxPos;
currNode.personPos = personPos;
currNode.ans = "";
queue<Box> q;
q.push(currNode);
while (!q.empty())
{
currNode = q.front();
q.pop();
if (currNode.currPos == target)
{
path = currNode.ans;
return true;
}
for (int i = ; i < ; i++) //盒子周围的四个方向
{
Box nextNode; // 计算下一个新位置
nextNode.currPos.r = currNode.currPos.r + dirs[i].r;
nextNode.currPos.c = currNode.currPos.c + dirs[i].c;
if (InFreeSpace(nextNode.currPos) && !mark[nextNode.currPos.r][nextNode.currPos.c][i])
{
Position pos; // 人应该在反方向的位置
pos.r = currNode.currPos.r - dirs[i].r;
pos.c = currNode.currPos.c - dirs[i].c;
if (BfsFromPersonToPos(currNode.personPos, currNode.currPos, pos, path)) // 如果人能到达反方向的位置
{
nextNode.ans = currNode.ans + path + (char)(op[i] - 'a' + 'A');
nextNode.personPos = currNode.currPos;
mark[nextNode.currPos.r][nextNode.currPos.c][i] = true;
q.push(nextNode);
}
}
}
}
return false;
} void PushingBoxes::Solve()
{
int k = ;
scanf("%d %d", &m, &n);
while (m > && n > )
{
GetMemory();
Box box;
Person person;
Position target;
for (int i = ; i < m; i++)
{
for (int j = ; j < n; j++)
{
scanf(" %c", &arr[i][j]);
if (arr[i][j] == 'S')
{
person.currPos.r = i;
person.currPos.c = j;
}
if (arr[i][j] == 'T')
{
target.r = i;
target.c = j;
}
if (arr[i][j] == 'B')
{
box.currPos.r = i;
box.currPos.c = j;
}
}
}
string path;
bool flag = BfsFromBoxToTarget(person.currPos, box.currPos, target, path);
printf("Maze #%d\n", ++k);
if (flag)
{
printf("%s\n\n", path.c_str());
}
else
{
printf("Impossible.\n\n");
}
ReleaseMemory();
scanf("%d %d", &m, &n);
}
} int main()
{
PushingBoxes obj;
obj.Solve();
return ;
}
POJ1475(Pushing Boxes)--bbffss的更多相关文章
- POJ1475 Pushing Boxes(双搜索)
POJ1475 Pushing Boxes 推箱子,#表示墙,B表示箱子的起点,T表示箱子的目标位置,S表示人的起点 本题没有 Special Judge,多解时,先最小化箱子被推动的次数,再最小化 ...
- POJ-1475 Pushing Boxes (BFS+优先队列)
Description Imagine you are standing inside a two-dimensional maze composed of square cells which ma ...
- poj1475 Pushing Boxes(BFS)
题目链接 http://poj.org/problem?id=1475 题意 推箱子游戏.输入迷宫.箱子的位置.人的位置.目标位置,求人是否能把箱子推到目标位置,若能则输出推的最少的路径,如果有多条步 ...
- POJ1475 Pushing Boxes(BFS套BFS)
描述 Imagine you are standing inside a two-dimensional maze composed of square cells which may or may ...
- poj1475 -- Pushing Boxes
这道题其实挺有趣 的,这让我想起小时候诺基亚手机上的推箱子游戏(虽然一点也不好玩) (英文不好-->) 题意翻译: 初始人(S),箱子(B),目的地(T)用人把箱子推到 T最小步数及其路径(满 ...
- POJ1475 Pushing Boxes 华丽丽的双重BFS
woc累死了写了两个半小时...就是BFS?我太菜了... 刚开始以为让人预先跑一遍BFS,然后一会儿取两节加起来就好了,结果发现求出来的最短路(就是这个意思)会因箱子的移动而变化....我死了QWQ ...
- poj1475 Pushing Boxes[双重BFS(毒瘤搜索题)]
地址. 很重要的搜索题.★★★ 吐槽:算是写过的一道码量比较大的搜索题了,细节多,还比较毒瘤.虽然是一遍AC的,其实我提前偷了大数据,但是思路还是想了好长时间,照理说想了半小时出不来,我就会翻题解,但 ...
- Pushing Boxes(广度优先搜索)
题目传送门 首先说明我这个代码和lyd的有点不同:可能更加复杂 既然要求以箱子步数为第一关键字,人的步数为第二关键字,那么我们可以想先找到箱子的最短路径.但单单找到箱子的最短路肯定不行啊,因为有时候不 ...
- poj 1475 || zoj 249 Pushing Boxes
http://poj.org/problem?id=1475 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=249 Pushin ...
随机推荐
- PHP7 serialize_precision 配置不当导致 json_encode() 浮点小数溢出错误
https://blog.csdn.net/moliyiran/article/details/81179825 感谢 @地狱星星:原因已找到, 该现象只出现在PHP 7.1+版本上建议使用默认值 s ...
- hive分区表新增字段,已有分区显示为null
如果在hive的分区表新增非分区字段,那么原有的分区的数据即使重新运行也会显示为null. 必须先删除该分区,再重新跑数据.
- ubuntu18 更换屏幕分辨率
ubuntu18.04怎么修改屏幕分辨率 最近在自己的电脑中安装了ubuntu18.04系统,默认分辨率不对所以只好自己修改分辨率,但是在桌面右键并没找到设置分辨率的选项,那么我们应该在哪里设置分辨率 ...
- python脚本使用源码安装不同版本的python
# coding=utf-8 import os import sys # 判断是否是root用户 if os.getuid() == 0: pass else: print('当前用户不是root用 ...
- shell编程系列12--文本处理三剑客之sed利用sed修改文件内容
shell编程系列12--文本处理三剑客之sed利用sed修改文件内容 修改命令对照表 编辑命令 1s/old/new/ 替换第1行内容old为new ,10s/old/new/ 替换第1行到10行的 ...
- 泡泡一分钟:Topomap: Topological Mapping and Navigation Based on Visual SLAM Maps
Topomap: Topological Mapping and Navigation Based on Visual SLAM Maps Fabian Bl¨ochliger, Marius Feh ...
- 1264 - Out of range value for column
现象:新建数据库,字段类型是tinyint,然后插入数据,数值为128,报标题错误 原因:如果在新建数据库的时候没有指定为unsigned,那么就是有符号的,所以tinyint的范围是-128~127 ...
- Python - Django - 模板语言之 Filters(过滤器)
通过管道符 "|" 来使用过滤器,{{ value|过滤器:参数 }} Django 的模板语言中提供了六十个左右的内置过滤器 urls.py: from django.conf. ...
- FastDFS配置详解之Storage配置
1 基本配置disabled #func:该配置文件是否生效#valu:## true:无效## false:生效disabled=false group_name#func:本storage ser ...
- 10点睛Spring4.1-Application Event
10.1 Application Event Spring使用Application Event给bean之间的消息通讯提供了手段 应按照如下部分实现bean之间的消息通讯 继承Application ...