codeforces#566(Div.2)B

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.

Title effect: w * h is inputted characters are '*' or, when forming such a predetermined pattern, called big star in the middle of the central point, and each time there is only one such input '.' the center point of the output "YES", otherwise a "NO".

 

Ideas: direct violence search, pay attention to pruning, through all the input, when the point is * Yes, determine whether it is the central point, when the center point is greater than 1:00 (does not meet the meaning of the questions), out of traversal; when the point when the ranks of the center point are inconsistent, but also output "NO";

Pay attention to such a situation, so to have only one central point of the test data were screened to see if there is such a point appears. code show as below:

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

 1 #include <iostream>
 2 #include <algorithm>
 3 #include <queue>
 4 #include <vector>
 5 #include <string>
 6 #define LL long long
 7 const int max_n=502;
 8 using namespace std;
 9 int w,h;
10 int tx[4]={1,-1,0,0};
11 int ty[4]={0,0 , . 1 , - . 1 };
 12 is  struct Node {
 13 is      int X, Y;
 14 } NUM [ . 3 ];
 15  char MP [max_n] [max_n];
 16  BOOL PAN ( int A, int B) // determines the point for out of bounds 
. 17  {
 18 is      return A> = 0 && B> = 0 && A <B && W < H;
 . 19  }
 20 is  BOOL BFS ( int A, int B) // determination point (a, b) is not the center point 
21  {
22     int n=0;
23     for(int i=0;i<4;i++)
24     {
25         int nx=a+tx[i],ny=b+ty[i];
26         if(mp[nx][ny]=='*'&&pan(nx,ny))n++;
27     }
28     if(n==4)return true;
29     else return false;
30 }
31 
32 int main()
33 {
34 is      int T = 0 , F = 0 ; // center points * number 
35      Scanf ( " % D% D " , & W, & H);
 36      for ( int I = 0 ; I <W; I ++ )
 37 [          CIN> > MP [I];
 38 is      for ( int I = 0 ; I <W; I ++ )
 39      {
 40          BOOL Judge = to false , to = to false ; // determines whether the center point, to prune 
41 is          for ( int J =0 ; J <H; J ++ )
 42 is          {
 43 is              IF (MP [I] [J] == ' * ' ) F ++; // count 
44 is              IF (MP [I] [J] == ' * ' && T> 0 && I ! NUM = [ 0 !] .x && J = NUM [ 0 ] .y) { // when the center point of the known point not in the same line 
45                  to = to true ;
 46 is                  BREAK ;
 47              }
 48              iF (MP [I] [ J] == ' * ' ) {
 49                  Judge = BFS (I, J);
50                  IF (Judge) {NUM [T] .x = I, NUM [T] .y = J; T ++;} // center point, able to save the coordinates 
51 is                  IF (T> . 1 ) {
 52 is                      to = to true ;
 53 is                      BREAK ;
 54 is                  }
 55              }
 56 is          }
 57 is          IF (to) { // prune 
58              T = 0 ;
 59              BREAK ;
 60          }
 61 is      }
 62 is          IF (T == 1 ) // for the center data points of a filter 
63     { // iterate because a plurality reduction center coordinates, it is intended to meet the problem should f -1 
64          int EX NUM = [ 0 ] .x, EY NUM = [ 0 ] .y;
 65          for ( int I = EX; I <W; I ++ )
 66          {
 67              IF (MP [I] [EY] == ' * ' ) F-- ;
 68              the else  BREAK ;
 69          }
 70          for ( int I = EX- . 1 ; I> = 0 ; i-- )
 71 is          {
 72              IF (MP [I] [EY] == ' *')f--;
73             else break;
74         }
75         for(int j=ey;j<h;j++)
76         {
77             if(mp[ex][j]=='*')f--;
78             else break;
79         }
80         for(int j=ey-1;j>=0;j--)
81         {
82             if(mp[ex][j]=='*')f--;
83             else break;
84         }
85         
86     }
87     if(f>-1||t!=1)cout<<"NO"<<endl;
88     else cout<<"YES"<<endl;
89     return 0;
90 }

 

Guess you like

Origin www.cnblogs.com/whocarethat/p/11009006.html