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.

一句话题解:所有人到终点等它即可。。。

完全没想到是有多弱?。。。。。

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<functional>
#include<iostream>
#include<cmath>
#include<cctype>
#include<ctime>
using namespace std;
#define For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define Rep(i,n) for(int i=0;i<n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define RepD(i,n) for(int i=n;i>=0;i--)
#define Forp(x) for(int p=pre[x];p;p=next[p])
#define Lson (x<<1)
#define Rson ((x<<1)+1)
#define MEM(a) memset(a,0,sizeof(a));
#define MEMI(a) memset(a,127,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define INF (2139062143)
#define F (100000007)
#define MAXN (1000+10)
long long mul(long long a,long long b){return (a*b)%F;}
long long add(long long a,long long b){return (a+b)%F;}
long long sub(long long a,long long b){return (a-b+(a-b)/F*F+F)%F;}
typedef long long ll;
int n,m,tx,ty,sx,sy;
char c[MAXN][MAXN];
bool b[MAXN][MAXN]={0};
int qx[MAXN*MAXN],qy[MAXN*MAXN],head=1,tail=1,d[MAXN][MAXN];
void bfs()
{
qx[1]=tx,qy[1]=ty;b[tx][ty]=1;
memset(d,127,sizeof(d));d[tx][ty]=0;
while (head<=tail)
{
int x=qx[head],y=qy[head];
if (1<x&&!b[x-1][y]) b[x-1][y]=1,d[x-1][y]=d[x][y]+1,qx[++tail]=x-1,qy[tail]=y;
if (x<n&&!b[x+1][y]) b[x+1][y]=1,d[x+1][y]=d[x][y]+1,qx[++tail]=x+1,qy[tail]=y;
if (1<y&&!b[x][y-1]) b[x][y-1]=1,d[x][y-1]=d[x][y]+1,qx[++tail]=x,qy[tail]=y-1;
if (y<m&&!b[x][y+1]) b[x][y+1]=1,d[x][y+1]=d[x][y]+1,qx[++tail]=x,qy[tail]=y+1;
head++;
} }
int main()
{
// freopen("Biridian Forest.in","r",stdin);
scanf("%d%d",&n,&m);
For(i,n) scanf("%s",c[i]+1);
For(i,n) For(j,m)
if (c[i][j]=='S') sx=i,sy=j;
else if (c[i][j]=='E') tx=i,ty=j;
else if (c[i][j]=='T') b[i][j]=1;
bfs();
/*
For(i,n)
{
For(j,m) cout<<d[i][j]<<' ';cout<<endl;
}
*/
int ans=0;
For(i,n) For(j,m)
if (d[i][j]<=d[sx][sy]&&isdigit(c[i][j])) ans+=c[i][j]-48;
cout<<ans<<endl; return 0;
}

CF 329B(Biridian Forest-贪心-非二分)的更多相关文章

  1. Codeforces Round #192 (Div. 1) B. Biridian Forest 暴力bfs

    B. Biridian Forest Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/329/pr ...

  2. [Codeforces Round #192 (Div. 2)] D. Biridian Forest

    D. Biridian Forest time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  3. 1118 Birds in Forest (25 分)

    1118 Birds in Forest (25 分) Some scientists took pictures of thousands of birds in a forest. Assume ...

  4. 【Codeforces 329B】Biridian Forest

    [链接] 我是链接,点我呀:) [题意] 题意 [题解] 找到出口到每个点的最短距离. 设你到出口的最短距离为temp 那么如果某个人到终点的距离<=temp,则他们肯定能遇到你 因为他们可以在 ...

  5. CF 161B Discounts(贪心)

    题目链接: 传送门 Discounts time limit per test:3 second     memory limit per test:256 megabytes Description ...

  6. CF 115B Lawnmower(贪心)

    题目链接: 传送门 Lawnmower time limit per test:2 second     memory limit per test:256 megabytes Description ...

  7. CF Soldier and Badges (贪心)

    Soldier and Badges time limit per test 3 seconds memory limit per test 256 megabytes input standard ...

  8. CF Anya and Ghosts (贪心)

    Anya and Ghosts time limit per test 2 seconds memory limit per test 256 megabytes input standard inp ...

  9. PAT A1118 Birds in Forest (25 分)——并查集

    Some scientists took pictures of thousands of birds in a forest. Assume that all the birds appear in ...

随机推荐

  1. 浅谈2-SAT(待续)

    2-SAT问题,其实是一个逻辑互斥问题.做了两道裸题之后仔细想来,和小时候做过的“有两个女生,如果A是女生,那么B一定不是女生.A和C性别相同,求A.B.C三人的性别.”几乎是一样的. 对于这道题我们 ...

  2. iOS 11开发教程(十四)iOS11应用代码添加视图

    iOS 11开发教程(十四)iOS11应用代码添加视图 如果开发者想要使用代码为主视图添加视图,该怎么办呢.以下将为开发者解决这一问题.要使用代码为主视图添加视图需要实现3个步骤. (1)实例化视图对 ...

  3. 每日踩坑 2018-12-25 【Unable to convert MySQL date/time value to System.DateTime】异常

    之前的公司一直是用 ORM ,所以底层的事情完全不用在意, 到了这家全是似乎全是 SQL 硬编码 所以也有机会重新去补补以前也没怎么写过的基本 ADO. 不过这两天总有自己写个简易ORM的想法在蠢蠢欲 ...

  4. POJ1743 Musical Theme [后缀数组+分组/并查集]

    Musical Theme Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 27539   Accepted: 9290 De ...

  5. hdu 1116 敌兵布阵(树状数组区间求和)

    题意: 给出一行数字,然后可以修改其中第i个数字,并且可以询问第i至第j个数字的和(i <= j). 输入: 首行输入一个t,表示共有t组数据. 接下来每行首行输入一个整数n,表示共有n个数字. ...

  6. HDU 5836 Rubik's Cube BFS

    Rubik's Cube 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5836 Description As we all know, Zhu is ...

  7. ant design的一些坑

    1.在本地修改ant design的某些样式可以生效,但在线上就失效了.比如collapse组件里的箭头图标在本地和在线上的类名有变化,本地类名,线上类名:箭头图标的svg样式在线上会自动添加一个内联 ...

  8. CentOS安装CLI

    #使用root账号 vim /etc/yum.repos.d/epel.repo [epel] name=epel baseurl=http://mirrors.sohu.com/fedora-epe ...

  9. [原创] 浅谈ETL系统架构如何测试?

    [原创] 浅谈ETL系统架构如何测试? 来新公司已入职3个月时间,由于公司所处于互联网基金行业,基金天然固有特点,基金业务复杂,基金数据信息众多,基金经理众多等,所以大家可想一下,基民要想赚钱真不容易 ...

  10. EditPlus(4.0.0.395)中文免激活绿色版

    EditPlus一套功能强大,可取代记事本的文字编辑器,拥有无限制的撤消与重做.英文拼字检查.自动换行.列数标记.搜寻取代.同时编辑多文件.全屏幕浏览功能.而它还有一个好用的功能,就是它有监视剪贴板的 ...