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
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即可。

最后注意统计敌人数量的时候有敌人的地方必须之前遍历过才可以。测试的时候没有注意这一点一直WA。

Test: #5, time: 0 ms., memory: 24656 KB, exit code: 0, checker exit code: 1, verdict: WRONG_ANSWER
Input
1 10
9T9TSET9T9
Output
36
Answer
0
Checker Log
wrong answer expected '0', found '36'

题目地址:http://codeforces.com/contest/330/problem/D

代码:

 #include<stdio.h>
#include<string.h>
#include<stdbool.h>
int i,j,n,m,s1,s2,t1,t2,head,tail,xx,yy,sum,
dist[][],
di[][],
q[][],
dx[]={,,,-},
dy[]={,-,,}; bool can[][]; char a[]; int
pre()
{
memset(can,false,sizeof(can));
memset(di,,sizeof(di));
memset(dist,,sizeof(dist));
return ;
} int
init()
{
scanf("%d%d\n",&n,&m);
for(i=;i<=n;i++)
{
scanf("%s",&a);
for(j=;j<m;j++)
{
if(a[j]=='E')
{
t1=i;
t2=j;
}
if(a[j]=='S')
{
s1=i;
s2=j;
can[i][j]=true;
}
if((a[j]>='')&&(a[j]<=''))
{
can[i][j]=true;
di[i][j]=a[j]-'';
}
}
} return ;
} int
main()
{
pre();
init();
head=;tail=;
q[][]=t1;
q[][]=t2; while(head!=tail)
{
head=head%+;
for(i=;i<;i++)
{
xx=dx[i]+q[head][];
yy=dy[i]+q[head][];
if (can[xx][yy])
{
tail=tail%+;
q[tail][]=xx;
q[tail][]=yy;
dist[xx][yy]=dist[q[head][]][q[head][]]+;
can[xx][yy]=false;
}
}
} for(i=;i<=n;i++)
for(j=;j<m;j++)
{
if((!can[i][j])&&(di[i][j]>)&&(dist[i][j]<=dist[s1][s2]))
sum+=di[i][j];
} printf("%d\n",sum);
return ;
}

[Codeforces Round #192 (Div. 2)] D. 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. 1) C. Graph Reconstruction 随机化

    C. Graph Reconstruction Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/3 ...

  3. Codeforces Round #192 (Div. 1) A. Purification 贪心

    A. Purification Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/329/probl ...

  4. Codeforces Round #461 (Div. 2) B. Magic Forest

    B. Magic Forest time limit per test 1 second memory limit per test 256 megabytes Problem Description ...

  5. Codeforces Round #192 (Div. 2) (330B) B.Road Construction

    题意: 要在N个城市之间修建道路,使得任意两个城市都可以到达,而且不超过两条路,还有,有些城市之间是不能修建道路的. 思路: 要将N个城市全部相连,刚开始以为是最小生成树的问题,其实就是一道简单的题目 ...

  6. Codeforces Round #192 (Div. 2) (330A) A. Cakeminator

    题意: 如果某一行没有草莓,就可以吃掉这一行,某一列没有也可以吃点这一列,求最多会被吃掉多少块蛋糕. //cf 192 div2 #include <stdio.h> #include & ...

  7. Codeforces Round #192 (Div. 2)

    吐槽一下,这次的CF好简单啊. 可是我为什么这么粗心这么大意这么弱.把心沉下来,想想你到底想做什么! A 题意:O(-1) 思路:O(-1) #include <iostream> #in ...

  8. Codeforces Round #192 (Div. 2) A. Cakeminator

    #include <iostream> #include <vector> using namespace std; int main(){ int r,c; cin > ...

  9. Codeforces Round #192 (Div. 2) B. Road Construction

    #include <iostream> #include <vector> using namespace std; int main(){ int n,m; cin > ...

随机推荐

  1. CCI_chapter 3 Stacks and Queues

    3.1Describe how you could use a single array to implement three stacks for stack 1, we will use [0, ...

  2. 那年曾让我哭笑不得抓狂的C语言

    1.关于+=以及-= 这是两个运算符,但你否有过这种经历: int temp; char i ;i<MAX;i++) { ... temp=+; //这里本意是每次循环,temp都自增2,但是却 ...

  3. logstash 发送zabbix 给消息加上type

    input { file { type => "zj_api" path => ["/data01/applog_backup/zjzc_log/zj-api ...

  4. js深入研究之初始化验证

    <script type="text/javascript"> var Book = function(isbn, title, author) { if(!this. ...

  5. Linux企业级项目实践之网络爬虫(18)——队列处理

    所有的URL都接受管理,并在此进行流动.URL从管理模块的存储空间开始,一直到最后输出给磁盘上的URL索引,都由此部分调度.首先,给出URL调度的一般过程,如图所示.其流程的各个具体操作,后面详述.要 ...

  6. jsDelivr - Free open source CDN for javascript libraries, jQuery plugins, CSS frameworks, Fonts and more

    jsDelivr - Free open source CDN for javascript libraries, jQuery plugins, CSS frameworks, Fonts and ...

  7. 关于maven-jetty-plugin 自动重启问题

    <plugin> <groupId>org.mortbay.jetty</groupId> <artifactId>maven-jetty-plugin ...

  8. C++中的重载、覆盖、隐藏

    前几天面试时被问及C++中的覆盖.隐藏,概念基本答不上来,只答了怎么用指针实现多态,也还有遗漏.最终不欢而散.回来后在网上查找学习了一番,做了这个总结.其中部分文字借用了别人的博客,望不要见怪.引用的 ...

  9. 对easyui datagrid进行扩展,当滚动条拉直最下面就异步加载数据。

    以下方法是通用的,只要把datagrid定义为全局的即可,其他部分的代码不用进行修改! 可以把以下代码放入到一个单独的js文件,然后再需要的页面引入即可! $(function(){ try{ $(& ...

  10. mount USB Device(U disk) on crux based on vmware

    1. 在 /mnt 下建立一个名叫USB的文件夹,文件夹名自定 cd /mnt mkdir USB 2. 查看一下磁盘分区情况 fdisk –l 3. 插入U盘 4. 再次查看磁盘分区情况,对比第一次 ...