#include <iostream>
#include <cstdio>
#include <string>
#include <sstream>
#include <algorithm>
#include <set>
#include <numeric>
#include <cmath>
#include <map>
#include <vector>
#include <queue>
#include <stack>
#include <cstring>
#include <queue>
#include <numeric>
#include <iomanip>
#define ll long long
using namespace std;
const int MAXN=2000001;
int parent[2000000],sz[2000000];
int m,n;
int ccn[2000001]={0,};
string board[1500];
int find(int u){
return u==parent[u] ? u : parent[u]=find(parent[u]);
}
int getId(int r,int c){
return r*3*m+c;
}
void mrg(int u,int v){
u=find(u); v=find(v);
if(u==v)return;
parent[u]=v;
sz[v]+=sz[u];
}
int dy[4]={0,0,1,-1};
int dx[4]={1,-1,0,0};
int ddy[8];
int ddx[8];
bool checkInf(int y,int x){
int root=find(getId(y,x));
for(int k=0;k<8;k++){
int yyy=y+ddy[k];
int xxx=x+ddx[k];
int node=find(getId(y+ddy[k],x+ddx[k]));
if(node==root)return true;
}
return false;
}
bool isCan(int y,int x,char word){
if(y>=0 && y<3*n && x>=0 && x<3*m && word==board[y][x])return true;
return false;
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
for(int i=0;i<2000000;i++)parent[i]=i,sz[i]=1;
cin>>n>>m;
ddx[0]=-m; ddx[1]=0; ddx[2]=m; ddx[3]=-m; ddx[4]=m; ddx[5]=-m; ddx[6]=0; ddx[7]=m;
ddy[0]=-n; ddy[1]=-n; ddy[2]=n; ddy[3]=0; ddy[4]=0; ddy[5]=n; ddy[6]=n; ddy[7]=n;
for(int i=0;i<n;i++){
cin>>board[i];
board[i]+=board[i];
board[i]+=board[i];
}
for(int i=0;i<n;i++){
board[i+n]=board[i];
}
for(int i=0;i<n;i++){
board[i+2*n]=board[i];
}
for(int i=0;i<3*n;i++){
for(int j=0;j<3*m;j++){
int y=i,x=j;
for(int k=0;k<4;k++){
int ny=y+dy[k];
int nx=x+dx[k];
if(isCan(ny,nx,board[y][x])){
mrg(getId(ny,nx),getId(y,x));
}
}
}
}
for(int i=n;i<n+n;i++){
for(int j=m;j<m+m;j++){
if(checkInf(i,j))cout<<-1<<' ';
else cout<<sz[find(getId(i,j))]<<' ';
}
cout<<"\n";
}
return 0;
}
Compilation message
YZ.cpp: In function 'bool checkInf(int, int)':
YZ.cpp:43:13: warning: unused variable 'yyy' [-Wunused-variable]
int yyy=y+ddy[k];
^
YZ.cpp:44:13: warning: unused variable 'xxx' [-Wunused-variable]
int xxx=x+ddx[k];
^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
59 ms |
27512 KB |
Execution killed because of forbidden syscall writev (20) |
2 |
Halted |
0 ms |
0 KB |
- |