ans

liujunhao 2024-02-02 13:59:45 16 返回题目

#include<bits/stdc++.h>

using namespace std;

char a[2000][2000];

struct Step{

int x,y,s;

}; const int dir[4][2]={{-1,0},{1,0},{0,-1},{0,1}};

bool vis[2000][2000];

int n;

queueq;

void solve(){

cin>>n;
for(int i=1;i<=n;i++){
	for(int j=1;j<=n;j++){
		cin>>a[i][j];
	}
}
while(!q.empty())q.pop();
memset(vis, false, sizeof(vis));
q.push({1,1,0});vis[1][1]=true;
while(!q.empty()){
	int x=q.front().x;
	int y=q.front().y;
	int s=q.front().s;q.pop();
	if(x==n&&y==n){
		cout<<"YES"<<"\n";
		return ;
	}
	for(int k=0;k<4;k++){
		int _x=x+dir[k][0];
		int _y=y+dir[k][1];
		if(_x>=1&&x<=n&&_y>=1&&_y<=n&&!vis[_x][_y]&&a[_x][_y]!='#'){
			vis[_x][_y]=true;
			q.push({_x,_y,s+1});
		}
	}
}
cout<<"NO"<<"\n";

} int main(){

int t;
cin>>t;
while(t--)solve();
return 0;

}

{{ vote && vote.total.up }}