Codeforces -- 192. div2.d biridian forest BFS

Source: Internet
Author: User
B. biridian foresttime limit per Test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard 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-dimen1_grid consistingRRows andCColumns. 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 andT(T?>? 0) mikemon breeders are located on the same cell, exactlyTMikemon battles will ensue that time (since you will be battling each of thoseTBreeders once). After the battle, all of thoseTBreeders 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 wowould 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 particle 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:RAndC(1? ≤?R,?C? ≤? 1000), denoting the number of rows and the number of columns in biridian forest. The nextRRows 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 particle, 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 participant ipate in if you pick a strategy that minimize this number.

Sample test (s) Input
5 7000E0T3T0TT0T0010T0T02T0T0T00T0S000
Output
3
Input
1 4SE23
Output
2
Note

The following picture into strates the first example. The blue line denotes a possible sequence of moves that you shoshould 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 shocould 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 conducting CTED. After those battles, all of your opponents leave the forest.

Finally, you make another move by leaving the forest.



The N * m maze is given. t indicates that the tree cannot go. 0 indicates the open space. 1 ~ 9 represents the number of enemies on the square. From the start point S to the end point, find the minimum number of enemies that must be killed along the way. Idea: we can think like this: if an enemy on a square can meet me, it is equivalent to waiting for me at the end, if I reach the destination, some enemies will not be able to catch up with me, so I can take the shortest route, if the number of enemy steps is smaller than the number of my steps, the number of enemies will be added. I am using a s array. When a number is encountered during the BFS process, the number of steps to be changed is stored in the S array, after BFS is complete, traverse the entire graph. If s [I] [J] <my minimum number of steps, add the number of enemies for this point. BFS adopts reverse. Code:
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>#include <string>#include <map>#include <stack>#include <vector>#include <set>#include <queue>#pragma comment (linker,"/STACK:102400000,102400000")#define maxn 1005#define MAXN 2005#define mod 1000000009#define INF 0x3f3f3f3f#define pi acos(-1.0)#define eps 1e-6typedef long long ll;using namespace std;struct Node{    int x,y,step;};int s[maxn][maxn];char mp[maxn][maxn];bool vis[maxn][maxn];int dir[4][2]={0,1,0,-1,-1,0,1,0};int n,m,sx,sy,ex,ey,ans,shortest;bool ISok(int x,int y){    if (x>=0&&x<n&&y>=0&&y<m&&!vis[x][y]&&mp[x][y]!='T')        return true;    return false;}void bfs(){    int flag=0;    queue<Node>Q;    memset(vis,false,sizeof(vis));    Node st,now;    while (!Q.empty())        Q.pop();    st.x=sx,st.y=sy,st.step=0;    vis[sx][sy]=true;    Q.push(st);    while (!Q.empty())    {        st=Q.front();        Q.pop();        if (st.x==ex&&st.y==ey)        {            shortest=st.step;            return ;        }        for (int i=0;i<4;i++)        {            int dx=st.x+dir[i][0];            int dy=st.y+dir[i][1];            if (ISok(dx,dy))            {                if (isdigit(mp[dx][dy])&&mp[dx][dy]!='0')                    s[dx][dy]=st.step+1;                now.x=dx;                now.y=dy;                now.step=st.step+1;                vis[dx][dy]=true;                Q.push(now);            }        }    }    return ;}int main(){    while (~scanf("%d%d",&n,&m))    {        ans=0;        memset(s,0,sizeof(s));        for (int i=0;i<n;i++)        {            scanf("%s",mp[i]);            for (int j=0;j<m;j++)            {                if (mp[i][j]=='E')                    sx=i,sy=j;                if (mp[i][j]=='S')                    ex=i,ey=j;            }        }        bfs();//        printf("shortest=%d\n",shortest);        for (int i=0;i<n;i++)            for (int j=0;j<m;j++)                if (s[i][j]&&s[i][j]<=shortest)                    ans+=(mp[i][j]-'0');        printf("%d\n",ans);    }    return 0;}/*5 7000E0T3T0TT0T0010T0T02T0T0T00T0S0001 4SE23*/


Codeforces -- 192. div2.d biridian forest BFS

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.