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 ...
随机推荐
- osg include lib -computer1
E:\\OpenSceneGraph\\OpenSceneGraph_3_6_install\\lib E:\\OpenSceneGraph\\3rdParty\\v140-x64\\lib Open ...
- 内存检测工具valgrind的安装和简单使用
1. 安装 .tar.bz2 cd valgrind- sudo ./configure sudo make sudo make install 2. 简单使用 #include <stdio. ...
- MyBatis的学习总结:调用存储过程【参考】
一.创建存储过程 存储过程的目的:统计edi_test_task 正在运行的任务和非运行的任务 CREATE DEFINER=`root`@`%` PROCEDURE `edihelper`.`SP_ ...
- QML随机颜色
color=Qt.rgba(Math.random(),Math.random(),Math.random(),1)
- JS获当前网页元素高度offsetHeight
本文测试的是offsetHeight,获取网页中某元素的高度,单位是像素,获取的类型是整型,可以进行数字运算.如图,网页中的元素本身的高度包括,自身的内容+padding+border,而margin ...
- upload-labs 上传漏洞靶场环境以及writeup
一个帮你总结所有类型的上传漏洞的靶场 https://github.com/c0ny1/upload-labs 靶场环境(基于phpstudy这个php集成环境) https://github.com ...
- 记录一下vue slot
使用步骤: 1.在组件(panda)当中建立一个slot,定义好name.(<slot name='myname'></slot>) 2.引用组件的时候如果需要补充内容, ...
- vue中使用animate.css实现动画
参考链接:https://www.cnblogs.com/ccyinghua/p/7872694.html 参考链接:https://www.jianshu.com/p/2e0b2f8d40cf 使用 ...
- 学习记录:《C++设计模式——李建忠主讲》7.“领域规则”模式
领域规则模式:在特定领域中,某些变化虽然频繁,但可以抽象为某种规则.这时候,结合特定的领域,将问题抽象为语法规则,从而给出该领域下的一般性解决方案. 典型模式:解释器模式(Interpreter). ...
- Hive 企业调优
9.企业级调优 9.1 Fetch 抓取 Fetch 抓取:Hive 中对某些情况的查询可以不必使用 MapReduce 计算: hive.fetch.task.conversion:more 9.2 ...