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 (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
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.

题目大意:就是输入w*h个字符,分别为'*'或'.',规定当形成这样的图形时,则称中间的大五角星为中心点,每次输入时有且仅有一个这样的中心点输出“YES”,否则输出“NO”.

思路:直接暴力搜索,注意剪枝即可,遍历所有输入,当该点为*是,判断其是否为中心点,当中心点数大于1时(不符合题意了),跳出遍历;当该点与中心点的行列都不一致时,也输出“NO”;

要注意这样一种情况,所以要对只有一个中心点的测试数据,进行筛选,看是否有这样的点出现。代码如下:

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

 #include <iostream>
#include <algorithm>
#include <queue>
#include <vector>
#include <string>
#define LL long long
const int max_n=;
using namespace std;
int w,h;
int tx[]={,-,,};
int ty[]={,,,-};
struct node{
int x,y;
}num[];
char mp[max_n][max_n];
bool pan(int a,int b)//判断该点是否越界
{
return a>=&&b>=&&a<w&&b<h;
}
bool bfs(int a,int b)//判断点(a,b)是不是中心点
{
int n=;
for(int i=;i<;i++)
{
int nx=a+tx[i],ny=b+ty[i];
if(mp[nx][ny]=='*'&&pan(nx,ny))n++;
}
if(n==)return true;
else return false;
} int main()
{
int t=,f=;//中心点数,*数
scanf("%d %d",&w,&h);
for(int i=; i<w;i++)
cin>>mp[i];
for(int i=;i<w;i++)
{
bool judge=false,to=false;//判断是否为中心点,to剪枝
for(int j=;j<h;j++)
{
if(mp[i][j]=='*')f++;//计数
if(mp[i][j]=='*'&&t>&&i!=num[].x&&j!=num[].y){//当该点与已知中心点不在统一行列时
to=true;
break;
}
if(mp[i][j]=='*'){
judge=bfs(i,j);
if(judge){num[t].x=i,num[t].y=j;t++;}//是中心点,存下坐标
if(t>){
to=true;
break;
}
}
}
if(to){//剪枝
t=;
break;
}
}
if(t==)//对中心点数为1的数据筛选
{//遍历,因为多减了一次中心点坐标,所以符合题意的f应为-1
int ex=num[].x,ey=num[].y;
for(int i=ex;i<w;i++)
{
if(mp[i][ey]=='*')f--;
else break;
}
for(int i=ex-;i>=;i--)
{
if(mp[i][ey]=='*')f--;
else break;
}
for(int j=ey;j<h;j++)
{
if(mp[ex][j]=='*')f--;
else break;
}
for(int j=ey-;j>=;j--)
{
if(mp[ex][j]=='*')f--;
else break;
} }
if(f>-||t!=)cout<<"NO"<<endl;
else cout<<"YES"<<endl;
return ;
}

codeforces#566(Div.2)B的更多相关文章

  1. Codeforces Round #566 (Div. 2)

    Codeforces Round #566 (Div. 2) A Filling Shapes 给定一个 \(3\times n\) 的网格,问使用 这样的占三个格子图形填充满整个网格的方案数 如果 ...

  2. Codeforces #344 Div.2

    Codeforces #344 Div.2 Interview 题目描述:求两个序列的子序列或操作的和的最大值 solution 签到题 时间复杂度:\(O(n^2)\) Print Check 题目 ...

  3. Codeforces #345 Div.1

    Codeforces #345 Div.1 打CF有助于提高做题的正确率. Watchmen 题目描述:求欧拉距离等于曼哈顿距离的点对个数. solution 签到题,其实就是求有多少对点在同一行或同 ...

  4. Codeforces Beta Round #27 (Codeforces format, Div. 2)

    Codeforces Beta Round #27 (Codeforces format, Div. 2) http://codeforces.com/contest/27 A #include< ...

  5. Codeforces#441 Div.2 四小题

    Codeforces#441 Div.2 四小题 链接 A. Trip For Meal 小熊维尼喜欢吃蜂蜜.他每天要在朋友家享用N次蜂蜜 , 朋友A到B家的距离是 a ,A到C家的距离是b ,B到C ...

  6. codeforces #592(Div.2)

    codeforces #592(Div.2) A Pens and Pencils Tomorrow is a difficult day for Polycarp: he has to attend ...

  7. codeforces #578(Div.2)

    codeforces #578(Div.2) A. Hotelier Amugae has a hotel consisting of 1010 rooms. The rooms are number ...

  8. codeforces #577(Div.2)

    codeforces #577(Div.2) A  Important Exam A class of students wrote a multiple-choice test. There are ...

  9. codeforces #332 div 2 D. Spongebob and Squares

    http://codeforces.com/contest/599/problem/D 题意:给出总的方格数x,问有多少种不同尺寸的矩形满足题意,输出方案数和长宽(3,5和5,3算两种) 思路:比赛的 ...

随机推荐

  1. pycharm与github的使用

    1.clone代码: 输入github的存储库地址,输入本地存储目录,点击clone即可: 2.上传代码: 输入存储库名字,点击share即可:github上就会有你分享的代码啦

  2. Linux下使用cx_Oracle的一些配置

    在安装完成cx_Oracle后,import  cx_Oracle时报错,首先查看.bash_profile文件中环境变量配置 # .bash_profile # Get the aliases an ...

  3. 码云因为认证失败导致推送失败 生成 SSH 密钥对

  4. 达信:深度解读COSO新版企业风险管理框架(ERM)

    http://www.sohu.com/a/124375769_489979 2016年6月,美国反欺诈财务报告委员会(The Committee of Sponsoring Organization ...

  5. Scrapy笔记05- Item详解

    Scrapy笔记05- Item详解 Item是保存结构数据的地方,Scrapy可以将解析结果以字典形式返回,但是Python中字典缺少结构,在大型爬虫系统中很不方便. Item提供了类字典的API, ...

  6. three.js 测试1

    关键看一下里面的注释 代码: <!DOCTYPE html> <html> <head> <meta charset="utf-8" /& ...

  7. axios发送自定义请求头的跨域解决

    前端发送来的axios请求信息 this.$axios.request({  url:'http://127.0.0.1:8001/pay/shoppingcar/',  method:'post', ...

  8. 数据结构与算法系列——排序(4)_Shell希尔排序

    1. 工作原理(定义) 希尔排序,也称递减增量排序算法,是插入排序的一种更高效的改进版本.但希尔排序是非稳定排序算法. 希尔排序的基本思想是:先将整个待排序的记录序列分割成为若干子序列分别进行直接插入 ...

  9. phpstrom 配置getter和setter

    先看一段代码 protected $mddid; /** * @return mixed */ public function getMddid() { return $this->mddid; ...

  10. kali 添加swap 交换分区

    这里采用的是添加交换文件 mkdir /swap #创建/swap 文件夹 dd if=/dev/zero of=/swap/swapfile bs=1M count=4096 # 在/swap 下创 ...