社区讨论

第八个点第二组数据??

P1363幻象迷宫参与者 3已保存回复 6

讨论操作

快速查看讨论及其快照的属性,并进行相关操作。

当前回复
6 条
当前快照
1 份
快照标识符
@mi6xa230
此快照首次捕获于
2025/11/20 12:19
4 个月前
此快照最后确认于
2025/11/20 12:19
4 个月前
查看原帖
求问:
CPP
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
using namespace std;

const int maxn=15e2+10;
int dx[4]={0,1,0,-1};
int dy[4]={1,0,-1,0};
struct que
{
	int x,y;
}q[maxn*maxn];
bool map1[maxn][maxn];
int n,m,sx,sy;
int vis[maxn][maxn][3];

bool bfs()
{
	int h=0,t=1;
	q[t].x=sx;
	q[t].y=sy;
	vis[sx][sy][0]=1;
	vis[sx][sy][1]=sx;
	vis[sx][sy][2]=sy;
	while(h<t)
	{
		h++;
	//	cout<<q[h].x<<" "<<q[h].y<<endl;
		for(int i=0;i<4;i++)
		{
			int nx=q[h].x+dx[i];
			int ny=q[h].y+dy[i];
			int x=(nx-1+n)%n+1;
			int y=(ny-1+m)%m+1;
		//	cout<<nx<<" "<<ny<<" "<<x<<" "<<y<<endl;
			if(map1[x][y])
			{
				if(!vis[x][y][0])
				{
					vis[x][y][0]=1;
					vis[x][y][1]=nx;
					vis[x][y][2]=ny;
					q[++t].x=nx;
					q[t].y=ny;
				}
				else if(vis[x][y][1]!=nx||vis[x][y][2]!=ny)
				{
				//	cout<<nx<<" "<<ny<<" "<<x<<" "<<y<<endl;
					return true;
				}
				
			}
		}
	}
	return false;
}
int main()
{
	while(cin>>n>>m)
	{
		memset(vis,0,sizeof(vis));
		for(int i=1;i<=n;i++)
		{
			for(int j=1;j<=m;j++)
			{
				char ch;
				cin>>ch;
				if(ch=='#')map1[i][j]=false;
				else 
				{
					map1[i][j]=true;
					if(ch=='S')sx=i,sy=j;
				}
			}
		}
		if(bfs())printf("Yes\n");
		else printf("No\n");
	}
	return 0;
}


回复

6 条回复,欢迎继续交流。

正在加载回复...