博客
关于我
Codeforces Round #644 (Div. 3) E. Polygon(思维/dfs)
阅读量:315 次
发布时间:2019-03-04

本文共 1383 字,大约阅读时间需要 4 分钟。


在这里插入图片描述

首先不难发现发出的1肯定是先出现边界,然后1出现在和边界相连的1,一次向上或向左连接成连通块,那么我们考虑第一种做法:从所有的边界存在1的地方搜索,然后对连通块上的1进行标记。最后对整个 n × m n×m n×m的方阵中所有的1如果存在不被标记的就是NO,否则为YES

还有一种简单的方法:只需看除了边界的每一个1的右边或者下边是否有1,如果都合法则正确,否则错误

第一种方法:

#include 
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;#define lowbit(x) (x&(-x))typedef long long ll;typedef unsigned long long ull;typedef pair
P;const double eps=1e-8;const double pi=acos(-1.0);const int inf=0x3f3f3f3f;const ll INF=1e18;const int Mod=1e9+7;const int maxn=2e5+10;int n;int a[105][105];bool vis[105][105];const int dx[]={ 0,-1};const int dy[]={ -1,0};void dfs(int r,int c){ vis[r][c]=1; if(a[r][c]==0) return; for(int i=0;i<2;i++){ int x=r+dx[i],y=c+dy[i]; if(!vis[x][y] && r>=1 && r<=n && y>=1 && y<=n) dfs(x,y); }}int main(){ //freopen("in.txt","r",stdin); //freopen("out.txt","w",stdout); ios::sync_with_stdio(0),cin.tie(0),cout.tie(0); int t; char c; cin>>t; while(t--){ cin>>n; memset(vis,0,sizeof vis); for(int i=1;i<=n;i++) for(int j=1;j<=n;j++){ cin>>c; a[i][j]=c-'0'; } for(int i=1;i<=n;i++) if(!vis[n][i] && a[i][n]) dfs(i,n); for(int i=1;i<=n;i++) if(!vis[n][i] && a[n][i]) dfs(n,i); int flag=0; for(int i=1;i<=n;i++) for(int j=1;j<=n;j++){ if(!vis[i][j] && a[i][j]) flag=1; } if(flag) cout<<"NO\n"; else cout<<"YES\n"; } return 0;}

转载地址:http://iuqq.baihongyu.com/

你可能感兴趣的文章
StringBuilder拼接字符串,“,”在前还是在后问题
查看>>
给asterisk1.8.7添加menuselct选项
查看>>
ASP.NET Core分布式项目实战(oauth2 + oidc 实现 server部分)--学习笔记
查看>>
ASP.NET Core分布式项目实战(oauth2 + oidc 实现 client部分)--学习笔记
查看>>
组合模式
查看>>
PyQt5之音乐播放器
查看>>
css居中方法与双飞翼布局
查看>>
Redis进阶实践之十八 使用管道模式提高Redis查询的速度
查看>>
SQL注入
查看>>
XCTF-upload1
查看>>
LeetCode 题解 | 1. 两数之和
查看>>
#2036:改革春风吹满地
查看>>
MPI Maelstrom POJ - 1502 ⭐⭐ 【Dijkstra裸题】
查看>>
P1379 八数码难题 ( A* 算法 与 IDA_star 算法)
查看>>
按需取余
查看>>
算法学习笔记: 珂朵莉树
查看>>
算法学习笔记:母函数详解
查看>>
Codeforces Round #664 题解(A ~ C)
查看>>
Problem 1342B - Binary Period (思维)
查看>>
Problem A - Sequence with Digits (数学推导)
查看>>