当前位置:网站首页>P1363 幻象迷宫(dfs)
P1363 幻象迷宫(dfs)
2022-06-23 03:45:00 【Harris-H】
P1363 幻象迷宫(dfs)
取模后的点若被走了两次,则存在环。
注意走两次的点取模前必须不同。
因此我们对每个点维护一下取模前的位置。
然后用标记数组维护位置是否被走过。
然后每次特判一下dfs即可。
时间复杂度: O ( n ∗ m ∗ 4 ) O(n*m*4) O(n∗m∗4)
代码参考题解
#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");
}
}
边栏推荐
- JS Part 4
- How to solve the problem that the web page fails to log in after the easycvr service is started?
- 华为联机对战服务玩家快速匹配后,不同玩家收到的同一房间内玩家列表不同
- 支持在 Kubernetes 运行,添加多种连接器,SeaTunnel 2.1.2 版本正式发布!
- 新版kali切换最高账户
- 理想汽车×OceanBase:当造车新势力遇上数据库新势力
- linux下的开源数据库是什么
- Centos7 installing MySQL and configuring InnoDB_ ruby
- The tax software exits when it detects that it is a virtual machine. How to solve this problem?
- [pycharm] ide Eval resetter
猜你喜欢
随机推荐
Efficient remote office experience | community essay solicitation
AI video cloud vs narrowband HD, who is the favorite in the video Era
[tcapulusdb knowledge base] [list table] example code for deleting the data at the specified location in the list
粒子动画背景登录页面particles.js
svg d3.js生成tree树状图
【owt】owt-client-native-p2p-e2e-test vs2017构建2 :测试单元构建及运行
Source code encryption of data encryption technology
京东云分布式数据库StarDB荣获中国信通院 “稳定性实践先锋”
Insert sort directly
【LeetCode】23. Merge K ascending linked lists
纳瓦尔宝典:不靠运气致富的原则
redis 精讲系列介绍八 - 淘汰策略
How can I realize video call and interactive live broadcast in a small program?
Black horse PostgreSQL, why is it black in the end
mysql 数据恢复 (.ibdata1, bin log)
What is the digital "true" twin? At last someone made it clear!
软件项目管理 8.4.软件项目质量计划
2022年的软件开发:首席信息官应该知道的五个现实
How to implement collection sorting?
The tax software exits when it detects that it is a virtual machine. How to solve this problem?









