E2. Stars Drawing (Hard Edition)
time limit per test
3 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
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 00 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 11, the middle figure is a star of size 22 and the rightmost figure is a star of size 33.
You are given a rectangular grid of size n×mn×m consisting only of asterisks '*' and periods (dots) '.'. Rows are numbered from 11 to nn, columns are numbered from 11 to mm. 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⋅mn⋅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⋅mn⋅m stars.
Input
The first line of the input contains two integers nn and mm (3≤n,m≤10003≤n,m≤1000) — 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 kk (0≤k≤n⋅m0≤k≤n⋅m) — the number of stars needed to draw the given grid. The next kk lines should contain three integers each — xjxj, yjyj and sjsj, where xjxj is the row index of the central star character, yjyj is the column index of the central star character and sjsj is the size of the star. Each star should be completely inside the grid.
Examples
input
Copy
6 8 ....*... ...**... ..*****. ...**... ....*... ........
output
Copy
3 3 4 1 3 5 2 3 5 1
input
Copy
5 5 .*... ****. .**** ..**. .....
output
Copy
3 2 2 1 3 3 1 3 4 1
input
Copy
5 5 .*... ***.. .*... .*... .....
output
Copy
-1
input
Copy
3 3 *.* .*. *.*
output
Copy
-1
Note
In the first example the output
2 3 4 1 3 5 2
is also correct.
思路:
可以先预处理出每一段连续星号的起点和终点,横行和纵行都要处理。再遍历图,枚举点,将以它为中心能取到的最大星形标记,即min(四边的长度),记录坐标和大小。标记可以将左边界和上边界-1,右边界和下边界+1.这样只需要从头到尾扫一遍就能知道哪些点被覆盖。将覆盖的星号变为点,如果还有星号输出-1。
代码:
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define MAXN 1005
#define mod 19260817
int n,m;
char G[MAXN][MAXN];
int row[MAXN][MAXN][2],col[MAXN][MAXN][2],dis[MAXN][MAXN][2];
struct node
{
int x,y,len;
}p[MAXN*MAXN];
int main()
{
scanf("%d%d",&n,&m);
for(int i=0;i<n;i++)
scanf("%s",G[i]);
memset(row,-1,sizeof row);
memset(col,-1,sizeof col);
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
if(G[i][j]=='*')
{
if(j==0 || row[i][j-1][0]==-1)
row[i][j][0]=j;
else row[i][j][0]=row[i][j-1][0];
}
}
for(int j=m-1;j>=0;j--)
{
if(G[i][j]=='*')
{
if(j==m-1 || row[i][j+1][1]==-1)
row[i][j][1]=j;
else row[i][j][1]=row[i][j+1][1];
}
}
}
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
if(G[j][i]=='*')
{
if(i==0 || col[j-1][i][0]==-1)
col[j][i][0]=j;
else col[j][i][0]=col[j-1][i][0];
}
}
for(int j=n-1;j>=0;j--)
{
if(G[j][i]=='*')
{
if(j==n-1 || col[j+1][i][1]==-1)
col[j][i][1]=j;
else col[j][i][1]=col[j+1][i][1];
}
}
}
int tot=0;
memset(dis,0,sizeof dis);
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
if(G[i][j]=='*')
{
int len=min(min(min(j-row[i][j][0],row[i][j][1]-j),i-col[i][j][0]),col[i][j][1]-i);
if(len==0) continue;
dis[i][j-len][0]--;
dis[i][j+len][0]++;
dis[i-len][j][1]--;
dis[i+len][j][1]++;
p[tot].y=i+1;
p[tot].x=j+1;
p[tot++].len=len;
}
}
}
for(int i=0;i<n;i++)
{
int s=0;
for(int j=0;j<m;j++)
{
s+=dis[i][j][0];
if(s<0 || (s==0 && dis[i][j][0]>0))
G[i][j]='.';
}
}
for(int i=0;i<m;i++)
{
int s=0;
for(int j=0;j<n;j++)
{
s+=dis[j][i][1];
if(s<0 || (s==0 && dis[j][i][1]>0))
G[j][i]='.';
}
}
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
if(G[i][j]=='*')
{
puts("-1");
exit(0);
}
}
}
printf("%d\n",tot);
for(int i=0;i<tot;i++)
{
printf("%d %d %d\n",p[i].y,p[i].x,p[i].len);
}
return 0;
}