Saving Tang Monk II HihoCoder - 1828 2018北京赛站网络赛A题
《Journey to the West》(also 《Monkey》) is one of the Four Great Classical Novels of Chinese literature. It was written by Wu Cheng'en during the Ming Dynasty. In this novel, Monkey King Sun Wukong, pig Zhu Bajie and Sha Wujing, escorted Tang Monk to India to get sacred Buddhism texts.
During the journey, Tang Monk was often captured by demons. Most of demons wanted to eat Tang Monk to achieve immortality, but some female demons just wanted to marry him because he was handsome. So, fighting demons and saving Monk Tang is the major job for Sun Wukong to do.
Once, Tang Monk was captured by the demon White Bones. White Bones lived in a palace and she cuffed Tang Monk in a room. Sun Wukong managed to get into the palace, and he wanted to reach Tang Monk and rescue him.
The palace can be described as a matrix of characters. Different characters stand for different rooms as below:
'S' : The original position of Sun Wukong
'T' : The location of Tang Monk
'.' : An empty room
'#' : A deadly gas room.
'B' : A room with unlimited number of oxygen bottles. Every time Sun Wukong entered a 'B' room from other rooms, he would get an oxygen bottle. But staying there would not get Sun Wukong more oxygen bottles. Sun Wukong could carry at most 5 oxygen bottles at the same time.
'P' : A room with unlimited number of speed-up pills. Every time Sun Wukong entered a 'P' room from other rooms, he would get a speed-up pill. But staying there would not get Sun Wukong more speed-up pills. Sun Wukong could bring unlimited number of speed-up pills with him.
Sun Wukong could move in the palace. For each move, Sun Wukong might go to the adjacent rooms in 4 directions(north, west,south and east). But Sun Wukong couldn't get into a '#' room(deadly gas room) without an oxygen bottle. Entering a '#' room each time would cost Sun Wukong one oxygen bottle.
Each move took Sun Wukong one minute. But if Sun Wukong ate a speed-up pill, he could make next move without spending any time. In other words, each speed-up pill could save Sun Wukong one minute. And if Sun Wukong went into a '#' room, he had to stay there for one extra minute to recover his health.
Since Sun Wukong was an impatient monkey, he wanted to save Tang Monk as soon as possible. Please figure out the minimum time Sun Wukong needed to reach Tang Monk.
Input
There are no more than 25 test cases.
For each case, the first line includes two integers N and M(0 < N,M ≤ 100), meaning that the palace is a N × M matrix.
Then the N×M matrix follows.
The input ends with N = 0 and M = 0.
Output
For each test case, print the minimum time (in minute) Sun Wukong needed to save Tang Monk. If it's impossible for Sun Wukong to complete the mission, print -1
Sample Input
2 2
S#
#T
2 5
SB###
##P#T
4 7
SP.....
P#.....
......#
B...##T
0 0
Sample Output
-1
8
11 题意:
给你一个二维字符矩阵,S字符代表孙悟空的位置,T字符代表唐生的位置。
现在是孙悟空要用最小的距离去到唐生的位置,
路途中有以下几种位置
'#' 是毒区,要有一一个氧气才可以进去,并且需要额外消耗一个时间来恢复身体。
'B' 是氧气区,进来会拿到一个氧气,每一个氧气区可以无限拿,但是孙悟空身上最多可以拿5个氧气管。
’P‘ 是能量区,进来会获得一个能力包,一个能量包可以让你行动一次不消耗时间。
’.‘ 空区域,任意行走。
思路:
开一个思维的dis数组来记录以下状态
dis[x][y][o][p] 表示 在x,y位置,有o个氧气,p个能力包时的最小用时。
然后用BFS正常搜即可,细节处理下即可。 提示:
每一个能量包能用就用的话可以让代码写起来更简单。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <iomanip>
#define ALL(x) (x).begin(), (x).end()
#define rt return
#define dll(x) scanf("%I64d",&x)
#define xll(x) printf("%I64d\n",x)
#define sz(a) int(a.size())
#define all(a) a.begin(), a.end()
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '\0', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eps 1e-6
#define gg(x) getInt(&x)
#define db(x) cout<<"== [ "<<x<<" ] =="<<endl;
using namespace std;
typedef long long ll;
ll gcd(ll a, ll b) {return b ? gcd(b, a % b) : a;}
ll lcm(ll a, ll b) {return a / gcd(a, b) * b;}
ll powmod(ll a, ll b, ll MOD) {ll ans = ; while (b) {if (b % )ans = ans * a % MOD; a = a * a % MOD; b /= ;} return ans;}
inline void getInt(int* p);
const int maxn = ;
const int inf = 0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/
int n, m;
char s[][];
struct node
{
int x, y, step, o, p;
node() {}
node(int xx, int yy, int oo, int pp, int ss)
{
x = xx;
y = yy;
o = oo;
p = pp;
step = ss;
}
};
int xx[] = { -, , , };
int yy[] = {, , -, };
int dis[][][][];
int main()
{
//freopen("D:\\common_text\\code_stream\\in.txt","r",stdin);
//freopen("D:\\common_text\\code_stream\\out.txt","w",stdout);
while (~scanf("%d %d", &n, &m))
{
if (n + m == )
{
break;
}
repd(i, , n)
{
scanf("%s", s[i] + );
}
int sx, sy;
repd(i, , n)
{
repd(j, , m)
{
if (s[i][j] == 'S')
{
sx = i;
sy = j;
break;
}
}
}
queue<node> q;
q.push(node(sx, sy, , , ));
memset(dis, inf, sizeof(dis));
// cout<<dis[0][2][2][1]<<endl;
int ans = inf;
while (!q.empty())
{
node temp = q.front();
q.pop();
repd(i, , )
{
int tx = temp.x + xx[i];
int ty = temp.y + yy[i];
if (tx >= && tx <= n && ty >= && ty <= m)
{
if (s[tx][ty] == 'T')
{
if (temp.p > )
{
dis[tx][ty][temp.o][temp.p] = min(dis[tx][ty][temp.o][temp.p], temp.step);
ans = min(dis[tx][ty][temp.o][temp.p], ans);
} else
{
dis[tx][ty][temp.o][temp.p] = min(dis[tx][ty][temp.o][temp.p], temp.step + );
ans = min(dis[tx][ty][temp.o][temp.p], ans);
}
} else if (s[tx][ty] == 'B')
{
// get oxy
if (temp.o >= )
{
if (temp.p > )
{
if (dis[tx][ty][temp.o][temp.p] > temp.step)
{
dis[tx][ty][temp.o][temp.p] = temp.step;
q.push(node(tx, ty, temp.o, temp.p - , temp.step));
}
} else
{
if (dis[tx][ty][temp.o][temp.p] > temp.step + )
{
dis[tx][ty][temp.o][temp.p] = temp.step + ;
q.push(node(tx, ty, temp.o, temp.p, temp.step + ));
}
}
} else
{
if (temp.p > )
{
if (dis[tx][ty][temp.o][temp.p] > temp.step)
{
dis[tx][ty][temp.o][temp.p] = temp.step;
q.push(node(tx, ty, temp.o + , temp.p - , temp.step));
}
} else
{
if (dis[tx][ty][temp.o][temp.p] > temp.step + )
{
dis[tx][ty][temp.o][temp.p] = temp.step + ;
q.push(node(tx, ty, temp.o + , temp.p, temp.step + ));
}
}
}
} else if (s[tx][ty] == 'P')
{
// get p
if (temp.p > )
{
if (dis[tx][ty][temp.o][temp.p] > temp.step)
{
dis[tx][ty][temp.o][temp.p] = temp.step;
q.push(node(tx, ty, temp.o, temp.p, temp.step));
}
} else
{
if (dis[tx][ty][temp.o][temp.p] > temp.step + )
{
dis[tx][ty][temp.o][temp.p] = temp.step + ;
q.push(node(tx, ty, temp.o, temp.p + , temp.step + ));
}
}
} else if (s[tx][ty] == '#')
{
// du qu
if (temp.o > )
{
if (temp.p > )
{
if (dis[tx][ty][temp.o][temp.p] > temp.step + )
{
dis[tx][ty][temp.o][temp.p] = temp.step + ;
q.push(node(tx, ty, temp.o - , temp.p - , temp.step + ));
}
} else
{
if (dis[tx][ty][temp.o][temp.p] > temp.step + )
{
dis[tx][ty][temp.o][temp.p] = temp.step + ;
q.push(node(tx, ty, temp.o - , temp.p, temp.step + ));
}
}
}
} else
{
// nomal
if (temp.p > )
{
if (dis[tx][ty][temp.o][temp.p] > temp.step)
{
dis[tx][ty][temp.o][temp.p] = temp.step;
q.push(node(tx, ty, temp.o, temp.p - , temp.step));
}
} else
{
if (dis[tx][ty][temp.o][temp.p] > temp.step + )
{
dis[tx][ty][temp.o][temp.p] = temp.step + ;
q.push(node(tx, ty, temp.o, temp.p, temp.step + ));
}
}
}
}
}
}
if (ans == inf)
{
printf("-1\n");
} else
{
printf("%d\n", ans );
}
} return ;
} inline void getInt(int* p) {
char ch;
do {
ch = getchar();
} while (ch == ' ' || ch == '\n');
if (ch == '-') {
*p = -(getchar() - '');
while ((ch = getchar()) >= '' && ch <= '') {
*p = *p * - ch + '';
}
}
else {
*p = ch - '';
while ((ch = getchar()) >= '' && ch <= '') {
*p = *p * + ch - '';
}
}
}
Saving Tang Monk II HihoCoder - 1828 2018北京赛站网络赛A题的更多相关文章
- ACM/ICPC 2018亚洲区预选赛北京赛站网络赛 A、Saving Tang Monk II 【状态搜索】
任意门:http://hihocoder.com/problemset/problem/1828 Saving Tang Monk II 时间限制:1000ms 单点时限:1000ms 内存限制:25 ...
- ACM-ICPC2018北京网络赛 Saving Tang Monk II(bfs+优先队列)
题目1 : Saving Tang Monk II 时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 <Journey to the West>(also < ...
- Saving Tang Monk II(bfs+优先队列)
Saving Tang Monk II https://hihocoder.com/problemset/problem/1828 时间限制:1000ms 单点时限:1000ms 内存限制:256MB ...
- 北京2018网络赛 hihocoder#1828 : Saving Tang Monk II (BFS + DP +多开一维)
hihocoder 1828 :https://hihocoder.com/problemset/problem/1828 学习参考:https://www.cnblogs.com/tobyw/p/9 ...
- hihocoder #1828 : Saving Tang Monk II(BFS)
描述 <Journey to the West>(also <Monkey>) is one of the Four Great Classical Novels of Chi ...
- hihocoder 1828 Saving Tang Monk II (DP+BFS)
题目链接 Problem Description <Journey to the West>(also <Monkey>) is one of the Four Great C ...
- ACM/ICPC 2018亚洲区预选赛北京赛站网络赛 A.Saving Tang Monk II(优先队列广搜)
#include<bits/stdc++.h> using namespace std; ; ; char G[maxN][maxN]; ]; int n, m, sx, sy, ex, ...
- hihoCoder-1828 2018亚洲区预选赛北京赛站网络赛 A.Saving Tang Monk II BFS
题面 题意:N*M的网格图里,有起点S,终点T,然后有'.'表示一般房间,'#'表示毒气房间,进入毒气房间要消耗一个氧气瓶,而且要多停留一分钟,'B'表示放氧气瓶的房间,每次进入可以获得一个氧气瓶,最 ...
- Saving Tang Monk II
题目链接:http://hihocoder.com/contest/acmicpc2018beijingonline/problem/1 AC代码: #include<bits/stdc++.h ...
随机推荐
- NOIP2010提高组真题部分整理(没有关押罪犯)
目录 \(NOIP2010\)提高组真题部分整理 \(T1\)机器翻译: 题目背景: 题目描述: 输入输出格式: 输入输出样例: 说明: 题解: 代码: \(T2\)乌龟棋 题目背景: 题目描述: 输 ...
- luoguP1502过河题解
日常吐(fei)嘈(hua) 这道题作为最近卡了我3天的dp题(最后还是在题解的帮助下冥思苦想才过掉的题),窝觉得此题肥肠之脑洞,写此博客纪念 题解 过河 先来日常手玩样例: 咦感觉怎么手玩答案都像是 ...
- python模块------pyautogui
安装 pip install pyautogui 基本使用 查询 screenWidth, screenHeight = pyautogui.size() # 屏幕尺寸 mouseX, mouseY ...
- ORCAD导网表:遇"No_connect" property
问题: Orcad Capture中将No Connect标识放置到了原本应该放置连线的管脚上,不知道怎么删除. 虽然添加一根Wire可以掩盖该管脚上已经添加的No Connect标识,但是到处网表的 ...
- NavisWorks连接外部数据库,为模型附加属性
可以直接从Navisworks 文件连接到外部数据库,并在场景中的对象与数据库表中的字段之间创建链接以引入额外特性. 1.连接mdb数据库 新建数据连接 单击“新建”按钮,新建数据连接,输入一个名称, ...
- leetcode 139 单词拆分(word break)
一开始的错误答案与错误思路,幻想直接遍历得出答案: class Solution { public: bool wordBreak(string s, vector<string>& ...
- 阶段3 2.Spring_08.面向切面编程 AOP_3 spring基于XML的AOP-编写必要的代码
新建项目 先改打包方式 导包,就先导入这俩包的坐标 aspectjweaver为了解析切入点表达式 新建业务层接口 定义三个方法 看返回和参数的区别.为了把这三类方法表现出来,并不局限于方法干什么事 ...
- 阶段3 2.Spring_04.Spring的常用注解_5 自动按照类型注入
运行出现了空指针异常 @Autowired 注解出现的位置 AutoWired的代码 常用的就是写类上和方法上. 运行测试,刚才运行是一个空指针异常 也就是通过Autowired 这个accountD ...
- Elasticsearch 6.2.3版本 string 类型字段 排序 报错 Fielddata is disabled on text fields by default
背景说明 最近在做一个 Elasticsearch 的分页查询,并且对查询结果按照特定字段进行排序的功能. 但是执行结果却报错,报错信息如下: { "error": { " ...
- java:Spring框架1(基本配置,简单基础代码模拟实现,spring注入(DI))
1.基本配置: 步骤一:新建项目并添加spring依赖的jar文件和commons-logging.xx.jar: 步骤二:编写实体类,DAO及其实现类,Service及其实现类; 步骤三:在src下 ...