现在的位置: 首页 > 综合 > 正文

Codeforces Round #192 (Div. 2) D. Biridian Forest (水bfs)

2013年10月13日 ⁄ 综合 ⁄ 共 4654字 ⁄ 字号 评论关闭

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

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 7
000E0T3
T0TT0T0
010T0T0
2T0T0T0
0T0S000
output
3
input
1 4
SE23
output
2
题目链接:http://codeforces.com/contest/330/problem/D

这题真的是满水的  可惜我一开始把题意理解错了  衰。。。!我以为每次战斗还需要花时间。。。
真的形成了思维定式啊。。。
每次战斗不需要花时间的话  那就只需要从终点出发bfs()就够了  到起点那层的下一层结束就够了。

代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <cmath>
#include <stack>
#include <map>
#include <queue>
#define maxn 1005
using namespace std;

const int INF=0x3f3f3f3f;
int n,m,ans;
int sx,sy,ex,ey;
int mp[maxn][maxn];
int dx[]={-1,1,0,0};
int dy[]={0,0,-1,1};
bool vis[maxn][maxn];
char s[maxn];
struct Node
{
    int x,y;
    int step;
}cur,now,q[maxn*maxn];

void bfs()
{
    int i,j,depth,nx,ny,nstep,tx,ty;
    int head=0,tail=-1;
    memset(vis,0,sizeof(vis));
    depth=INF;
    ans=0;
    cur.x=ex;
    cur.y=ey;
    cur.step=0;
    vis[ex][ey]=1;
    q[++tail]=cur;
    while(head<=tail)
    {
        now=q[head];
        nx=now.x;
        ny=now.y;
        nstep=now.step;
        if(nx==sx&&ny==sy) depth=nstep;
        if(nstep>depth) break ;
        ans+=mp[nx][ny];
        for(i=0;i<4;i++)
        {
            tx=nx+dx[i];
            ty=ny+dy[i];
            if(tx>=1&&tx<=n&&ty>=1&&ty<=m&&mp[tx][ty]<INF&&!vis[tx][ty])
            {
                vis[tx][ty]=1;
                cur.x=tx;
                cur.y=ty;
                cur.step=nstep+1;
                q[++tail]=cur;
            }
        }
        head++;
    }
}
int main()
{
    int i,j,cnt1,cnt2;
    while(~scanf("%d%d",&n,&m))
    {
        for(i=1; i<=n; i++)
        {
            scanf("%s",s);
            for(j=1; j<=m; j++)
            {
                if(s[j-1]=='T') mp[i][j]=INF;
                else if(s[j-1]>='0'&&s[j-1]<='9')
                {
                    mp[i][j]=s[j-1]-'0';
                }
                else if(s[j-1]=='S')
                {
                    mp[i][j]=0;
                    sx=i;
                    sy=j;
                }
                else if(s[j-1]=='E')
                {
                    mp[i][j]=0;
                    ex=i;
                    ey=j;
                }
            }
        }
        bfs();
        printf("%d\n",ans);
    }
    return 0;
}

抱歉!评论已关闭.