1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
| int r,c; int con[505][505]; void solve(){ memset(con,0,sizeof(con)); sa(r),sa(c); repp(i,1,r) { repp(j,1,c) { repp(x,1,r) { repp(y,1,c) { if(i==x||j==y)continue; if(i+j==x+y)continue; if(i-j==x-y)continue; int f1 = (i-1)*c + j-1; int f2 = (x-1)*c + y-1; con[f1][f2]=1; } } } } int n = r*c; vector<int>v; rep(i,0,n) { v.push_back(i); } repp(i,1,1000) { random_shuffle(v.begin(),v.end()); int flag=0; rep(i,0,n-1) { int k = i+1; for(;k<n;k++) { if(con[v[i]][v[k]]) { swap(v[k],v[i+1]); break; } } if(k>=n) { flag = 1; } } if(flag == 0){ cout<<"POSSIBLE"<<endl; rep(i,0,n) { int x = v[i]/c + 1; int y = v[i]%c + 1; cout<<x<<" "<<y<<endl; } return; } } cout<<"IMPOSSIBLE"<<endl; }
|