ACM/ICPC 2018亚洲区预选赛北京赛站网络赛 A(bfs)

2023-11-01 02:10

本文主要是介绍ACM/ICPC 2018亚洲区预选赛北京赛站网络赛 A(bfs),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

传送门

题面:

#1828 : 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
8
11

题意:

    你要从S走到T,其中每经过一次B,你将获得一罐氧气,你最多可以获得5罐氧气;每经过一次P,你可以获得一个加速器(你可以拥有无限多个加速器)。拥有一个加速器可以使得某一时刻走到下一个时刻不需要时间。每经过一次#,你需要花费一罐氧气,且需要花费额外的1单位时间,你没有氧气无法进入#。现在问你从S到T的最短路径。

题目分析:

    题目中是要求我们求从S到T的最短路径,因此我们不难想到可以用bfs进行解决。而在这个题中,最难处理的一点就是氧气瓶数量。

    为了解决氧气瓶的问题,因此我们可以在常规的dis数组的基础上再开一维状态用来记录现在拿了多少瓶氧气瓶,即代表着现在位于点处,拥有num个氧气瓶。

    而因为在之后的更新中,因为P的存在,导致了某些状态可以能会重复遍历,因此我们不能采用vis数组判重的方法去重,我们需要采用类似最短路松弛操作的做法,当发现一条更短的路径时才更新当前的状态。

代码:

