Stars Drawing (Hard Edition) CodeForces - 1015E2

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 11to 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, yjyjis 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

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.

题意:
给出一个n*m的图,四方向*的最大长度相同为一个星星,问能否找到不大于n*m颗星星,使得图中的所有*都被星星覆盖。
思路:
这道题类似与dp,首先开四个与字符数组相同大小的数组l,r,up,dn,然后进行两次扫描,这四个数组分别代表了从左边、
右边、上边、下边开始数,数到当前位置的星号时已经有几个星号。然后再次扫描,枚举每一个星号。

#include<stdio.h>
#include<string.h>
#include<vector>
#include<iostream>
#include<queue>
#include<algorithm>
using namespace std;
#define inf 0x3f3f3f3f
#define pb push_back
#define mp make_pair
char g[1010][1010];
int l[1010][1010],r[1010][1010],up[1010][1010],dn[1010][1010];
int jx[1010][1010],jy[1010][1010];
int n,m;
struct ans
{
    int x,y;
    int len;
};
vector<ans>p;
int main()
{

    scanf("%d%d",&n,&m);
    for(int i=1; i<=n; i++)
        scanf("%s",g[i]+1);
    for(int i=1; i<=n; i++)
    {
        for(int j=1; j<=m; j++)
        {
            if(g[i][j]=='*')
            {
                l[i][j]=l[i][j-1]+1;
                up[i][j]=up[i-1][j]+1;
            }
        }
    }

    for(int i=n; i>=1; i--)
    {
        for(int j=m; j>=1; j--)
        {
            if(g[i][j]=='*')
            {
                r[i][j]=r[i][j+1]+1;
                dn[i][j]=dn[i+1][j]+1;
            }
        }
    }
    for(int i=1; i<=n; i++)
    {
        for(int j=1; j<=m; j++)
        {
            if(g[i][j]=='*')
            {
                int len=inf;
                len=min(min(l[i][j],r[i][j]),
                        min(up[i][j],dn[i][j]));
                if(len>1)
                {
                    ans temp;
                    temp.x=i;
                    temp.y=j;
                    temp.len=len-1;
                    p.pb(temp);
                    jx[i-len+1][j]++;
                    jx[i+len][j]--;
                    jy[i][j-len+1]++;
                    jy[i][j+len]--;
                }
            }
        }
    }
    for(int i=1; i<=n; i++)
    {
        for(int j=1; j<=m; j++)
        {
            jx[i][j]+=jx[i-1][j];
            jy[i][j]+=jy[i][j-1];
        }
    }

    for(int i=1; i<=n; i++)
    {
        for(int j=1; j<=m; j++)
        {
            if(g[i][j]=='*'&&jx[i][j]==0&&jy[i][j]==0)
            {
                printf("-1\n");
                return 0;
            }
        }
    }
    int size=p.size();
    printf("%d\n",size);
    for(int i=0; i<size; i++)
    {
        printf("%d %d %d\n",p[i].x,p[i].y,p[i].len);
    }
    return 0;
}
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值