icpc 2018 北京网络赛 A(搜索)

Saving Tang Monk II

时间限制:1000ms

单点时限:1000ms

内存限制:256MB

描述

《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.

输入

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.

输出

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

样例输入
2 2
S#
#T
2 5
SB###
##P#T
4 7
SP.....
P#.....
......#
B...##T
0 0
样例输出
-1
811

//傻逼搜索,不知道比赛的时候哪里写残了

#include <bits/stdc++.h>
using namespace std;
const int maxn=110;
char mp[maxn][maxn];
bool vis[maxn][maxn][10];
struct node{
    int x,y,step,b;
    inline bool operator<(const node &rhs) const {
         return step>rhs.step;    }
}now,tmp;
int n,m,sx,sy;

inline bool check(int x,int y){
    if(x>=0 && x<n &&y>=0 &&y<m) return true;
    return false;
}

int go[4][2]={
    {-1,0},{1,0},{0,-1},{0,1}
};

int bfs(){
    memset(vis,false,sizeof(vis));
    priority_queue<node>q;
    q.push(node{sx,sy,0,0});
    vis[sx][sy][0]=1;
    while(!q.empty()){
        now=q.top();
        q.pop();
        if(mp[now.x][now.y]==‘T‘) return now.step;
        for (int i=0; i<4; i++){
            tmp=now;
            int nx=now.x+go[i][0],ny=now.y+go[i][1];
            tmp.x=nx,tmp.y=ny;
            if(!check(nx,ny)) continue;
            if(mp[nx][ny]==‘B‘){
                tmp.step++;
                tmp.b=min(tmp.b+1,5);
                if(!vis[nx][ny][tmp.b]){
                    vis[nx][ny][tmp.b]=1;
                    q.push(tmp);
                }
            } else if(mp[nx][ny]==‘P‘){
                if(!vis[nx][ny][tmp.b]){
                    vis[nx][ny][tmp.b]=1;
                    q.push(tmp);
                }
            } else if(mp[nx][ny]==‘#‘){
                if(!tmp.b) continue;
                tmp.b--;
                tmp.step+=2;
                if(!vis[nx][ny][tmp.b]){
                    vis[nx][ny][tmp.b]=1;
                    q.push(tmp);
                }
            } else {
                tmp.step++;
                if(!vis[nx][ny][tmp.b]){
                    vis[nx][ny][tmp.b]=1;
                    q.push(tmp);
                }
            }
        }
    }
    return -1;
}
int main(){
    while(scanf("%d%d",&n,&m)==2,n+m){
        for (int i=0; i<n; i++){
            scanf("%s",mp[i]);
            for (int j=0; mp[i][j]; ++j){
                if(mp[i][j]==‘S‘)
                    sx=i,sy=j;
            }
        }
        int ans=bfs();
        printf("%d\n",ans);
    }
    return 0;
}

原文地址:https://www.cnblogs.com/acerkoo/p/9691632.html

时间: 2024-11-09 12:39:36

icpc 2018 北京网络赛 A(搜索)的相关文章

2018 北京网络赛

描述 <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

ICPC 2018 南京网络赛 J Magical Girl Haze(多层图最短路)

传送门:https://nanti.jisuanke.com/t/A1958 题意:n个点m条边的路,你有k次机会将某条路上的边权变为0,问你最短路径长度 题解:最短路变形,我们需要在常规的最短路上多开 一维,记录,我消耗j次机会时的最短路径长度为多少 代码: /** * ┏┓ ┏┓ * ┏┛┗━━━━━━━┛┗━━━┓ * ┃ ┃ * ┃ ━ ┃ * ┃ > < ┃ * ┃ ┃ * ┃... ⌒ ... ┃ * ┃ ┃ * ┗━┓ ┏━┛ * ┃ ┃ Code is far away fro

2015北京网络赛A题The Cats&#39; Feeding Spots

题意:给你一百个点,找个以这些点为中心的最小的圆,使得这个圆恰好包含了n个点,而且这个圆的边界上并没有点 解题思路:暴力枚举每个点,求出每个点到其他点的距离,取第n大的点,判断一下. 1 #include<cstdio> 2 #include<cmath> 3 #include<algorithm> 4 #include<iostream> 5 #include<memory.h> 6 using namespace std; 7 const i

2015北京网络赛 Couple Trees 倍增算法

2015北京网络赛 Couple Trees 题意:两棵树,求不同树上两个节点的最近公共祖先 思路:比赛时看过的队伍不是很多,没有仔细想.今天补题才发现有个 倍增算法,自己竟然不知道.  解法来自 qscqesze ,这个其实之前如果了解过倍增的话还是不是很难,不过这题的数据也不是很给力,极限数据理论上是过不了的.  其他解法有树链剖分?并不是很清楚.就这样水过了吧... 1 #include <iostream> 2 #include <cstdio> 3 #include &l

hihocoder1236(北京网络赛J):scores 分块+bitset

北京网络赛的题- -.当时没思路,听大神们说是分块+bitset,想了一下发现确实可做,就试了一下,T了好多次终于过了 题意: 初始有n个人,每个人有五种能力值,现在有q个查询,每次查询给五个数代表查询的五种能力值,输出有多少个人每种能力值都比查询的小 n和q都是50000,每种能力值最大也为50000 思路: 对于某一个大小的能力值,有哪些人的此项能力值比他小可以用一个50000的bitset表示.这样我们在查询的时候就可以拿到5个对应的bitset,对其进行and就可以得出最终的人数 这样每

2015北京网络赛 D-The Celebration of Rabbits 动归+FWT

2015北京网络赛 D-The Celebration of Rabbits 题意: 给定四个正整数n, m, L, R (1≤n,m,L,R≤1000). 设a为一个长度为2n+1的序列. 设f(x)为满足x≤ai≤m+x且ai的异或和为0 的序列a的个数. 求 ∑Rx=Lf(x)mod1000000007 思路:因为对于每一个第一次分配后的a序列对应唯一的x,所以我们就枚举x然后在求序列的个数.

2018 CCPC网络赛

2018 CCPC网络赛 Buy and Resell 题目描述:有一种物品,在\(n\)个地点的价格为\(a_i\),现在一次经过这\(n\)个地点,在每个地点可以买一个这样的物品,也可以卖出一个物品,问最终赚的钱的最大值. solution 用两个堆来维护,一个堆维护已经找到卖家的,一个堆维护还没找到卖家的. 对于第\(i\)个地点,在已经找到卖家的堆里找出卖的钱的最小值,如果最小值小于\(a_i\),则将卖家换成\(i\),然后将原来的卖家放到没找到卖家的那里:如果最小值对于\(a_i\)

2018徐州网络赛H. Ryuji doesn&#39;t want to study

题目链接: https://nanti.jisuanke.com/t/31458 题解: 建立两个树状数组,第一个是,a[1]*n+a[2]*(n-1)....+a[n]*1;第二个是正常的a[1],a[2],a[3]...a[n] #include "bits/stdc++.h" using namespace std; #define ll long long const int MAXN=1e5+10; ll sum[MAXN],ans[MAXN]; ll num[MAXN];

ACM-ICPC 2018徐州网络赛-H题 Ryuji doesn&#39;t want to study

C*M....死于update的一个long long写成int了 心累 不想写过程了 ******** 树状数组,一个平的一个斜着的,怎么斜都行 题库链接:https://nanti.jisuanke.com/t/31460 #include <iostream> #include <cstring> #define ll long long #define lowbit(x) (x & -x) using namespace std; const int maxn =