5.4 CJJ的补题之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.

输入:

The first line contains two integers hh and ww (1≤h1≤h, w≤500w≤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.

输出:

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

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

样例输入:

5 6
......
..*...
.****.
..*...
..*...

复制

样例输出:

YES

复制

样例输入:

3 5
..*..
****.
.*...

复制

样例输出:

NO

复制

样例输入:

7 7
.......
...*...
..****.
...*...
...*...
.......
.*.....

复制

样例输出:

NO

复制

样例输入:

5 6
..**..
..**..
******
..**..
..**..

复制

样例输出:

NO

复制

样例输入:

3 7
.*...*.
***.***
.*...*.

复制

样例输出:

NO

复制

样例输入:

5 10
..........
..*.......
.*.******.
..*.......
..........

复制

样例输出:

NO

复制

注释:

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.

题目大意:

判断一个图里面有没有唯一一个一行一列加号

解题思路:

关键点:

1. n<=500,m<=500完全可以n^2的复杂度

2. 判断这个图是不是所给要求的图很困难,但是找到一个完整的加号并且把它消掉很简单

all in all,我们就可以用n^2复杂度找到一个加号把他消掉(只消这1个),再判断就没有*就可以啦~

代码:

#include <iostream>
using namespace std;
int n, m;
const int N = 555;
char a[N][N];
int dx[4] = {-1, 0, 1, 0};
int dy[4] = {0, 1, 0, -1};
void solve()
{
    int f = 0;
    //遍历图
    for (int i = 1; i < n - 1; i++)
    {
        for (int j = 1; j < m - 1; j++)
        {
            int flag = 1;
            //判断枚举的点是不是+中心
            for (int k = 0; k < 4; k++)
            {
                int xx = i + dx[k];
                int yy = j + dy[k];
                if (a[i][j] == '.')
                {
                    flag = 0;
                    break;
                }
                if (xx >= 0 && xx < n && yy >= 0 && yy < m)
                {
                    if (a[xx][yy] == '.')
                    {
                        flag = 0;
                        break;
                    }
                }
            }
            //是的话消掉这个+
            if (flag)
            {
                // cout << i << " " << j << endl;
                a[i][j] = '.';
                for (int k = 0; k < 4; k++)
                {
                    int p = 1;
                    int xx = i + dx[k] * p;
                    int yy = j + dy[k] * p;
                    while (a[xx][yy] == '*')
                    {
                        // cout << xx << " " << yy << endl;
                        a[xx][yy] = '.';
                        p++;
                        xx = i + dx[k] * p;
                        yy = j + dy[k] * p;
                    }
                }
                f = 1;
            }
            if (f)
                break;
        }
        if (f)
            break;
    }
}
int main()
{
    int F = 0;
    cin >> n >> m;
    //特判整个图里面有没有*
    for (int i = 0; i < n; i++)
        for (int j = 0; j < m; j++)
        {
            cin >> a[i][j];
            if(a[i][j]=='*')
                F = 1;
        }
    //消+        
    solve();
    int flag = 1;
    //消完一个+之后判断还有没有*
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < m; j++)
        {
            if (a[i][j] == '*')
            {
                flag = 0;
                break;
            }
        }
        if (!flag)
            break;
    }
    if (flag&&F)
        cout << "YES" << endl;
    else
        cout << "NO" << endl;
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值