B. Plus from Picture
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
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≤ℎ
1
≤
h
, ?≤500
w
≤
500
) — the height and width of the picture.
The ?
i
-th of the next ℎ
h
lines contains string ??
s
i
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
2
.
In the fifth example, there are two shapes.
In the sixth example, there is an empty space inside of the shape.
题意:找到中心点,然后往四个方向标记。
#include <iostream>
#include <string>
#include <stack>
#include <set>
#include <vector>
#include <algorithm>
using namespace std;
#define ll long long
int main()
{
int n, m;
cin >> n >> m;
int count = 0;
vector<vector<char> > v (n);
for (int i = 0; i < n; i++) {
v[i].resize(m);
for (int j = 0; j < m; j++) {
cin >> v[i][j];
if (v[i][j] == '*') count++;
}
}
int x=0;
int y=0;
for (int i = 1; i < n-1; i++) {
for (int j = 1; j < m-1; j++) {
if (v[i][j] == '*' && v[i-1][j] == '*' && v[i+1][j] == '*' && v[i][j-1] == '*' && v[i][j+1] == '*') {
x = i;
y = j;
}
}
}
if (x == 0) {
cout << "NO" << endl;
return 0;
}
//cout << "aa " << x << " " << y << endl;
for (int i = x+1; i < n; i++) {
if (v[i][y] != '*') break;
count--;
}
for (int i = x-1; i >= 0; i--) {
if (v[i][y] != '*') break;
count--;
}
for (int j = y+1; j < m; j++) {
if (v[x][j] != '*') break;
count--;
}
for (int j = y-1; j >= 0; j--) {
if (v[x][j] != '*') break;
count--;
}
if (count == 1) {
cout << "YES" << endl;
} else
cout << "NO" << endl;
return 0;
}