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.
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.
If the given picture satisfies all conditions, print "YES". Otherwise, print "NO".
You can output each letter in any case (upper or lower).
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.
题目大意:就是输入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的更多相关文章
- Codeforces Round #566 (Div. 2)
Codeforces Round #566 (Div. 2) A Filling Shapes 给定一个 \(3\times n\) 的网格,问使用 这样的占三个格子图形填充满整个网格的方案数 如果 ...
- Codeforces #344 Div.2
Codeforces #344 Div.2 Interview 题目描述:求两个序列的子序列或操作的和的最大值 solution 签到题 时间复杂度:\(O(n^2)\) Print Check 题目 ...
- Codeforces #345 Div.1
Codeforces #345 Div.1 打CF有助于提高做题的正确率. Watchmen 题目描述:求欧拉距离等于曼哈顿距离的点对个数. solution 签到题,其实就是求有多少对点在同一行或同 ...
- Codeforces Beta Round #27 (Codeforces format, Div. 2)
Codeforces Beta Round #27 (Codeforces format, Div. 2) http://codeforces.com/contest/27 A #include< ...
- Codeforces#441 Div.2 四小题
Codeforces#441 Div.2 四小题 链接 A. Trip For Meal 小熊维尼喜欢吃蜂蜜.他每天要在朋友家享用N次蜂蜜 , 朋友A到B家的距离是 a ,A到C家的距离是b ,B到C ...
- codeforces #592(Div.2)
codeforces #592(Div.2) A Pens and Pencils Tomorrow is a difficult day for Polycarp: he has to attend ...
- codeforces #578(Div.2)
codeforces #578(Div.2) A. Hotelier Amugae has a hotel consisting of 1010 rooms. The rooms are number ...
- codeforces #577(Div.2)
codeforces #577(Div.2) A Important Exam A class of students wrote a multiple-choice test. There are ...
- codeforces #332 div 2 D. Spongebob and Squares
http://codeforces.com/contest/599/problem/D 题意:给出总的方格数x,问有多少种不同尺寸的矩形满足题意,输出方案数和长宽(3,5和5,3算两种) 思路:比赛的 ...
随机推荐
- CodeForces 150E: Freezing with Style
题目传送门:CF150E. 据说这个傻逼题还有一个 \(\log\) 的做法,但是我还不会. 题意简述: 给定一棵 \(n\)(\(2\le n\le 10^5\))个点的树,边有边权. 定义一条路径 ...
- Nginx——请求head被过滤
前言 nginx代理服务器,app发出的请求头被直接过滤了,当时想到nginx会自动过滤掉带有_的请求头信息,所以直接改了Nginx的配置当然也可以将app的request中header中的_改为- ...
- arguments简单函数 求整数递加和
function add(n){if(n == 1) return 1;else return n + arguments.callee(n-1);alert(arguments.callee(1)) ...
- JDOJ 2175: 忠诚2
JDOJ 2175: 忠诚2 题目传送门 Description 老管家是一个聪明能干的人.他为财主工作了整整10年,财主为了让自已账目更加清楚.要求管家每天记k次账,由于管家聪明能干,因而管家总是让 ...
- 7-ESP8266 SDK开发基础入门篇--串口处理数据,控制LED
接着上一节的写 咱先做一个单片机串口接收到什么就回过来什么 咱自己写个发送函数,其实就是仿照官方的写的 别忘了 现在咱建个任务处理串口数据 下载进去 现在是三个任务都在运行了...操作系统是不是很神奇 ...
- [PHP] Elasticsearch 6.4.2 的安装和使用
Elasticsearch 6.4.2 的安装和使用 一.安装http://www.ruanyifeng.com/blog/2017/08/elasticsearch.htmlhttps://www. ...
- Set学习笔记
今天又去看了一下STL里的set,来这里水一下博客 What is set? set的本质是一种功能受限的平衡树,不支持重复数字,也就是说如果插入一大堆数字12,输出它的长度还是1 如何定义 定义 s ...
- 软件工程卷1 抽象与建模 (Dines Bjorner 著)
I 开篇 1. 绪论 II 离散数学 2. 数 (已看) 3. 集合 4. 笛卡尔 5. 类型 6. 函数 7. λ演算 8. 代数 9. 数理逻辑 III 简单RSL 10. RSL中的原子类型和值 ...
- wpf, C# 实现目录对话框选择
引用:using System.Windows.Forms; 代码: FolderBrowserDialog fbWnd = new FolderBrowserDialog(); fbWnd.Desc ...
- debian/ubuntu安装mssql
添加源: debian源:deb [arch=amd64] https://packages.microsoft.com/debian/10/prod buster main ubuntu源:deb ...