站外题!求条
  • 板块题目总版
  • 楼主eggy__party
  • 当前回复31
  • 已保存回复33
  • 发布时间2024/8/3 13:56
  • 上次更新2024/8/7 11:47:52
查看原帖
站外题!求条
1288110
eggy__party楼主2024/8/3 13:56

迷宫最小步数(弱化版)

输入格式

给定一个大小为n * m的迷宫,求出从起点到终点的最小步数,只能上下左右四个方向走(若起点无法到达终点则输出-1)【*代表通道、#代表墙壁】

输出格式

一个整数,代表从起点到终点的最小步数。

样例

Input 1

5 6
*****#
*##*##
*#**##
*#*#*#
******
1 1 5 3

Output 1

6

0<n,m<50

我的code\mathbb{code}

#include<bits/stdc++.h>
using namespace std;
int dx[4]= {0,1,0,-1},dy[4]= {1,0,-1,0};
bool vis[1001][1001];int z,b,c,d;
int cnt,n,m,ans=INT_MAX;
char a[1001][1001];
bool f(int x,int y) {
	return x>=0 and x<n and y>=0 and y<m and a[x][y]=='*' and !vis[x][y];
}
void dfs(int x,int y) {
	if(x==c-1 and y==d-1) {
		ans=min(ans,cnt);
        cnt=0;
		return ;
	}
	vis[x][y]=true;
	for(int i=0; i<4; i++) {
		int nx=x+dx[i];
		int ny=y+dy[i];
		if(f(nx,ny)) {
            cnt++;
			dfs(nx,ny);
		}
	}
	vis[x][y]=false;
}
int main() {
	cin>>n>>m;
	for(int i=0; i<n; i++) {
		for(int j=0; j<m; j++) {
			cin>>a[i][j];
			vis[i][j]=false;
		}
	}
    
    cin>>z>>b>>c>>d;
	dfs(z-1,b-1);
	cout<<ans;
	return 0;
}
2024/8/3 13:56
加载中...