1. 程式人生 > >Codeforces Round #501 (Div. 3) E(字首和)

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;
}