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 r r rows and c c 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):
0^0
```cpp
#include<bits/stdc++.h>
using namespace std;
const int N=1007;
char a[N][N];
int n,m,dis[N][N],sx,sy,tx,ty,ans;
const int x_[4]={1,-1,0,0};
const int y_[4]={0,0,1,-1};
struct node{
int x,y,d;
};
queue<node>q;
void bfs(int tx,int ty){
q.push(node{tx,ty,0});
dis[tx][ty]=-1;
int x,y;
while(!q.empty()){
node now=q.front();q.pop();
for(int i=0;i<4;i++){
x=now.x+x_[i];
y=now.y+y_[i];
dis[x][y]=now.d+1;
}
}
}
int main(){
cin>>n>>m;
for(int i=1;i<=n;i++)scanf("%s",a[i]+1);
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++){
if(a[i][j]=='S'){sx=i;sy=j;}
if(a[i][j]=='E'){tx=i;ty=j;}
}
bfs(tx,ty);
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
if(dis[i][j]!=0&&dis[i][j]<=dis[sx][sy]&&a[i][j]>='0'&&a[i][j]<='9')ans+=a[i][j]-'0';
cout<<ans;
本文介绍了一个基于二维网格的迷宫寻路问题,利用BFS(宽度优先搜索)算法寻找从起点到终点的最短路径,并计算途中特定类型节点的数量。通过C++代码实现,展示了算法的具体应用过程。
3001

被折叠的 条评论
为什么被折叠?



