CF330 DBiridian Forest BFS宽度搜索

来源:互联网 发布:linux系统排行榜 2016 编辑:程序博客网 时间:2024/05/22 14:49
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, 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.

Sample test(s)
input
5 7000E0T3T0TT0T0010T0T02T0T0T00T0S000
output
3
input
1 4SE23
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.


题解:
你可以这么想,如果我是攻击者,我只用到达出口等就好了。只要逃亡者离出口的步数大于等于某一块攻击者,那么就加上那块的数字。 所以此题用BFS就能解决。 当初我用了2次BFS,先算逃亡的人到出口的距离,再用这个距离当作深度从出口再进行一次BFS,搜数字。 其实只要一次BFS就好了的,直接从EXIT开始搜索,然后最后再进行判断,很多大牛的算法就是一次搜索。。
       当初数组开太小了。WA了不知道多少次了。。
自己的代码:
/* * @author ipqhjjybj * @date  20130720 * */#include <cstdio>#include <cstdlib>#include <algorithm>#include <iostream>#include <cstring>#include <vector>using namespace std;#define ll long long#define max(a,b) ((a)>(b)?(a):(b))#define min(a,b) ((a)<(b)?(a):(b))#define clr(x,k) memset(x,k,sizeof(x))struct node{    int x,y;    node(){};    node(int a,int b){        x=a,y=b;    };};node S,e;char s[1011][1011];int n,m;#define isattack(x) ('1'<=x&&x<='9')int xi[4]={0,0,1,-1};int yi[4]={1,-1,0,0};int visit[1011][1011];int abs(int a){    if(a>0)return a;    else return -a;}node stack[1100000];int B[1100000];int bfs(int xx,int yy,int deep){    if(abs(e.x-xx)+abs(e.y-yy)>deep) return 0;    int x,y;    node temp;    int st,ed;    st=0,ed=1;    clr(visit,0);    clr(B,0);    visit[xx][yy]=1;    stack[0]=node(xx,yy);    B[0]=0;    int bufa=0;    while(st<ed&&bufa<=deep){        bufa=B[st]+1;        temp = stack[st++];        for(int i=0;i<4;i++){            x=temp.x+xi[i],y=temp.y+yi[i];            if(!visit[x][y]&& 1<=x&&x<=n&&1<=y&&y<=m&&s[x][y]!='T'){                if(s[x][y]=='E')                    return bufa;                B[ed]=bufa;                stack[ed++]=node(x,y);                visit[x][y]=1;            }        }    }    return 0;}int bfs2(int xx,int yy,int deep){    int x,y;    node temp;    int st,ed;    st=0,ed=1;    clr(visit,0);    clr(B,0);    visit[xx][yy]=1;    stack[0]=node(xx,yy);    B[0]=0;    int bufa=0;    int ans=0;    while(st<ed&&bufa<=deep){        bufa=B[st]+1;        temp = stack[st++];        for(int i=0;i<4;i++){            x=temp.x+xi[i],y=temp.y+yi[i];            if(!visit[x][y]&& 1<=x&&x<=n&&1<=y&&y<=m&&s[x][y]!='T'){                if(bufa>deep)                    break;                if(isattack(s[x][y])){                    ans+=s[x][y]-'0';                }                B[ed]=bufa;                stack[ed++]=node(x,y);                visit[x][y]=1;            }        }    }    return ans;}int main(){    //freopen("330D.in","r",stdin);    scanf("%d %d",&n,&m);    getchar();    for(int i=1;i<=n;i++)        gets(s[i]+1);    for(int i=1;i<=n;i++)        for(int j=1;j<=m;j++){            if(s[i][j]=='S'){                S.x=i;                S.y=j;            }else if(s[i][j]=='E')                e.x=i,e.y=j;        }    int bufa=bfs(S.x,S.y,m*n*2);   // printf("bufa=%d\n",bufa);    int ans=bfs2(e.x,e.y,bufa);    printf("%d\n",ans);    return 0;}

附加一份别人的代码:
#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>#include<queue>using namespace std;const int dx[4]={-1,0,1,0},dy[4]={0,-1,0,1};struct Po{int x,y;};char ch[1005][1005];int n,m,sx,sy,ex,ey,d[1005][1005];queue<Po>q;void Bfs(){memset(d,-1,sizeof(d));q.push((Po){ex,ey});d[ex][ey]=0;while (!q.empty()){int xx=q.front().x,yy=q.front().y; q.pop();for (int i=0;i<4;i++){int x=xx+dx[i],y=yy+dy[i];if ((ch[x][y]=='S'||ch[x][y]>='0'&&ch[x][y]<='9')&&d[x][y]==-1){d[x][y]=d[xx][yy]+1;q.push((Po){x,y});}}}}int main(){scanf("%d%d\n",&n,&m);for (int i=1;i<=n;i++){scanf("%s",ch[i]+1);for (int j=1;j<=m;j++)if (ch[i][j]=='S') sx=i,sy=j;else if (ch[i][j]=='E') ex=i,ey=j;}Bfs();int ans=0;for (int i=1;i<=n;i++)for (int j=1;j<=m;j++)if (d[i][j]!=-1&&ch[i][j]!='S'&&ch[i][j]!='E'&&d[i][j]<=d[sx][sy])ans+=ch[i][j]-'0';printf("%d",ans);return 0;}