CodeForces - 330D Biridian Forest

今天药忘吃喽~ 2022-06-08 13:22 212阅读 0赞

D. 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):

a5a88bae091e2185655dead4b8002dde978460aa.png

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, exactly t mikemon battles will ensue that time (since you will be battling each of those t breeders once). After the battle, all of those t 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 and c (1 ≤ r, c ≤ 1000), denoting the number of rows and the number of columns in Biridian Forest. The next r 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.

Examples

input

  1. 5 7
  2. 000E0T3
  3. T0TT0T0
  4. 010T0T0
  5. 2T0T0T0
  6. 0T0S000

output

  1. 3

input

  1. 1 4
  2. SE23

output

  1. 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:

50aea743036b4c1d05455f5bf5466aa0d1544457.png

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:

22359acf14df99b3379bf8ad0a4b287ccc3ca621.png

Here’s what happens. First, you move one cell to the right.

6d358784fa53d500aed4e054dcc147047a02a91f.png

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.

5e251643d7a5c39635e3ae29d8ee99dccf53a3e1.png

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.

ce4e700418310e93e97c329e8b13a207369f8461.png

Finally, you make another move by leaving the forest.

8e510d50bcecdd09a7864c02c5ac8eff1ac78d72.png
思路:从终点广搜,只要breeder的步数小于等于你的步数, 就加上;自己开始的时候从breeder广搜的,结果超时了;

  1. #include<stdio.h>
  2. #include<cstring>
  3. #include<queue>
  4. using namespace std;
  5. const int maxn=1010;
  6. const int INF=0x3f3f3f3f;
  7. int n, m, sum=0, sx, sy, ex, ey, cnt=0, shortest;
  8. char mp[maxn][maxn];
  9. int vis[maxn][maxn], steps[maxn][maxn], breeder[maxn*maxn][2];
  10. int dir[4][2]={
  11. {0, 1}, {0, -1}, {-1, 0}, {1, 0}};
  12. struct node{
  13. int x, y, step;
  14. };
  15. void BFS()
  16. {
  17. memset(vis, 0, sizeof(vis));
  18. queue<node> q;
  19. q.push((node){ex, ey, 0});
  20. while(q.size())
  21. {
  22. node it=q.front();
  23. q.pop();
  24. if(it.x==sx && it.y==sy)
  25. {
  26. shortest=it.step;
  27. return;
  28. }
  29. for(int i=0; i<4; i++)
  30. {
  31. int tx=it.x+dir[i][0];
  32. int ty=it.y+dir[i][1];
  33. if(tx<1 || tx>n || ty<1 || ty>m)
  34. continue;
  35. if(!vis[tx][ty] && mp[tx][ty]!='T')
  36. {
  37. if(mp[tx][ty]>'0' && mp[tx][ty]<='9')
  38. {
  39. steps[tx][ty]=it.step+1;
  40. }
  41. vis[tx][ty]=1;
  42. q.push((node){tx, ty, it.step+1});
  43. }
  44. }
  45. }
  46. }
  47. int main()
  48. {
  49. memset(steps, 0x3f, sizeof(steps));
  50. scanf("%d%d", &n, &m);getchar();
  51. for(int i=1; i<=n; i++)
  52. {
  53. for(int j=1; j<=m; j++)
  54. {
  55. mp[i][j]=getchar();
  56. if(mp[i][j]=='S')
  57. {
  58. sx=i; sy=j;
  59. }
  60. else if(mp[i][j]=='E')
  61. {
  62. ex=i; ey=j;
  63. }
  64. else if(mp[i][j]>'0' && mp[i][j]<='9')
  65. {
  66. breeder[++cnt][0]=i; breeder[cnt][1]=j;
  67. }
  68. }
  69. getchar();
  70. }
  71. BFS();
  72. for(int i=1; i<=cnt; i++)
  73. {
  74. if(steps[breeder[i][0]][breeder[i][1]]<=shortest)
  75. sum+=(mp[breeder[i][0]][breeder[i][1]]-'0');
  76. }
  77. printf("%d\n", sum);
  78. return 0;
  79. }

发表评论

表情:
评论列表 (有 0 条评论,212人围观)

还没有评论,来说两句吧...

相关阅读

    相关 CodeForces1214D

    [CodeForces1214D][] 这个题据我所知有两种比较优秀的做法. 第一种是\\(DP\\)统计每个点的路径数,然后找出必经点,再从必经点开始\\(bfs\\

    相关 CodeForces1208D

    CodeForces1208D 也是挺吓人的一道题,我一开始以为给的是每个数字前比它小的数字有几个,然后我就苦苦看不懂样例... 然后我冷静了一下,重新分析,读题,发现

    相关 Codeforces 496D

    题意 -------------------- 进行若干场比赛,每次比赛两人对决,赢的人得到1分,输的人不得分,先得到t分的人获胜,开始下场比赛,某个人率先赢下s场比赛