Codeforce ---392B Biridian Forest (BFS)
来源:互联网 发布:淘宝网短款毛衫 编辑:程序博客网 时间:2024/05/16 12:29
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.
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.
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.
5 7000E0T3T0TT0T0010T0T02T0T0T00T0S000
3
1 4SE23
2
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.
分析:如果主任共在去终点的途中碰到了敌人,那么换个角度想一下,这些在路途中遇到的敌人都可以在终点遇到(这需要仔细想想为什么),那么问题就变简单了,我们可以算出有多少个含有敌人的点到终点的步数小于等于主人公到终点的步数,这些点上的敌人之和就是主人公需要对付的。
解法:有一种方法就是从起点和所有含有敌人的点开始bfs,算出所有的敌人到终点的步数,和主人公需要的步数作比较然后算出和,显然这种方法比较麻烦,并且有可能超时(没写这种方法。。。);第二种方法,我们把整个过程倒过来看,从终点往起点bfs,在搜到起点之前遇到的敌人都是会阻止主人公的人(同样仔细想想哦~)。
这题思路是比较新奇的,附上第二种方法的ac代码:
#include<iostream>#include<cstdio>#include<cstring>#include<queue>using namespace std;char a[1005][1005],book[1005][1005];int n,m;struct node{ int x; int y; int step;};node q,p;queue<node> que;int check(int x,int y){ if(x<1||x>n||y<1||y>m) return 0; if(book[x][y]==1||a[x][y]=='T') return 0; return 1;}int bfs(){ int next[4][2]={0,1,0,-1,1,0,-1,0}; int ans=0; int flag=0,t; while(que.size()) { q=que.front(); que.pop(); if(flag&&q.step==t)//这一步很关键的(开始没写这个判断条件wa了一遍,看代码好好理解); return ans; for(int i=0;i<4;i++) { int tx=q.x+next[i][0]; int ty=q.y+next[i][1]; if(check(tx,ty)) { if(a[tx][ty]>='1'&&a[tx][ty]<='9') { ans+=a[tx][ty]-'0'; } if(a[tx][ty]=='S') { t=q.step+1; flag=1; } book[tx][ty]=1; p.x=tx; p.y=ty; p.step=q.step+1; que.push(p); } } }}int main(){ while(~scanf("%d%d",&n,&m)) { getchar(); int sx,sy,ex,ey; memset(book,0,sizeof(book)); for(int i=1;i<=n;i++) { for(int j=1;j<=m;j++) { scanf("%c",&a[i][j]); if(a[i][j]=='S'){sx=i;sy=j;} if(a[i][j]=='E'){ex=i;ey=j;} } getchar(); } q.x=ex; q.y=ey; q.step=0; que.push(q); book[ex][ey]=1; int ans=bfs(); printf("%d\n",ans); } return 0;}
- Codeforce ---392B Biridian Forest (BFS)
- codeforce 329B Biridian Forest(bfs)
- codeforce 329B Biridian Forest
- CodeForces - 329B 《Biridian Forest》 【反向BFS】
- Codeforces 329B Biridian Forest(BFS)
- codeforces 329B B. Biridian Forest 精巧的bfs
- Codeforces Round #192 (Div. 1) B. Biridian Forest(BFS)
- Codeforces--192.div2.D Biridian Forest bfs
- Codeforces 330D Biridian Forest【思维+Bfs】
- Biridian Forest
- CF 329B(Biridian Forest-贪心-非二分)
- codeforce 689B 【bfs】
- codeforces Biridian Forest
- Biridian Forest CodeForces
- Codeforces Round #192 (Div. 2) D. Biridian Forest (水bfs)
- Igor and his way to work-Codeforce 793B BFS
- codeforce #501 c Misha and Forest
- Codeforce # B Sequence Formatting
- Ceph对象存储(rgw)的IPv6环境配置
- 常用排序算法汇总js版
- 布局技巧
- 算法系列——Remove Linked List Elements
- 矩阵的特征值及相关矩阵特征值和特征向量公式
- Codeforce ---392B Biridian Forest (BFS)
- 树莓派——认识树莓派
- Spring MVC中Controller如何进行重定向
- poj 1733 Parity game 并查集 解题报告
- Spring中配置使用Redis
- 空间限制
- 树的同构
- C++ 扫雷游戏实现
- properties文件