?
# | Author | Problem | Lang | Verdict | Time | Memory | Sent | Judged | |
---|---|---|---|---|---|---|---|---|---|
187570166 |
Practice: DaiRuiChen007 |
1638D - 29 | C++14 (GCC 6-32) | Accepted | 654 ms | 22592 KB | 2023-01-02 04:49:39 | 2023-01-02 04:49:39 |
// LUOGU_RID: 98471925 #include<bits/stdc++.h> using namespace std; const int MAXN=1001; const int dx[]={-1,-1,-1,0,0,1,1,1}; const int dy[]={-1,0,1,-1,1,-1,0,1}; struct node { int x,y,c; }; int a[MAXN][MAXN]; inline bool valid(int x,int y) { int col=max(max(a[x][y],a[x+1][y+1]),max(a[x][y+1],a[x+1][y])); if(!col) return false; return (a[x][y]==col||a[x][y]==0)&&(a[x+1][y+1]==col||a[x+1][y+1]==0)&& (a[x+1][y]==col||a[x+1][y]==0)&&(a[x][y+1]==col||a[x][y+1]==0); } inline int color(int x,int y) { return max(max(a[x][y],a[x+1][y+1]),max(a[x][y+1],a[x+1][y])); } signed main() { int n,m; scanf("%d%d",&n,&m); vector <node> op; queue <node> q; for(int i=1;i<=n;++i) { for(int j=1;j<=m;++j) { scanf("%d",&a[i][j]); } } for(int i=1;i<n;++i) { for(int j=1;j<m;++j) { if(valid(i,j)) q.push((node){i,j,0}); } } while(!q.empty()) { int x=q.front().x,y=q.front().y; q.pop(); if(color(x,y)==0) continue; op.push_back((node){x,y,color(x,y)}); a[x][y]=a[x+1][y]=a[x][y+1]=a[x+1][y+1]=0; for(int k=0;k<8;++k) { int tx=x+dx[k],ty=y+dy[k]; if(tx<1||ty<1||tx>=n||ty>=m) continue; if(valid(tx,ty)) q.push((node){tx,ty,0}); } } for(int i=1;i<=n;++i) { for(int j=1;j<=m;++j) { if(a[i][j]!=0) { puts("-1"); return 0; } } } printf("%d\n",(int)op.size()); reverse(op.begin(),op.end()); for(auto u:op) printf("%d %d %d\n",u.x,u.y,u.c); return 0; }
?
?
?
?