P11580 [CCC2020] Escape Room

洛谷

发现直接从起点开始不好处理,可以考虑从终点倒过来处理。

假设目前在位置 \((x,y)\) 那么下一次一定是选择值为 \(x\times y\) 的区域。

那么我们开按照当前区域的值存下这个区域的位置,使用广搜处理,记录每一个值是否加入过队列,防止重复。

时间复杂度为 \(O(n^2)\),可以通过。

代码:

#include<bits/stdc++.h>
using namespace std;
int m,n;
bool vis[1000005];
vector<pair<int,int>> e[1000005];
bool bfs(int sx,int sy){
	queue<pair<int,int>> q;
	q.push({sx,sy});
	while(!q.empty()){
		pair<int,int> u=q.front();
		q.pop();
		int tmp=u.first*u.second;
		if(tmp==1)return true;
		if(vis[tmp])continue;
		vis[tmp]=1;
		for(auto i:e[tmp])q.push(i);
	}
	return false;
}
signed main(){
	cin>>m>>n;
	for(int i=1;i<=m;i++){
		for(int j=1,x;j<=n;j++){
			cin>>x;
			e[x].push_back({i,j});
		}
	}
	if(bfs(m,n))cout<<"yes";
	else cout<<"no";
	return 0;
}
posted @ 2025-12-07 12:42  huhangqi  阅读(2)  评论(0)    收藏  举报