Codeforces--192.div2.D Biridian Forest bfs

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 7
000E0T3
T0TT0T0
010T0T0
2T0T0T0
0T0S000

output

3

input

1 4
SE23

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.

题意:给出n*m的迷宫,T代表树不能走,0代表空地,1~9代表该方格上有的敌人数,从起点S出发到终点,求一路上必须得干掉的敌人的最小值。

思路:我们可以这样想,如果某个方格上的敌人能够与我相遇,那就相当于这些敌人到终点去等我,这是等价的,如果我到终点了有些敌人还没到终点那敌人就不可能追上我了,所以我走最短的路线就可以了,如果敌人走的步数小于我的步数就加上敌人数量。我用的一个s数组,bfs过程中遇到数字就把改点的步数存在s数组中,bfs完了以后遍历全图若s[i][j]<我的最小步数,就加上该点的敌人数。bfs采用反向的。

代码:

#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-6
typedef 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 7
000E0T3
T0TT0T0
010T0T0
2T0T0T0
0T0S000
1 4
SE23
*/
时间: 2024-08-05 19:56:57

Codeforces--192.div2.D Biridian Forest bfs的相关文章

codeforces#327 div2

codeforces#327 div2 这场状态不好有点可惜,题目都不难,而且很好.. A题:水题. #include<bits/stdc++.h> #define REP(i,a,b) for(int i=a;i<=b;i++) #define MS0(a) memset(a,0,sizeof(a)) #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 using namespace std; typedef lo

codeforce 329B Biridian Forest

F - Biridian Forest Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Submit Status Description You're a mikemon breeder currently in the middle of your journey to become a mikemon master. Your current obstacle is go throu

Codeforces 583 DIV2 Robot&#39;s Task 贪心

原题链接:http://codeforces.com/problemset/problem/583/B 题意: 就..要打开一个电脑,必须至少先打开其他若干电脑,每次转向有个花费,让你设计一个序列,使得总花费最小. 题解: 就傻傻的走就好..从左走到右,再走回来,更新序列和答案就好. 代码: #include<iostream> #include<cstring> #include<algorithm> #include<cstdio> #define MA

Codeforces #180 div2 C Parity Game

// Codeforces #180 div2 C Parity Game // // 这道题的题目意思就不解释了 // // 题目有那么一点难(对于我而言),不多说啦 // // 解题思路: // // 首先如果a串和b串相等,不多说直接YES // 如果b串全是0,直接YES // 注意到a串有一个性质,1的个数不会超过本身的加1. // a有个1的上限设为x,b有个1的个数设为y,则如果x < y // 那么直接NO. // // 现在一般情况下,就是模拟啦,找到a的后缀和b的前缀一样的

Codeforces #246(div2)

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <ti

Codeforces #245(div2)

A:A. Points and Segments (easy) 题目看了n久,开始觉得尼玛这是div2的题目么,题目还标明了easy.. 意思是给你一n个点,m个区间,在n个点上放蓝球或者红球,然后让你找一种选择方案使得m个区间内的蓝球和红球数量之差不超过1. 开始想过用dfs,不过这只是div2的A题而已.. 然后想了下,直接输出010101序列不就可以么. 交了一发,发现要先排个序,再输出就可以了. AC代码: #include<iostream> #include<cstdio&g

codeforces#FF(div2) DZY Loves Sequences

n个数,可以任意改变其中一个数,求最长的上升子区间长度 思路:记录一个from[i]表示从位置i的数开始最长的上升区间长度 记录一个to[i]表示到位置i的数所能达到的最长上升区间长度 枚举要改变的数的位置i,此时能达到的长度为to[i - 1] + from[i + 1] + 1,取最大值 //#pragma comment(linker, "/STACK:102400000,102400000") //HEAD #include <cstdio> #include &l

Codeforces 258 Div2

A题,n*m根木棍,相交放置,轮流取走相交的两根,最后谁不能行动,则输掉. min(n,m)&1 为1则先取者赢. B题,给定一个长度为n,且各不相同的数组,问能否通过交换连续一段L....R使得变成单调递增. 如果一开始就是递增的,那么直接输出L...R就是1 1,交换一个就行了:否则判断中间是否有且一段单调递减,且两端交换后使得数组递增. 代码: 1 //Template updates date: 20140718 2 #include <iostream> 3 #include

Codeforces Gym 100187E E. Two Labyrinths bfs

E. Two Labyrinths Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100187/problem/E Description A labyrinth is the rectangular grid, each of the cells of which is either free or wall, and it's possible to move only between free