40分大佬求助,改下代码吧
  • 板块B3625 迷宫寻路
  • 楼主douBsh
  • 当前回复4
  • 已保存回复4
  • 发布时间2023/6/3 22:20
  • 上次更新2023/10/23 13:56:30
查看原帖
40分大佬求助,改下代码吧
383128
douBsh楼主2023/6/3 22:20
#include<bits/stdc++.h>
using namespace std;
int n,x,y,a[1145][1145];
string whole_map[1145][1145];
int map_x,map_y;
int last_x,last_y;
int next_step[4][2]={{0,1},{1,0},{0,-1},{-1,0}};
bool flag[1145][1145];
bool can_be_found;
void dfs(int x,int y,int step)
{
	int next_x,next_y;
	if(x==last_x&&y==last_y)
	{
		can_be_found=1;
		return;
	}
	for(int i=0;i<=3;i++)
	{
		next_x=x+next_step[i][0];
		next_y=y+next_step[i][1];
		if(next_x<1||next_x>map_x||next_y<1||next_y>map_y)
		{
			continue;
		}
		if(a[next_x][next_y]==0&&flag[next_x][next_y]==0)
		{
			flag[next_x][next_y]=1;
			dfs(next_x,next_y,step+1);
			flag[next_x][next_y]=0;
		}
	}
	return; 
}
int main()
{
	cin>>map_x>>map_y;
	last_x=map_x;
	last_y=map_y;
	for(int i=1;i<=map_x;i++)
	{
		for(int j=1;j<=map_y;j++)
		{
			cin>>whole_map[i][j];
			if(whole_map[i][j]==".")
			{
				a[i][j]=0;
			}
			else
			{
				a[i][j]=1;
			}
		}
	}
	flag[1][1]=1;
	dfs(1,1,0);
	if(can_be_found==1)
	{
		cout<<"Yes";
	}
	else
	{
		cout<<"No";
	}
	return 0;
}
2023/6/3 22:20
加载中...