只输出“No”,求调
查看原帖
只输出“No”,求调
602793
CW_shy楼主2023/7/23 11:34
#include<bits/stdc++.h>
using namespace std;
#define ll long long
ll h,w;
char a[505][505];
char s[10]={'s','n','u','k','e'};
bool vis[505][505];
ll dx[4]={0,0,1,-1},dy[4]={1,-1,0,0};
queue<ll>qx;
queue<ll>qy;
queue<ll>qstep;
int main()
{
	scanf("%lld%lld",&h,&w);
	for(int i=1;i<=h;i++)
	{
		for(int j=1;j<=w;j++) scanf("%c",&a[i][j]);
	}
	if(a[1][1]!='s')
	{
		cout<<"No";
		return 0;
	}
	qx.push(1),qy.push(1),qstep.push(0);
	vis[1][1]=1;
	while(!qx.empty())
	{
		ll x=qx.front(),y=qy.front(),xstep=qstep.front();
		qx.pop();
		qy.pop();
		qstep.pop();
		if(x==h&&y==w)
		{
			cout<<"Yes";
			return 0;
		}
		for(int i=0;i<4;i++)
		{
			ll xx=x+dx[i],yy=y+dy[i];
			if(xx>h||xx<1||yy>w||yy<1||vis[xx][yy]||a[xx][yy]!=s[(xstep+1)%5]) continue;
			vis[xx][yy]=1;
			qx.push(xx);
			qy.push(yy);
			qstep.push((xstep+1)%5);	
		} 
	}
	cout<<"No";
}
2023/7/23 11:34
加载中...