codeforces 1182B (DFS)
时间:2022-07-28
本文章向大家介绍codeforces 1182B (DFS),主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
题意描述
You have a given picture with size w×h. Determine if the given picture has a single “+” shape or not. A “+” shape is described below:
- A “+” shape has one center nonempty cell.
- There should be some (at least one) consecutive non-empty cells in each direction (left, right, up, down) from the center. In other words, there should be a ray in each direction.
- There should be some (at least one) consecutive non-empty cells in each direction (left, right, up, down) from the center. In other words, there should be a ray in each direction.
Find out if the given picture has single “+” shape.
给你一串字符,让你判断所有的*是否可以构成唯一的+号
思路
我们可以先找到构成加号中间的星号,然后分别向四个方向dfs,dfs的过程中统计星号的个数,最后判断一下dfs星号的个数是否与所有星号的个数相同即可。
AC代码
#include<bits/stdc++.h>
#define x first
#define y second
#define pb push_back
#define IOS ios::sync_with_stdio(false);cin.tie(0);
using namespace std;
typedef unsigned long long ULL;
typedef pair<int,int> PII;
typedef pair<long,long> PLL;
typedef pair<char,char> PCC;
typedef long long LL;
const int N=2*1e5+10;
const int M=150;
const int INF=0x3f3f3f3f;
const int MOD=998244353;
int n,m;
char g[505][505];
int dx[4]={1,-1,0,0},dy[4]={0,0,1,-1};
bool st[505][505];
int deg=0;
void dfs(int x,int y,int dir){
//cout<<x<<' '<<y<<endl;
if(g[x][y]=='.') return ;
if(x<0 || y<0 || x>=n || y>=m) return ;
if(!st[x][y]) deg++;
st[x][y]=true;
dfs(x+dx[dir],y+dy[dir],dir);
}
void solve(){
cin>>n>>m;
for(int i=0;i<n;i++) cin>>g[i];
int cnt=0,sum=0,sum_copy=0;
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
if(g[i][j]=='*') sum++;
}
}
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
if(i>=1 && j>=1 && i+1<n && j+1<m){
if(g[i][j]=='*' && g[i-1][j]=='*' && g[i+1][j]=='*' && g[i][j+1]=='*' && g[i][j-1]=='*'){
dfs(i,j,0);
dfs(i,j,1);
dfs(i,j,2);
dfs(i,j,3);
cnt++;
}
}
}
}
//cout<<cnt<<' '<<deg<<endl;
if(cnt!=1 || deg!=sum) cout<<"No"<<endl;
else cout<<"YES"<<endl;
}
int main(){
IOS;
solve();
return 0;
}
- React Native组件只Image
- 数据结构之线性表
- 生信宝典之傻瓜式 (三) 我的基因在哪里发光 - 如何查找基因在发表研究中的表达
- 谈谈 char *num="123";和char num[4]="123";的区别
- 未越狱的iPhone/iPad也中招:走近强大的间谍软件XAgent与MadCap
- 【年末收藏】17个新手常见Python运行时错误
- C++ STL之priority_queue
- 链表的相关操作
- 《笨办法学Python》 第0课手记
- Python黑客学习笔记:从HelloWorld到编写PoC(上)
- java多线程(内附实例:窗口售票问题、人和叉子的问题)
- 数据结构之队列
- 《笨办法学Python》 第20课手记
- 泄露数据中的秘密:中国网民的密码设置习惯
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- ingress通过daemonSet,nodeSelector,hostNetwork方式部署
- Oracle参数解析(event)
- Qt读写文件(2种方式)实现详解
- 字符串匹配 - KMP算法
- c# 判断文件是否发生了变化
- C# 用IrisSkin4.dll美化你的WinForm
- Oracle参数解析(shared_pool_size)
- C# GTS四轴运动控制器实例(固高科技步进电机不带编码器)
- Oracle参数解析(pre_page_sga)
- C#简单爬取数据(.NET使用HTML解析器NSoup和正则两种方式匹配数据)
- [WPF] WPF做的漂亮的登陆界面[附源码]
- C#自定义控件的创建
- Oracle参数解析(processor_group_name)
- [Oracle故障处理]记一次PX msg pool 4031错误的处理
- WPF的布局-Grid(表格布局)