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 ...
随机推荐
- 【转】项目搬迁,快捷导出环境依赖包到requirements.txt
项目搬迁的时候,需要把当前的环境依赖包导出,然后到部署项目的服务器上安装依赖. 我们可以通过下面的命令执行,把依赖包导出到requirements.txt文件里. 生成requirements.txt ...
- QML随机颜色
color=Qt.rgba(Math.random(),Math.random(),Math.random(),1)
- Python 初级 5 判断再判断
复习: 1 三种数据类型: 整数:int, (1, 2, 200) 浮点数: float(2.0, 9.5, 100.38) 字符串: str("小明", "abc&qu ...
- php 3.2 生成压缩文件,并下载
public function zip_download() { $array = array( 'http://local.qki.com/site_upload/erweima/20190826/ ...
- Connection reset by peer引发的思考
http://www.mamicode.com/info-detail-506381.html
- 【Leetcode_easy】933. Number of Recent Calls
problem 933. Number of Recent Calls 参考 1. Leetcode_easy_933. Number of Recent Calls; 完
- Pyhon时间参数的应用
Python获取 本周,上周,本月,上月,本季,上季,今年, 去年 # -*- coding: utf-8 -*-# @time: 2019-05-13 17:30 import datetime f ...
- skynet sproto 问题
刚碰到一个小细节,纠结了半个小时 sproto的协议,request 和{ 必须有空格
- Spark 基础操作
1. Spark 基础 2. Spark Core 3. Spark SQL 4. Spark Streaming 5. Spark 内核机制 6. Spark 性能调优 1. Spark 基础 1. ...
- HTTP_HOST , SERVER_NAME 区别
当端口是80的时候,他们的内容是一样的. 但是当端口不是80的时候,就不一样了. # HTTP_HOST = SERVER_NAME:SERVER_PORT /** * 获取当前的host */ pu ...