codeforces 1182B (DFS)

87 篇文章 1 订阅
14 篇文章 1 订阅

题意描述

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;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值