题目:
H . Time to Eat [ 问题 8933 ] [ 讨论 ]
Description
The UCF Programming Team has made it to the World Contest Finals (WF), thanks to the great team members and coaches. Fortunately for Dr. Orooji (Team Faculty Advisor), WF is in a city with streets running only horizontally and vertically (this means the chance of Dr. O getting lost is less). The city can be described as a two-dimensional grid with R rows and C columns. The team hotel is at the upper-left corner (cell with indices 1, 1). The contest will be at the bottom-right corner (cell with indices R,C).
The UCF group will start at the hotel and needs to be at the contest site. From a cell, the group can walk into one of the four neighboring cells (north, south, east, west). Note that the cells on the boundary do not have four neighbors. The group would like to make it to the contest with the fewest number of steps – moving from a cell to a neighboring cell is considered taking a step.
One complication with the trip from the hotel to the contest site is that the UCF group gets hungry and needs to eat after they’ve taken certain number of steps. For example, if they have to eat after taking 10 steps, then their 10th step (or an earlier step) must walk them into a cell with food (sub shop).
The need to eat means the group may not be able to take the shortest path from the hotel to the contest site because the sub shops may not be on that path. Fortunately, there are enough sub shops at different spots (cells) such that the group can eat when needed and make it to the contest site, though they may take a few extra steps than the straight path from the hotel to the contest site.
The Problem:
Given the description of the city, determine the minimum number of steps needed for the UCF group to go from their hotel to the contest site, taking into account that they need to eat after taking certain number of steps (or before taking that many steps if a sub shop is not exactly at that position on the path).
Input
The first input line contains four integers: R (1≤R≤50), indicating the number of rows in the grid, C (1≤C≤50), indicating the number of columns in the grid, F (1≤F≤100), indicating the number of steps the group can take and then need to eat, and S (1≤S≤100), indicating the number of sub shops in the grid.
Each of the next S input lines contains two integers (1≤r≤R, 1≤c≤C) providing the row and column number for a sub shop. Assume that all sub shops are at different locations and they are not at the hotel or contest site.
Output
Print the minimum number of steps needed for the UCF group to go from their hotel to the contest site.
Samples
Input 复制
10 6 5 7
1 6
5 4
8 1
9 1
8 3
10 1
2 5
Output
18
Input 复制
5 10 5 3
1 5
2 7
5 6
Output
13
题意:有一个团队需要从地图的左上角走到右下角,这个团队有一个体力值,每走一步会掉一点体力值,在体力值消耗完前需要去商店里吃东西补充体力值,最后走到右下角,问团队最少需要走的步数。(保证在体力值消耗完前能找到一个商店)
输入:给定地图的长R和宽C,团队的体力值F,商店的个数S,接下来S行每行一个坐标
输出:团队从左上角到右下角需要的最少步数
思路:一看到最少步数就可以想到最优,最短之类的词语,一般这种题就要用到BFS,所以这道题就是一个有点新奇的BFS。
// The past is irrevocable and the future can be changed.
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int m,n,k,s,x,y;
int a[111][111];
int dir[6][6]= {{1,0},{-1,0},{0,1},{0,-1}};
map< pair<pair<int,int>, pair<int,int> >,bool> mp;
struct ss{ //存团队状态的结构体 int x,y;int tl;int step;
}pre;
int dfs(){queue<ss>q; //开一个队列存团队在走到每一个点的状态 q.push(pre);mp[{{pre.x,pre.y},{pre.step,pre.tl}}]=1;//防止重复推入一样的情况导致内存超限 while(q.size()){ss fir=q.front();if(fir.x==m&&fir.y==n) //如果走到终点就返回团队走的步数 return fir.step;q.pop();for(int i=0;i<4;i++){ss sec;int dx=fir.x+dir[i][0];int dy=fir.y+dir[i][1];if(dx>=1&&dx<=m&&dy>=1&&dy<=n&&fir.tl>0){ //pre.tl>0防止体力等于零后又走到商店回满体力 if(a[dx][dy]) //如果走到商店,补满体力 sec.tl=k;else //否则减一点体力 sec.tl=fir.tl-1;sec.x=dx,sec.y=dy,sec.step=fir.step+1; //更新状态if(sec.tl>=0){ //如果此时体力大于等于零,将此时的状态存入队列 if(mp[{{sec.x,sec.y},{sec.step,sec.tl}}]==0){//防止重复推入一样的情况导致内存超限q.push(sec);mp[{{sec.x,sec.y},{sec.step,sec.tl}}]=1;}} } //cout<<sec.x<<" "<<sec.y<<" "<<sec.tl<<" "<<sec.step<<" "<<"|"<<endl;}}
}
int main(){cin>>m>>n>>k>>s;pre.x=1,pre.y=1,pre.tl=k,pre.step=0; //初始化团队最初的状态 for(int i=1;i<=s;i++){cin>>x>>y;a[x][y]=1;}int res=dfs();cout<<res;return 0;
}
相似题目:UCF HSPT 2020 - E . Use Giant Fans to Deal With Hurricanes?
题目:E . Use Giant Fans to Deal With Hurricanes? [ 问题 8006 ] [ 讨论 ]
Description
The Earth is under attack by Aliens! Anybody who has watched the movie “Signs” knows that Aliens are weak to water. In a last ditch effort to save humanity, the world military has cornered the Aliens on an island and set up giant fans on big aircraft carriers surrounding it.
The island can be represented as a square n×n grid. There are three types of cells. Cells with no Aliens on them are denoted by ‘W’ (water) or ‘L’ (land). An Alien’s position is represented by the letter ‘A’. Since it is an island, the borders of the grid are guaranteed to be water cells.
The military arranges their four fans facing north, south, east, and west, and can only activate one at a time. When a fan activates, it pushes all Aliens one cell in that direction. If an Alien is pushed into water, it instantly drowns. Aliens do not move unless a fan is activated. They just stand there. Menacingly.
While the world is facing an existential threat, the military still wishes to conserve energy in order to prevent another existential threat in the form of climate change. For this reason, you have been hired to find the minimum number of times a fan needs to be activated to kill all Aliens.
The Problem:
Determine the minimum number of times a fan must be activated in order to kill all Aliens.
Input
The first line of the input will begin with a single positive integer, t, representing the number of scenarios. For each scenario, multiple lines will follow. The first line contains a single integer, n (3≤n≤10), representing the dimensions of the island. Then, on the following lines there are n rows each consisting of n characters, as described in above, describing the state of the island. It is guaranteed that there is at least one alien.
Output
For each scenario, output a single line with an integer representing the minimum number of fan activations.
Samples
Input 复制
3
8
WWWWWWWW
WALLLLLW
WLWWLLLW
WLLWLWLW
WLALLALW
WWLLLLLW
WLLLLLAW
WWWWWWWW
4
WWWW
WLAW
WAWW
WWWW
3
WWW
WAW
WWW
Output
2
1
1
题意:给一个N*N的地图,里面有一些外星人,还有一些水池,外星人掉入水池里就会死亡 ,现在你有一个超级风扇,每次可以把所有的外星人向 上下左右其中一个方向吹走一步,最后把所有的外星人都吹入池塘,问你最少需要多少次操作可以把外星人都干掉。
思路:这个题跟上面那个很相似,也是一个特别的BFS,不过这个题目中队列里存的是每个外星人的状态。
//The sunrise in the east reminds people to wake up, less than the sunset knows my heart
#include<bits/stdc++.h>
using namespace std;
int n;
char s[101][101];
int dir[6][6]= {{1,0},{-1,0},{0,1},{0,-1}};
struct node {vector< pair<int,int> >p; //记录所有外星人的坐标! (在上个题目里面用的是 x和 y,区别不大) int step; //记录我们操作的步数! int num; //记录当前外星人的存活数量!
} Node;
int bfs() {queue<node> q;q.push(Node);while(q.size()) {node now=q.front();if(now.num==0)return now.step;q.pop();for(int i=0; i<4; i++) { //遍历每个方向!node new_node;for(int j=0; j<now.p.size(); j++) {int dx=now.p[j].first+dir[i][0];int dy=now.p[j].second+dir[i][1];if(s[dx][dy]!='W'&&dx>=1&&dx<=n&&dy>=1&&dy<=n) {new_node.p.push_back({dx,dy});}}new_node.step=now.step+1;new_node.num=new_node.p.size();q.push(new_node);}}
}
int main() {int t;cin>>t;for(int i=1; i<=t; i++) {Node.p.clear();cin>>n;for(int j=1; j<=n; j++)for(int k=1; k<=n; k++) {cin>>s[j][k];if(s[j][k]=='A') {Node.p.push_back({j,k});}}Node.step=0;Node.num=Node.p.size();int ans=bfs();cout<<ans;if(i!=t)cout<<endl;}return 0;
}