本文主要是介绍Plus from Picture,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
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 (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.
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
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.
#include <iostream>
#include <cmath>
#include<set>
#include <algorithm>
#include<string>
#include<string.h>
using namespace std;
int a[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
char v[1000][1000];
void clearr(int x,int y)
{v[x][y]='.';for(int i=0;i<4;i++){int x1=x,y1=y;while(v[x1+a[i][0]][y1+a[i][1]]=='*'){v[x1+a[i][0]][y1+a[i][1]]='.';x1+=a[i][0],y1+=a[i][1];}}}
int main()
{string s;char ss[10000];int n,m,b[1000],flag1=0;while(cin>>m>>n){memset(v,0,sizeof(v));for(int i=1;i<=m;i++){for(int j=1;j<=n;j++){cin>>v[i][j];}}flag1=1;for(int i=1;i<=m;i++){for(int j=1;j<=n;j++){if(v[i][j]=='*'&&v[i+1][j]=='*'&&v[i-1][j]=='*'&&v[i][j+1]=='*'&&v[i][j-1]=='*'&&i+1<=m&&i-1>=1&&j+1<=n&&j-1>=1){flag1=0;clearr(i,j);goto l;}}}l:int flag=0;for(int i=1;i<=m;i++){for(int j=1;j<=n;j++){if(v[i][j]=='*'){flag=1;}}}if(flag1==1||flag)cout<<"NO"<<endl;elsecout<<"YES"<<endl;}}
这篇关于Plus from Picture的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!