不是,怎么就40分了(我用的广搜,虽然它的标签是深搜)
查看原帖
不是,怎么就40分了(我用的广搜,虽然它的标签是深搜)
1213524
C_plus_plus_12345楼主2024/12/20 20:45
#include<bits/stdc++.h>
using namespace std;
int u[5]={0, 0, 1, 0, -1},
    w[5]={0, 1, 0, -1, 0};
int n, m, desx, desy, soux, souy, head, tail, x, y, a[101], b[101], pre[101], _map[101][101], sum;
char _map2[101][101];
bool f;
void out(int d)
{
	if(pre[d]!=0)
	{
		out(pre[d]);
	}
	printf("%d, %d\n", a[d], b[d]);
	++sum;
}
int main()
{
	cin>>n>>m;
	for(int i=1;i<=n;i++)
	{
		for(int j=1;j<=m;j++)
		{
			cin>>_map2[i][j];
			if(_map2[i][j]=='.')
			{
				_map[i][j]=0;
			}
			else if(_map2[i][j]=='#')
			{
				_map[i][j]=-1;
			}
		}
	}
	soux = 1; souy = 1;
	desx = n; desy = m;
	head=0;tail=1;f=false;_map[soux][souy]=-1;
	a[tail]=soux;b[tail]=souy;pre[tail]=0;
	while(head!=tail)
	{
		head++;
		for(int i=1;i<=4;i++)
		{
			x=a[head]+u[i];y=b[head]+w[i];
			if((x>0)&&(x<=n)&&(y>0)&&(y<=m)&&_map[x][y]==0)
			{
				tail++;
				a[tail]=x;b[tail]=y;pre[tail]=head;
				_map[x][y]=-1;
				if((x==desx)&&(y==desy))
				{
					f=true;
					out(tail);
					break;
				}
			}
		}
		if(f)
		{
			cout<<"Yes"<<endl;
			break;
		}
	}
	if(!f)
	{
		cout<<"No"<<endl;
	}
	return 0;
}
2024/12/20 20:45
加载中...