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
Hint
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.
题意:
给出一个只包含 * 和 . 的 w*h 矩阵, 判断该矩阵中是否满足如下条件
- 存在一个*的十字的形状
- 除了该十字外其余部分都为.
题解:
主要复杂度哈, 最多写到 N^3
先存一下 * 的个数, 然后对于每个可能的十字, 走完该十字判断剩余 * 个数是否为0
经验小结:
注意复杂度
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <string>
#include <stdlib.h>
#include <vector>
#include <queue>
#include <cmath>
#include <stack>
#include <map>
#include <set>
using namespace std;
#define ms(x, n) memset(x,n,sizeof(x));
typedef long long LL;
const int inf = 1 << 30;
const LL maxn = 510;
int h, w;
char c[maxn][maxn];
int main() {
int cnt = 0;
cin >> h >> w;
for(int i = 1; i <= h; ++i)
for(int j = 1; j <= w; ++j) {
cin >> c[i][j];
if(c[i][j] == '*')
++cnt;
}
int cur = 0;;
for(int i = 1; i <= h; ++i)
for(int j = 1; j <= w; ++j) {
if(c[i][j] == '*' && c[i + 1][j] == '*' && c[i - 1][j] == '*' && c[i][j + 1] == '*' && c[i][j - 1] == '*') {
cur = cnt-1;
for(int k = 1; i + k <= h; ++k) {
if(c[i + k][j] == '.')
break;
--cur;
}
for(int k = 1; j + k <= w; ++k) {
if(c[i][j + k] == '.')
break;
--cur;
}
for(int k = 1; i - k > 0; ++k) {
if(c[i - k][j] == '.')
break;
--cur;
}
for(int k = 1; j - k > 0; ++k) {
if(c[i][j - k] == '.')
break;
--cur;
}
if(cur == 0) {
cout << "YES\n";
return 0;
}
}
}
cout << "NO\n";
return 0;
}
/*
7 7
.......
...*...
..****.
...*...
...*...
.......
...*...
3 4
*.*.
****
..*.
*/