站外题求调
  • 板块灌水区
  • 楼主i_lovo_myword
  • 当前回复4
  • 已保存回复4
  • 发布时间2024/7/10 17:11
  • 上次更新2024/7/10 17:19:14
查看原帖
站外题求调
1381563
i_lovo_myword楼主2024/7/10 17:11

某本通1252,代码30分

#include<bits/stdc++.h>
using namespace std;
struct node{
	int x,y,step;
};
char mp[50][50];
int mk[50][50],n,m;
int dx[4]={1,-1,0,0};
int dy[4]={0,0,1,-1};
void bfs(int x,int y){
	queue<node> q;
	q.push((node){x,y,1});
	mk[x][y]=1;
	while(q.size()){
		node t=q.front();  
		q.pop();
		for(int i = 0;i < 4;i++){
			int xx=t.x+dx[i];
			int yy=t.y+dy[i];
			if(xx==n&&yy==m){
				cout << t.step+1;
				return;
			}
			if(xx>=1&&xx<=n&&yy>=1&&yy<=m&&!mk[xx][yy]&&mp[xx][yy]=='.'){
				mk[xx][yy] = 1;
				q.push((node){xx,yy,t.step+1});
			}
		}
	}
}
int main() {
	cin >> m >> n;        
	for(int i = 1;i <= n;i++){
		for(int j = 1;j <= m;j++) {
			cin >>  mp[i][j];
		}
	}
	if(n==1 and m==1){
		cout<<1;
		return 0;
	}
	bfs(1,1);
	return 0;
}
2024/7/10 17:11
加载中...