当前位置:网站首页>P1363 phantom maze (DFS)

P1363 phantom maze (DFS)

2022-06-23 04:19:00 Harris-H

P1363 Phantom maze (dfs)

If the point after the mold is taken is taken twice , Then there are rings .

Note that the two points must be different before taking the mold .

So we maintain the position of each point before taking the mold .

Then use the tag array to maintain whether the position has been passed .

And then each special judgment dfs that will do .

Time complexity : O ( n ∗ m ∗ 4 ) O(n*m*4) O(nm4)

Code reference solution

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull; 
const int N=1e3+5,M=2e4+5,inf=0x3f3f3f3f,mod=1e9+7;
const int hashmod[4] = {
    402653189,805306457,1610612741,998244353};
#define mst(a,b) memset(a,b,sizeof a)
#define db double
#define PII pair<int,int>
#define PLL pair<ll,ll>
#define x first
#define y second
#define pb emplace_back
#define SZ(a) (int)a.size()
#define rep(i,a,b) for(int i=a;i<=b;++i)
#define per(i,a,b) for(int i=a;i>=b;--i)
#define IOS ios::sync_with_stdio(false),cin.tie(nullptr) 
void Print(int *a,int n){
    
	for(int i=1;i<n;i++)
		printf("%d ",a[i]);
	printf("%d\n",a[n]); 
}
template <typename T>		//x=max(x,y) x=min(x,y)
void cmx(T &x,T y){
    
	if(x<y) x=y;
}
template <typename T>
void cmn(T &x,T y){
    
	if(x>y) x=y;
}
int main(){
    
	return 0;
}
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;

const int MAXN = 1500 + 1;
const int dx[4] = {
    1, -1, 0, 0};
const int dy[4] = {
    0, 0, 1, -1};

int n, m;
int st_x, st_y;
int vis[MAXN][MAXN][3];
bool fl, a[MAXN][MAXN];
char ch;

void dfs(int x, int y, int lx, int ly) {
    
	if(fl) return;
	if(vis[x][y][0] && (vis[x][y][1]!=lx || vis[x][y][2]!=ly)) {
    
		fl = 1;
		return;
	}
	vis[x][y][1] = lx, vis[x][y][2] = ly, vis[x][y][0] = 1;
	for(int i=0; i<4; ++i) {
    
		int xx = (x + dx[i] + n) % n, yy = (y + dy[i] + m) % m;
		int lxx = lx + dx[i], lyy = ly + dy[i];
		if(!a[xx][yy]) {
    
			if(vis[xx][yy][1]!=lxx || vis[xx][yy][2]!=lyy || !vis[xx][yy][0])
				dfs(xx, yy, lxx, lyy);
		}
	}
}
int main() {
    
	ios::sync_with_stdio(false);
	while(cin >> n >> m) {
    
		fl = 0;
		memset(a, 0, sizeof(a));
		memset(vis, 0, sizeof(vis));
		for(int i=0; i<n; ++i)
			for(int j=0; j<m; ++j) {
    
				cin >> ch;
				if(ch == '#') a[i][j] = 1;
				if(ch == 'S') st_x = i, st_y = j;
			}
		dfs(st_x, st_y, st_x, st_y);
		if(fl) puts("Yes");
		else puts("No");
	}
}
原网站

版权声明
本文为[Harris-H]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/174/202206222241035996.html