codeforce 329B 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

就是求哪些大于0小于10的点到E的最小步数小于S到E的最小步数,如果是,答案加上那一点的数字

直接bfs

这里注意记录x,y数组的大小


#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>

#define L(x) (x<<1)
#define R(x) (x<<1|1)
#define MID(x,y) ((x+y)>>1)

#define eps 1e-8
using namespace std;
#define N 1005

char a[N][N];
int n,m;
int vis[N][N];

struct stud{
int time,x,y;
};

int le[N*N],ri[N*N];
int k;

int step[4][2]={0,1,0,-1,-1,0,1,0};

int judge(int x,int y)
{
	if(x>=0&&x<n&&y>=0&&y<m)
		return 1;
	return 0;
}

void bfs(int sx,int sy)
{
	int i,j;
	memset(vis,0,sizeof(vis));
    vis[sx][sy]=1;
	struct stud cur,next;
	queue<stud>q;
	cur.x=sx;
	cur.y=sy;
	cur.time=0;
	q.push(cur);
	while(!q.empty())
	{
		cur=q.front();
		q.pop();
		for(i=0;i<4;i++)
		{
			int xx=cur.x+step[i][0];
			int yy=cur.y+step[i][1];
			if(judge(xx,yy)&&!vis[xx][yy]&&a[xx][yy]!='T')
			{
				vis[xx][yy]=cur.time+1;
				next.x=xx;
				next.y=yy;
				next.time=cur.time+1;
				q.push(next);
			}
		}
	}
}

int main()
{
	int i,j;
	int sx,sy;
	while(~scanf("%d%d",&n,&m))
	{
		k=0;
		for(i=0;i<n;i++)
		{
			scanf("%s",a[i]);
			for(j=0;j<m;j++)
			{
				if(a[i][j]=='E')
				{
					sx=i;
					sy=j;
				}
				else
				 if(a[i][j]=='S')
				{
					le[0]=i;
					ri[0]=j;
				}
				else
					if(a[i][j]>'0'&&a[i][j]<='9')
				{
					le[++k]=i;
					ri[k]=j;
				}
			}
		}
		bfs(sx,sy);
		int ans=0;
		for(i=1;i<=k;i++)
			if(vis[le[i]][ri[i]]<=vis[le[0]][ri[0]]&&vis[le[i]][ri[i]])
			 ans+=a[le[i]][ri[i]]-'0';
		printf("%d\n",ans);
	}
	 return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值