codeforces 329B B. Biridian Forest 精巧的bfs
2015-04-29 19:29
351 查看
B. Biridian Forest
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
You're a mikemon breeder currently in the middle of your journey to become a mikemon master. Your current obstacle is go through the infamous Biridian Forest.
The forest
The Biridian Forest is a two-dimensional grid consisting of
r rows and c columns. Each cell in Biridian Forest may contain a tree, or may be vacant. A vacant cell may be occupied by zero or more mikemon breeders (there may also be breeders other than you in the
forest). Mikemon breeders (including you) cannot enter cells with trees. One of the cells is designated as the exit cell.
The initial grid, including your initial position, the exit cell, and the initial positions of all other breeders, will be given to you. Here's an example of such grid (from the first example):
Moves
Breeders (including you) may move in the forest. In a single move, breeders may perform one of the following actions:
Do nothing.
Move from the current cell to one of the four adjacent cells (two cells are adjacent if they share a side). Note that breeders cannot enter cells with trees.
If you are located on the exit cell, you may leave the forest. Only you can perform this move — all other mikemon breeders will never leave the forest by using this type of movement.
After each time you make a single move, each of the other breeders simultaneously make a single move (the choice of which move to make may be different for each of the breeders).
Mikemon battle
If you and t (t > 0) mikemon breeders are located on the same cell, exactlyt mikemon battles will ensue that time (since you will be battling
each of thoset breeders once). After the battle, all of thoset breeders will leave the forest to heal their respective mikemons.
Note that the moment you leave the forest, no more mikemon battles can ensue, even if another mikemon breeder move to the exit cell immediately after that. Also note that a battle only happens between you and another breeders — there will be no battle between
two other breeders (there may be multiple breeders coexisting in a single cell).
Your goal
You would like to leave the forest. In order to do so, you have to make a sequence of moves, ending with a move of the final type. Before you make any move, however, you post this sequence on your personal virtual idol Blog. Then, you will follow this sequence
of moves faithfully.
Goal of other breeders
Because you post the sequence in your Blog, the other breeders will all know your exact sequence of moves even before you make your first move. All of them will move in such way that will guarantee a mikemon battle with you, if possible. The breeders that
couldn't battle you will do nothing.
Your task
Print the minimum number of mikemon battles that you must participate in, assuming that you pick the sequence of moves that minimize this number. Note that you are not required to minimize the number of moves you make.
Input
The first line consists of two integers: r andc (1 ≤ r, c ≤ 1000), denoting the number of rows and the number of columns in Biridian
Forest. The nextr rows will each depict a row of the map, where each character represents the content of a single cell:
'T': A cell occupied by a tree.
'S': An empty cell, and your starting position. There will be exactly one occurence of this in the map.
'E': An empty cell, and where the exit is located. There will be exactly one occurence of this in the map.
A digit (0-9): A cell represented by a digit X means that the cell is empty and is occupied by X breeders (in particular, if X is zero, it means that the cell is not occupied by any breeder).
It is guaranteed that it will be possible for you to go from your starting position to the exit cell through a sequence of moves.
Output
A single line denoted the minimum possible number of mikemon battles that you have to participate in if you pick a strategy that minimize this number.
Sample test(s)
Input
Output
Input
Output
Note
The following picture illustrates the first example. The blue line denotes a possible sequence of moves that you should post in your blog:
The three breeders on the left side of the map will be able to battle you — the lone breeder can simply stay in his place until you come while the other two breeders can move to where the lone breeder is and stay there until you come. The three breeders
on the right does not have a way to battle you, so they will stay in their place.
For the second example, you should post this sequence in your Blog:
Here's what happens. First, you move one cell to the right.
Then, the two breeders directly to the right of the exit will simultaneously move to the left. The other three breeder cannot battle you so they will do nothing.
You end up in the same cell with 2 breeders, so
2 mikemon battles are conducted. After those battles, all of your opponents leave the forest.
Finally, you make another move by leaving the forest.
题意:从起点走到重点,其中有些地方不能走,有些地方存在一些敌人(0~9),且能和我们一样正常移动或者是停留,每次动作都是一个单位的时间,而敌人总是尽量杀向你。求起点到终点能最少干掉敌人的数目。
首先如果每次我们处理的时候连‘我’和‘敌人’一起处理,显然是不行的,状态太多。仔细想一想,其实我们可以得到这样的一个结论:那些能在我们走向重点的路上截杀我们的敌人,其到达重点的距离一定是<=我们自身的。而最差情况下,那些能和我们打一架的敌人完全可以先在终点等我们。而那些在重点等着我们的敌人的总数就是我们要消灭的数量(在这种情况下我们总是要尽量早的到达重点,以防止更多无谓的敌人到达重点等我们杀)。
那么这样一来,我们以终点为起点做一次bfs就行了。统计那些早于起点碰到的敌人数量就是要求的答案
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
You're a mikemon breeder currently in the middle of your journey to become a mikemon master. Your current obstacle is go through the infamous Biridian Forest.
The forest
The Biridian Forest is a two-dimensional grid consisting of
r rows and c columns. Each cell in Biridian Forest may contain a tree, or may be vacant. A vacant cell may be occupied by zero or more mikemon breeders (there may also be breeders other than you in the
forest). Mikemon breeders (including you) cannot enter cells with trees. One of the cells is designated as the exit cell.
The initial grid, including your initial position, the exit cell, and the initial positions of all other breeders, will be given to you. Here's an example of such grid (from the first example):
Moves
Breeders (including you) may move in the forest. In a single move, breeders may perform one of the following actions:
Do nothing.
Move from the current cell to one of the four adjacent cells (two cells are adjacent if they share a side). Note that breeders cannot enter cells with trees.
If you are located on the exit cell, you may leave the forest. Only you can perform this move — all other mikemon breeders will never leave the forest by using this type of movement.
After each time you make a single move, each of the other breeders simultaneously make a single move (the choice of which move to make may be different for each of the breeders).
Mikemon battle
If you and t (t > 0) mikemon breeders are located on the same cell, exactlyt mikemon battles will ensue that time (since you will be battling
each of thoset breeders once). After the battle, all of thoset breeders will leave the forest to heal their respective mikemons.
Note that the moment you leave the forest, no more mikemon battles can ensue, even if another mikemon breeder move to the exit cell immediately after that. Also note that a battle only happens between you and another breeders — there will be no battle between
two other breeders (there may be multiple breeders coexisting in a single cell).
Your goal
You would like to leave the forest. In order to do so, you have to make a sequence of moves, ending with a move of the final type. Before you make any move, however, you post this sequence on your personal virtual idol Blog. Then, you will follow this sequence
of moves faithfully.
Goal of other breeders
Because you post the sequence in your Blog, the other breeders will all know your exact sequence of moves even before you make your first move. All of them will move in such way that will guarantee a mikemon battle with you, if possible. The breeders that
couldn't battle you will do nothing.
Your task
Print the minimum number of mikemon battles that you must participate in, assuming that you pick the sequence of moves that minimize this number. Note that you are not required to minimize the number of moves you make.
Input
The first line consists of two integers: r andc (1 ≤ r, c ≤ 1000), denoting the number of rows and the number of columns in Biridian
Forest. The nextr rows will each depict a row of the map, where each character represents the content of a single cell:
'T': A cell occupied by a tree.
'S': An empty cell, and your starting position. There will be exactly one occurence of this in the map.
'E': An empty cell, and where the exit is located. There will be exactly one occurence of this in the map.
A digit (0-9): A cell represented by a digit X means that the cell is empty and is occupied by X breeders (in particular, if X is zero, it means that the cell is not occupied by any breeder).
It is guaranteed that it will be possible for you to go from your starting position to the exit cell through a sequence of moves.
Output
A single line denoted the minimum possible number of mikemon battles that you have to participate in if you pick a strategy that minimize this number.
Sample test(s)
Input
5 7 000E0T3 T0TT0T0 010T0T0 2T0T0T0 0T0S000
Output
3
Input
1 4
SE23
Output
2
Note
The following picture illustrates the first example. The blue line denotes a possible sequence of moves that you should post in your blog:
The three breeders on the left side of the map will be able to battle you — the lone breeder can simply stay in his place until you come while the other two breeders can move to where the lone breeder is and stay there until you come. The three breeders
on the right does not have a way to battle you, so they will stay in their place.
For the second example, you should post this sequence in your Blog:
Here's what happens. First, you move one cell to the right.
Then, the two breeders directly to the right of the exit will simultaneously move to the left. The other three breeder cannot battle you so they will do nothing.
You end up in the same cell with 2 breeders, so
2 mikemon battles are conducted. After those battles, all of your opponents leave the forest.
Finally, you make another move by leaving the forest.
题意:从起点走到重点,其中有些地方不能走,有些地方存在一些敌人(0~9),且能和我们一样正常移动或者是停留,每次动作都是一个单位的时间,而敌人总是尽量杀向你。求起点到终点能最少干掉敌人的数目。
首先如果每次我们处理的时候连‘我’和‘敌人’一起处理,显然是不行的,状态太多。仔细想一想,其实我们可以得到这样的一个结论:那些能在我们走向重点的路上截杀我们的敌人,其到达重点的距离一定是<=我们自身的。而最差情况下,那些能和我们打一架的敌人完全可以先在终点等我们。而那些在重点等着我们的敌人的总数就是我们要消灭的数量(在这种情况下我们总是要尽量早的到达重点,以防止更多无谓的敌人到达重点等我们杀)。
那么这样一来,我们以终点为起点做一次bfs就行了。统计那些早于起点碰到的敌人数量就是要求的答案
#include <map> #include <set> #include <queue> #include <stack> #include <vector> #include <math.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <iostream> #include <algorithm> using namespace std; #define lson l, mid, rt << 1 #define rson mid + 1, r, rt << 1 | 1 #define ls rt << 1 #define rs rt << 1 | 1 #define pi acos(-1.0) #define eps 1e-8 typedef long long ll; const int inf = 0x3f3f3f3f; const int N = 1010; struct node{ int x, y, t; }st, ed; queue <node> q; char a ; bool vis ; int dir[4][2] = { 1, 0, -1, 0, 0, 1, 0, -1 }; int n, m; int main() { while( ~scanf("%d%d", &n, &m) ) { for( int i = 1; i <= n; ++i ) { scanf("%s", a[i]+1); } for( int i = 1; i <= n; ++i ) { for( int j = 1; j <= m; ++j ) { if( a[i][j] == 'S' ) { st.x = i, st.y = j; } if( a[i][j] == 'E' ) { ed.x = i, ed.y = j; } } } while( !q.empty() ) q.pop(); memset( vis, 0, sizeof( vis ) ); int ans = 0, tmp = inf; vis[ed.x][ed.y] = 1; ed.t = 0; q.push(ed); while( !q.empty() ) { node now = q.front(); q.pop(); if( now.t > tmp ) continue; if( now.x == st.x && now.y == st.y ) { tmp = now.t; } if( a[now.x][now.y] >= '0' && a[now.x][now.y] <= '9' ) ans += a[now.x][now.y] - '0'; for( int i = 0; i < 4; ++i ) { int xx = dir[i][0] + now.x; int yy = dir[i][1] + now.y; int tt = now.t + 1; if( !vis[xx][yy] && xx >= 1 && xx <= n && yy >= 1 && yy <= m && a[xx][yy] != 'T' ) { node nxt; nxt.x = xx, nxt.y = yy, nxt.t = tt; q.push(nxt); vis[xx][yy] = 1; } } } printf("%d\n", ans); } return 0; }
相关文章推荐
- CodeForces - 330D Biridian Forest(逆向bfs)
- Codeforces 330D Biridian Forest【思维+Bfs】
- Biridian Forest CodeForces - 329B (逆向bfs)
- CodeForces - 330D Biridian Forest —— BFS 逆搜
- Codeforces Round #192 (Div. 1) B. Biridian Forest 暴力bfs
- Codeforces Round #192 (Div. 1) B. Biridian Forest(BFS)
- CodeForces 329B(BFS)
- CodeForces - 329B 《Biridian Forest》 【反向BFS】
- CodeForces - 330D Biridian Forest(BFS+思维)
- E - Biridian Forest CodeForces - 330D(BFS)
- CodeForces 329B(bfs)
- CF 329B(Biridian Forest-贪心-非二分)
- CF 329B(Biridian Forest-贪心-非二分)
- Codeforces Round #192 (Div. 2) D. Biridian Forest (水bfs)
- codeforces 520 B. Two Buttons (bfs)
- Codeforces 463D Gargari and Permutations(BFS)
- codeforces 295C Greg and Friends(BFS+DP)
- codeforces 793B - Igor and his way to work(bfs)
- Codeforces 811D Vladik and Favorite Game【思维+Bfs】
- codeforces 590C(bfs)