#include <bits/stdc++.h>
#define maxn 105
using namespace std;
const int INF=0x3f3f3f3f;
struct Node{int x,y,num;Node(int _x,int _y,int _num){x=_x,y=_y,num=_num;}
};
int dir[4][2]={{0,1},{0,-1},{1,0},{-1,0}};
int dis[maxn][maxn][10];
char str[maxn][maxn];
int n,m;
int sx,sy,tx,ty;
void bfs(int x,int y){memset(dis,INF,sizeof(dis));queue<Node> que;dis[x][y][0]=0;que.push(Node(x,y,0));while(!que.empty()){Node now=que.front();que.pop();for(int i=0;i<4;i++){Node to=Node(0,0,0);to.x=now.x+dir[i][0];to.y=now.y+dir[i][1];if((to.x>=n||to.x<0)||(to.y>=m||to.y<0)) continue;if(str[to.x][to.y]=='B'&&now.num<5) to.num=now.num+1;//当前处于氧气状态,且之前的氧气瓶数量少于u,则获取else to.num=now.num;// 否则直接覆盖int tmp=INF;//用tmp作为松弛的条件if(str[to.x][to.y]!='#') tmp=dis[now.x][now.y][now.num]+1;//不是毒气,则直接+1else if(to.num>=1){//为#且氧气数量大于1to.num--;tmp=dis[now.x][now.y][now.num]+2;}if(str[to.x][to.y]=='P') tmp--;if(dis[to.x][to.y][to.num]>tmp){//松弛操作dis[to.x][to.y][to.num]=tmp;que.push(to);}}}
}
int main()
{while(~scanf("%d%d",&n,&m)){if(n==0&&m==0) break;for(int i=0;i<n;i++) scanf("%s",str[i]);for(int i=0;i<n;i++){for(int j=0;j<m;j++)if(str[i][j]=='S') sx=i,sy=j;else if(str[i][j]=='T') tx=i,ty=j;}bfs(sx,sy);int ans=0x3f3f3f3f;for(int i=0;i<=5;i++) ans=min(ans,dis[tx][ty][i]);if(ans>=INF) printf("-1\n");else printf("%d\n",ans);}return 0;
}

 

这篇关于ACM/ICPC 2018亚洲区预选赛北京赛站网络赛 A(bfs)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/319740

相关文章

hdu1254(嵌套bfs,两次bfs)

/*第一次做这种题感觉很有压力,思路还是有点混乱,总是wa,改了好多次才ac的思路:把箱子的移动当做第一层bfs,队列节点要用到当前箱子坐标(x,y),走的次数step,当前人的weizhi(man_x,man_y),要判断人能否将箱子推到某点时要嵌套第二层bfs(人的移动);代码如下:

认识、理解、分类——acm之搜索

普通搜索方法有两种:1、广度优先搜索;2、深度优先搜索; 更多搜索方法: 3、双向广度优先搜索; 4、启发式搜索(包括A*算法等); 搜索通常会用到的知识点:状态压缩(位压缩,利用hash思想压缩)。

Linux 网络编程 --- 应用层

一、自定义协议和序列化反序列化 代码: 序列化反序列化实现网络版本计算器 二、HTTP协议 1、谈两个简单的预备知识 https://www.baidu.com/ --- 域名 --- 域名解析 --- IP地址 http的端口号为80端口,https的端口号为443 url为统一资源定位符。CSDNhttps://mp.csdn.net/mp_blog/creation/editor

ASIO网络调试助手之一:简介

多年前,写过几篇《Boost.Asio C++网络编程》的学习文章,一直没机会实践。最近项目中用到了Asio,于是抽空写了个网络调试助手。 开发环境: Win10 Qt5.12.6 + Asio(standalone) + spdlog 支持协议: UDP + TCP Client + TCP Server 独立的Asio(http://www.think-async.com)只包含了头文件,不依

BUUCTF靶场[web][极客大挑战 2019]Http、[HCTF 2018]admin

目录   [web][极客大挑战 2019]Http 考点:Referer协议、UA协议、X-Forwarded-For协议 [web][HCTF 2018]admin 考点:弱密码字典爆破 四种方法:   [web][极客大挑战 2019]Http 考点:Referer协议、UA协议、X-Forwarded-For协议 访问环境 老规矩,我们先查看源代码

poj 3181 网络流,建图。

题意: 农夫约翰为他的牛准备了F种食物和D种饮料。 每头牛都有各自喜欢的食物和饮料,而每种食物和饮料都只能分配给一头牛。 问最多能有多少头牛可以同时得到喜欢的食物和饮料。 解析: 由于要同时得到喜欢的食物和饮料,所以网络流建图的时候要把牛拆点了。 如下建图: s -> 食物 -> 牛1 -> 牛2 -> 饮料 -> t 所以分配一下点: s  =  0, 牛1= 1~

poj 2195 bfs+有流量限制的最小费用流

题意: 给一张n * m(100 * 100)的图,图中” . " 代表空地, “ M ” 代表人, “ H ” 代表家。 现在,要你安排每个人从他所在的地方移动到家里,每移动一格的消耗是1,求最小的消耗。 人可以移动到家的那一格但是不进去。 解析: 先用bfs搞出每个M与每个H的距离。 然后就是网络流的建图过程了,先抽象出源点s和汇点t。 令源点与每个人相连,容量为1,费用为

poj 3068 有流量限制的最小费用网络流

题意: m条有向边连接了n个仓库,每条边都有一定费用。 将两种危险品从0运到n-1,除了起点和终点外,危险品不能放在一起,也不能走相同的路径。 求最小的费用是多少。 解析: 抽象出一个源点s一个汇点t,源点与0相连,费用为0,容量为2。 汇点与n - 1相连,费用为0,容量为2。 每条边之间也相连,费用为每条边的费用,容量为1。 建图完毕之后,求一条流量为2的最小费用流就行了

poj 2112 网络流+二分

题意: k台挤奶机,c头牛,每台挤奶机可以挤m头牛。 现在给出每只牛到挤奶机的距离矩阵,求最小化牛的最大路程。 解析: 最大值最小化,最小值最大化,用二分来做。 先求出两点之间的最短距离。 然后二分匹配牛到挤奶机的最大路程,匹配中的判断是在这个最大路程下,是否牛的数量达到c只。 如何求牛的数量呢,用网络流来做。 从源点到牛引一条容量为1的边,然后挤奶机到汇点引一条容量为m的边

POJ 3057 最大二分匹配+bfs + 二分

SampleInput35 5XXDXXX...XD...XX...DXXXXX5 12XXXXXXXXXXXXX..........DX.XXXXXXXXXXX..........XXXXXXXXXXXXX5 5XDXXXX.X.DXX.XXD.X.XXXXDXSampleOutput321impossible