其他分享
首页 > 其他分享> > Codeforces Round #566 (Div. 2)B. Plus from Picture

Codeforces Round #566 (Div. 2)B. Plus from Picture

作者:互联网

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.
All other cells are empty.
Find out if the given picture has single “+” shape.

Input
The first line contains two integers h and w (1≤h, w≤500) — the height and width of the picture.

The i-th of the next h lines contains string si of length w 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
inputCopy
5 6


.
.


outputCopy
YES
inputCopy
3 5

.
.

outputCopy
NO
inputCopy
7 7


.



.*…
outputCopy
NO
inputCopy
5 6




outputCopy
NO
inputCopy
3 7
..
.
..
outputCopy
NO
inputCopy
5 10


.
.*****.


outputCopy
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 2.

In the fifth example, there are two shapes.

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

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<ctime>
#include<iostream>
#include<algorithm>
#include<map>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<string>
#define ll long long
#define dd double
using namespace std;

char s[505][505];

int main() {
	ios::sync_with_stdio(false);
	ll n, m;
	cin >> n >> m;
	ll x = -1;
	ll y = -1;
	ll flag = 0;
	for (ll i = 0; i < n; i++) {
		for (ll j = 0; j < m; j++) {
			cin >> s[i][j];
			if (s[i][j] == '*' && flag == 0) {
				x = i;
				y = j;
				flag = 1;
			}
		}
	}
	if (x != -1) {
		for (ll i = x; i < n; i++) {
			if (s[i][y] == '.' && i <= x + 2) {
				cout << "NO" << endl;
				return 0;
			}
			if (s[i][y] == '.' && i > x + 2) {
				break;
			}
			else {
				s[i][y] = '-';
			}
		}
		ll z = -1;
		for (ll i = x + 1; i < n; i++) {
			if (s[i][y - 1] == '*' && s[i][y + 1] == '*') {
				z = i;
				if (s[i + 1][y] == '.') {
					cout << "NO" << endl;
					return 0;
				}
				break;
			}
		}
		if (z == -1) {
			cout << "NO" << endl;
			return 0;
		}
		else {
			for (ll i = y - 1; i >= 0; i--) {
				if (s[z][i] == '*') {
					s[z][i] = '-';
				}
				else {
					break;
				}
			}
			for (ll i = y + 1; i < m; i++) {
				if (s[z][i] == '*') {
					s[z][i] = '-';
				}
				else {
					break;
				}
			}
		}
		ll flag2 = 0;
		for (ll i = 0; i < n; i++) {
			for (ll j = 0; j < m; j++) {
				if (s[i][j] == '*') {
					flag2 = 1;
					break;
				}
			}
			if (flag2 == 1) {
				break;
			}
		}
		if (flag2 == 1) {
			cout << "NO" << endl;
		}
		else {
			cout << "YES" << endl;
		}
	}
	else {
		cout << "NO" << endl;
	}
}

标签:Picture,++,ll,Codeforces,566,shape,inputCopy,include,outputCopy
来源: https://blog.csdn.net/weixin_44231195/article/details/92175948