zoukankan      html  css  js  c++  java
  • B. Plus from Picture

    You have a given picture with size w×hw×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.
    • All other cells are empty.

    Find out if the given picture has single "+" shape.

    Input

    The first line contains two integers hh and ww (1h1≤h, w500w≤500) — the height and width of the picture.

    The ii-th of the next hh lines contains string sisi of length ww consisting "." and "*" where "." denotes the empty space and "*" denotes the non-empty space.

    Output

    If the given picture satisfies all conditions, print "YES". Otherwise, print "NO".

    You can output each letter in any case (upper or lower).

    Examples
    input
    Copy
    5 6
    ......
    ..*...
    .****.
    ..*...
    ..*...
    
    output
    Copy
    YES
    
    input
    Copy
    3 5
    ..*..
    ****.
    .*...
    
    output
    Copy
    NO
    
    input
    Copy
    7 7
    .......
    ...*...
    ..****.
    ...*...
    ...*...
    .......
    .*.....
    
    output
    Copy
    NO
    
    input
    Copy
    5 6
    ..**..
    ..**..
    ******
    ..**..
    ..**..
    
    output
    Copy
    NO
    
    input
    Copy
    3 7
    .*...*.
    ***.***
    .*...*.
    
    output
    Copy
    NO
    
    input
    Copy
    5 10
    ..........
    ..*.......
    .*.******.
    ..*.......
    ..........
    
    output
    Copy
    NO
    
    Note

    In the first example, the given picture contains one "+".

    In the second example, two vertical branches are located in a different column.

    In the third example, there is a dot outside of the shape.

    In the fourth example, the width of the two vertical branches is 22.

    In the fifth example, there are two shapes.

    In the sixth example, there is an empty space inside of the shape.

    #include <iostream>
    #include <vector>
    #include <algorithm>
    #include <string>
    #include <set>
    #include <queue>
    #include <map>
    #include <sstream>
    #include <cstdio>
    #include <cstring>
    #include <numeric>
    #include <cmath>
    #include <iomanip>
    #include <deque>
    #include <bitset>
    //#include <unordered_set>
    //#include <unordered_map>
    #define ll              long long
    #define pii             pair<int, int>
    #define rep(i,a,b)      for(int  i=a;i<=b;i++)
    #define dec(i,a,b)      for(int  i=a;i>=b;i--)
    #define forn(i, n)      for(int i = 0; i < int(n); i++)
    using namespace std;
    int dir[4][2] = { { 1,0 },{ 0,1 } ,{ 0,-1 },{ -1,0 } };
    const long long INF = 0x7f7f7f7f7f7f7f7f;
    const int inf = 0x3f3f3f3f;
    const double pi = 3.14159265358979323846;
    const double eps = 1e-6;
    const int mod = 1e9 + 7;
    const int N = 3e3 + 5;
    //if(x<0 || x>=r || y<0 || y>=c)
    
    inline ll read()
    {
        ll x = 0; bool f = true; char c = getchar();
        while (c < '0' || c > '9') { if (c == '-') f = false; c = getchar(); }
        while (c >= '0' && c <= '9') x = (x << 1) + (x << 3) + (c ^ 48), c = getchar();
        return f ? x : -x;
    }
    ll gcd(ll m, ll n)
    {
        return n == 0 ? m : gcd(n, m % n);
    }
    ll lcm(ll m, ll n)
    {
        return m * n / gcd(m, n);
    }
    bool prime(int x) {
        if (x < 2) return false;
        for (int i = 2; i * i <= x; ++i) {
            if (x % i == 0) return false;
        }
        return true;
    }
    ll qpow(ll m, ll k, ll mod)
    {
        ll res = 1, t = m;
        while (k)
        {
            if (k & 1)
                res = res * t % mod;
            t = t * t % mod;
            k >>= 1;
        }
        return res;
    }
    int h, w, coll, colr, rowu, rowd;
    bool vis[505][505];
    int main()
    {
        cin >> h >> w;  
        vector<string> a(h);
        forn(i, h)
            cin >> a[i];
        int fg = 0;
        forn(i, h)
        {
            forn(j, w)
            {
                if (a[i][j] == '*' && i>0 && i<h-1 && a[i+1][j]=='*' && a[i-1][j]=='*' && j>0 && j<w-1 && a[i][j-1]=='*' && a[i][j+1]=='*')
                {
                    for (int col1 = j; col1 < w && a[i][col1] != '.'; col1++)
                        vis[i][col1] = 1;
                    for (int col2 = j; col2 >= 0 && a[i][col2] != '.'; col2--)
                        vis[i][col2] = 1;
                    for (int row1 = i; row1 < h && a[row1][j] != '.'; row1++)
                        vis[row1][j] = 1;
                    for (int row2 = i; row2 >= 0 && a[row2][j] != '.'; row2--)
                        vis[row2][j] = 1;
                    fg = 1;
                    break;
                }
            }
            if (fg)
                break;
        }
        forn(i, h)
        {
            forn(j, w)
            {
                if (a[i][j] == '*' && vis[i][j] == 0)
                    fg = 2;
            }
        }
        if (fg == 0 || fg == 2)
            cout << "NO" << endl;
        else
            cout << "YES" << endl;
        return 0;
    }
  • 相关阅读:
    对于bilibili主页head部分的代码的总结以及疑问。
    小项目使用框架(B/S),简单实现mvc分离
    petshop4学习_重构DataList实现分页
    照书抄了个小的sniffer。不过有问题测试不能通过,请求各位帮帮我
    《灵魂的迷茫》
    vs2005中gridview的RowCommand事件
    快速得到google的Gmail(已申请的免看)
    命令行下重启/启动停止IIS服务器的命令
    ES6之const命令
    不容忽视的js面试题
  • 原文地址:https://www.cnblogs.com/dealer/p/13198666.html
Copyright © 2011-2022 走看看