题目传送门:点击传送
Time limit per test:3 seconds
Memory limit per test:256 megabytes
Description
A star is a figure of the following type: an asterisk character '*' in the center of the figure and four rays (to the left, right, top, bottom) of the same positive length. The size of a star is the length of its rays. The size of a star must be a positive number (i.e. rays of length 0 are not allowed).
Let's consider empty cells are denoted by '.', then the following figures are stars:
The leftmost figure is a star of size 1, the middle figure is a star of size 2 and the rightmost figure is a star of size 3.(星星定义)
You are given a rectangular grid of size n×m consisting only of asterisks '*' and periods (dots) '.'. Rows are numbered from 1 to n, columns are numbered from 1 to m. Your task is to draw this grid using any number of stars or find out that it is impossible. Stars can intersect, overlap or even coincide with each other. The number of stars in the output can't exceed n⋅m. Each star should be completely inside the grid. You can use stars of same and arbitrary sizes.
In this problem, you do not need to minimize the number of stars. Just find any way to draw the given grid with at most n⋅m stars.
Input
The first line of the input contains two integers nn and mm (3≤n,m≤100) — the sizes of the given grid.
The next nn lines contains mm characters each, the ii-th line describes the ii-th row of the grid. It is guaranteed that grid consists of characters '*' and '.' only.
Output
If it is impossible to draw the given grid using stars only, print "-1".
Otherwise in the first line print one integer k (0≤k≤n⋅m) — the number of stars needed to draw the given grid. The next k lines should contain three integers each — xj, yj and sj, where xj is the row index of the central star character, yj is the column index of the central star character and sj is the size of the star. Each star should be completely inside the grid.
Examples
input
6 8
....*...
...**...
..*****.
...**...
....*...
........
output
3
3 4 1
3 5 2
3 5 1
input
5 5
.*...
****.
.****
..**.
.....
output
3
2 2 1
3 3 1
3 4 1
input
5 5
.*...
***..
.*...
.*...
.....
output
-1
input
3 3
*.*
.*.
*.*
output
-1
Note
In the first example the output
2
3 4 1
3 5 2
is also correct.
解析:
星星定义:如题目图片所示,级别为1、2、3
给定n*m的格子,要你找星星(数量不做限制)覆盖*,如果还是有*没有被覆盖,输出-1;
否则输出星星数(不会大于n*m)
想法:对于每个*都找能形成的最大星星,并用数组记录星星覆盖过的*,最后输出那些被覆盖次数为0的*
注意:对于每个*只需要输出级别最大的星(否则会超出星星数量n*m)
//极端情况下每个格子都能画星星,若输出各级星星,数量必定超过n*m
比如4级星星只需输出4,不需要输出4、3、2、1
代码
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <queue>
using namespace std;
char a[105][105];
int b[105][105];//记录被星星覆盖次数
int cnt,ans=0,n,m,stat=1;
struct s
{
int x,y,num;
}star[10005]; //用来存星星
int check(int x,int y,int i)//检查是否越界
{
if(x+i<n&&x-i>=0&&y+i<m&&y-i>=0)
return 1;
else return 0;
}
void find_star(int x,int y) //对于某个坐标找它的最大星星
{
if(!check(x,y,1))
return;
if(a[x+1][y]=='.'||a[x-1][y]=='.'||a[x][y+1]=='.'||a[x][y-1]=='.')
return;
int i;
b[x][y]++;
for(i=1;check(x,y,i);i++)
{
if(a[x+i][y]=='.'||a[x-i][y]=='.'||a[x][y+i]=='.'||a[x][y-i]=='.')
break;
else cnt++;
b[x+i][y]++,b[x-i][y]++,b[x][y+i]++,b[x][y-i]++;
}
return;
}
int main()
{
scanf("%d %d",&n,&m);
getchar();
int i,j;
for(i=0;i<n;i++)//读图
{
for(j=0;j<m;j++)
a[i][j]=getchar();
getchar();
}
for(i=0;i<n;i++)
for(j=0;j<m;j++)
{
cnt=0; //cnt的结果为星星大小
if(a[i][j]=='*')
find_star(i,j);
if(cnt)
star[ans].x=i,star[ans].y=j,star[ans].num=cnt,ans++; //记录星星
}
for(i=0;i<n;i++)
for(j=0;j<m;j++)
if(a[i][j]=='*'&&b[i][j]==0)//检查是否有*没有被星星覆盖
stat=0;
if(stat)//stat为真输出各星星 反则输出-1
{
printf("%d\n",ans);
for(i=0;i<ans;i++)
printf("%d %d %d\n",star[i].x+1,star[i].y+1,star[i].num);
}
else printf("-1\n");
return 0;
}