救救孩子我吧《P3395 路障》玄关
  • 板块P3395 路障
  • 楼主Apollo0130
  • 当前回复1
  • 已保存回复1
  • 发布时间2024/7/5 11:41
  • 上次更新2024/7/5 14:18:46
查看原帖
救救孩子我吧《P3395 路障》玄关
973280
Apollo0130楼主2024/7/5 11:41

RE4个 WA1个

#include<bits/stdc++.h>
using namespace std;
const int N=1005;
queue<int>q1;
queue<int>q2;
int g[N][N];
int lx[N],ly[N];
bool st[N][N],f;
int dx[5]={-1,0,1,0};
int dy[5]={0,1,0,-1};
int n,j;
int tx,ty;

void bfs(int x,int y){
	q1.push(x);
	q2.push(y);
	for(int z=1;q1.size();z++){
		for(int i=0;i<4;i++){
			tx=q1.front()+dx[i];
			ty=q2.front()+dy[i];
			if(tx<1 || tx>n || ty<1 || ty>n)continue;
			if(!st[tx][ty] && !g[tx][ty]){
				q1.push(tx);
				q2.push(ty);
				st[tx][ty]=1;
			}
			g[lx[++j]][ly[j]]=1;
//			for(int i=1;i<=n;i++){
//				for(int j=1;j<=n;j++){
//					cout<<g[i][j];
//				}
//				cout<<endl;
//			}
//			cout<<endl;
			if(tx==n && ty==n){
				f=1;
				return;
			}
		}
		if(f)break;
		q1.pop();
		q2.pop();
	}
	return;
}

int main(){
	int t;
	cin>>t;
	while(t--){
		f=0;
		while(!q1.empty())q1.pop(),q2.pop();
		memset(g,0,sizeof(g));
		memset(st,0,sizeof(st));
		memset(lx,0,sizeof(lx));
		memset(ly,0,sizeof(ly));
		cin>>n;
		for(int i=1;i<=n;i++){
			cin>>lx[i]>>ly[i];
		}	
		st[1][1]=1;
		bfs(1,1);
		if(f)cout<<"YES";
		else cout<<"NO";
	}
}
2024/7/5 11:41
加载中...