Codeforces Round #501 (Div. 3) E(前缀和)

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.

 

这个题有两个版本 easy跟hard

easy版本数据范围为100*100,这个显然就是暴力枚举每个*的店,往四个方向扩展,打上标记,最后看是否存在不被打上标记的“*”即可。

hard版本数据范围为1000*1000,easy的暴力方法显然不可行。

我们可以这么考虑,对于一个"*"的点,他的上下左右四个位置的最大扩展长度是跟他旁边的点有关系的,那么,可以用前缀和的方法计算出每个点的l,r,u,d。

在更新点的时候,我们可以采用a[l]+1,a[r+1]-1的方法来更新,最后加起来,就可以得到哪些点被覆盖了。

#include<bits/stdc++.h>
#define mp make_pair
#define fir first
#define se second
#define ll long long
#define pb push_back
using namespace std;
const int maxn=2e5+10;
const ll mod=1e9+7;
const int maxm=1e6+10;
const double eps=1e-7;
const int inf=0x3f3f3f3f;
const double pi = acos (-1.0);
int n,m;
char str[1010][1010];
int vis[1010][1010];
vector<pair<pair<int,int>,int> > v;
int l[1010][1010],r[1010][1010],u[1010][1010],d[1010][1010],cnt[1010][1010],cnt1[1010][1010];
int main(){
    scanf("%d %d",&n,&m);
    for (int i=1;i<=n;i++)
       scanf("%s",str[i]+1);
    for (int i=1;i<=n;i++){
        for (int j=1;j<=m;j++){
            if (str[i][j]=='.') continue;
            else{
                if (str[i][j-1]=='*')
                    l[i][j]=l[i][j-1]+1;
                else l[i][j]=0;
            }
        }
    }
    for (int i=1;i<=n;i++){
        for (int j=m;j>=1;j--){
            if (str[i][j]=='.') continue;
            else{
                if (str[i][j+1]=='*')
                    r[i][j]=r[i][j+1]+1;
                else r[i][j]=0;
            }
        }
    }
    for (int j=1;j<=m;j++){
        for (int i=1;i<=n;i++){
            if (str[i][j]=='.') continue;
            else{
                if (str[i-1][j]=='*'){
                    u[i][j]=u[i-1][j]+1;
                }
                else u[i][j]=0;
            }
        }
    }
    for (int j=1;j<=m;j++){
        for (int i=n;i>=1;i--){
            if (str[i][j]=='.') continue;
            else{
                if (str[i+1][j]=='*'){
                    d[i][j]=d[i+1][j]+1;
                }
                else d[i][j]=0;
            }
        }
    }
    for (int i=1;i<=n;i++){
        for (int j=1;j<=m;j++){
            if(str[i][j]=='*'){
                int Min=min(min(l[i][j],r[i][j]),min(u[i][j],d[i][j]));
                if (!Min) continue;
                v.pb(mp(mp(i,j),Min));
                int L=j-Min;
                int R=j+Min;
                int U=i-Min;
                int D=i+Min;
                cnt[i][L]++;
                cnt[i][R+1]--;
                cnt1[j][U]++;
                cnt1[j][D+1]--;
            }
        }
    }
    for (int i=1;i<=n;i++){
        for (int j=1;j<=m;j++){
            cnt[i][j]+=cnt[i][j-1];
        }
    }
    for (int j=1;j<=m;j++){
        for (int i=1;i<=n;i++){
            cnt1[j][i]+=cnt1[j][i-1];
        }
    }
    for (int i=1;i<=n;i++){
        for (int j=1;j<=m;j++){
            vis[i][j]=cnt[i][j]+cnt1[j][i];
        }
    }
    for (int i=1;i<=n;i++){
        for (int j=1;j<=m;j++){
            if (str[i][j]=='*'&&!vis[i][j]){
                printf("-1\n");
                return 0;
            }
        }
    }
    printf("%d\n",(int)v.size());
    for (int i=0;i<v.size();i++)
        printf("%d %d %d\n",v[i].fir.fir,v[i].fir.se,v[i].se);
    return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值