codeforce 329B Biridian Forest(bfs)

在这个充满挑战的Biridian森林中,您作为Mikemon育种者,需要通过制定策略来最小化与敌对育种者的Mikemon战斗次数,最终成功逃脱。每一步都至关重要,不仅需要考虑路径的最短距离,还要避免不必要的冲突。通过了解森林布局、Mikemon的位置和行为模式,您可以设计出最优的逃脱路线。

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;
}


### Codeforces 题目难度分布及评级标准 Codeforces 的题目难度范围广泛,涵盖了从新手到专家级别的各种挑战[^1]。该平台通过细致的分层机制来区分不同难度等级的问题,使得每位参赛者都能找到适合自己水平的任务。 #### 评分体系概述 Codeforces 使用基于积分制的评价系统,其中每道题都有一个对应的分数(rating),用于表示其相对难易程度。较低的 rating 表明这是一道较为简单的题目;而较高的 rating 则意味着更高的复杂性和解决难度。通常情况下: - **简单题**:Rating 小于等于 1200 分 - **中等偏难题**:Rating 范围大约在 1600 至 2000 分之间 - **困难题**:Rating 大于等于 2400 分 这些数值并不是固定的界限,而是根据社区反馈以及比赛实际情况调整的结果。 #### Divisions 和 Contest Types 为了更好地适应不同程度的学习者和技术爱好者的需求,Codeforces 提供了多种类型的竞赛活动,比如 Division 1, Division 2, Division 3 及 Division 4 等不同类型的比赛[^2]。每个 division 对应着不同的最低准入门槛——即参与者应该具备的基础能力或经验水平。例如,在更高级别的比赛中会遇到更多高难度的问题。 #### 技术领域覆盖 平台上发布的题目不仅限于单一的技术方向,还涉及到多个计算机科学的重要分支,如贪心算法、动态规划、图论等领域。这种多样性有助于全面锻炼编程技巧并促进跨学科思维的发展。 ```python # 示例 Python 代码片段展示如何获取某场比赛的信息 import requests def get_contest_info(contest_id): url = f"https://codeforces.com/api/contest.standings?contestId={contest_id}&from=1&count=1" response = requests.get(url).json() if 'result' not in response or 'problems' not in response['result']: return None problems = response['result']['problems'] for problem in problems: print(f"{problem['index']}: {problem['name']} - Rating: {problem.get('rating', 'N/A')}") get_contest_info(1669) ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值