跳到正文

个人记录

弗拉迪尾刺

sjz_M7_mby约 1 分钟
原文
文章信息与版本

文章信息

发布
2025年8月6日
收录
2025年12月12日
更新
2026年9月27日
篇幅
约 1 分钟

版本

  1. v1当前
C++
#include<bits/stdc++.h>
using namespace std;
int r,c,n,vis[41][41];char mp[41][41];
int dx[4]={1,0,-1,0};
int dy[4]={0,1,0,-1};
struct node{
	int x,y,id;
};
queue<node> q;
bool flag=0;
void bfs(){
	while(!q.empty()){
		if(q.front().x==r&&q.front().y==c){
			cout<<q.front().id<<endl;
			return ;
		}
		for(int i=0;i<4;i++){
			int nx=q.front().x+dx[i];
			int ny=q.front().y+dy[i];
			if(nx>=1&&ny>=1&&nx<=r&&ny<=c&&vis[nx][ny]==0&&mp[nx][ny]=='.'){
				node p={nx,ny,q.front().id+1};
				q.push(p);
				vis[nx][ny]=1;
			}
		}
		q.pop();
	}
}
int main(){
	cin>>r>>c;
	for(int i=1;i<=r;i++){
		for(int j=1;j<=c;j++){
			cin>>mp[i][j];
		}
	}
	node a={1,1,1};
	q.push(a);
	bfs();
	return 0;
}
原文