This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define pb push_back
#define all(v) (v).begin(), (v).end()
#define rep(i, n) for (int i = 0; i < n; ++i)
#define rrep(i, n) for (int i = 1; i <= n; ++i)
#define ff first
#define ss second
using namespace std;
typedef long long ll;
void __print(int x) { cerr << x; }
void __print(long x) { cerr << x; }
void __print(long long x) { cerr << x; }
void __print(unsigned x) { cerr << x; }
void __print(unsigned long x) { cerr << x; }
void __print(unsigned long long x) { cerr << x; }
void __print(float x) { cerr << x; }
void __print(double x) { cerr << x; }
void __print(long double x) { cerr << x; }
void __print(char x) { cerr << '\'' << x << '\''; }
void __print(const char *x) { cerr << '\"' << x << '\"'; }
void __print(const string &x) { cerr << '\"' << x << '\"'; }
void __print(bool x) { cerr << (x ? "true" : "false"); }
template <typename T, typename V> void __print(const pair<T, V> &x) {
cerr << '{';
__print(x.first);
cerr << ", ";
__print(x.second);
cerr << '}';
}
template <typename T> void __print(const T &x) {
int f = 0;
cerr << '{';
for (auto &i : x)
cerr << (f++ ? ", " : ""), __print(i);
cerr << "}";
}
void _print() { cerr << "]\n"; }
template <typename T, typename... V> void _print(T t, V... v) {
__print(t);
if (sizeof...(v))
cerr << ", ";
_print(v...);
}
#ifndef ONLINE_JUDGE
#define dbg(x...) \
cerr << "\e[91m" << __func__ << ":" << __LINE__ << " [" << #x << "] = ["; \
_print(x); \
cerr << "\e[39m" << endl;
#else
#define dbg(x...)
#endif
//#define int ll
typedef pair<int,int> pi;
pi pos[100000];
int a[102][102];
int N,M;
#define mp make_pair
vector<tuple<int,int,int>> ans;
void print_(){
rep(i,N){
rep(j,M) cout << a[i][j] << " \n"[j==M-1];
}
}
void rotate(pi x, pi y, pi z ){
assert(x.ff == y.ff && y.ss==z.ss);
int vx = a[x.ff][x.ss], vy=a[y.ff][y.ss], vz = a[z.ff][z.ss];
int i1 = x.ff, i2 = z.ff, j1 = x.ss, j2 = y.ss;
ans.pb({1,i1,(j2-j1+M)%M});
ans.pb({2,j2,(i2-i1+N)%N});
ans.pb({1,i1,(j1-j2+M)%M});
ans.pb({2,j2,(i1-i2+N)%N});
// x y z -> z x y
a[i1][j1] = vz;
a[i1][j2] = vx;
a[i2][j2] = vy;
pos[vz] = make_pair(i1,j1);//i1,j1};
pos[vx] = make_pair(i1,j2);//{i1,j2};
pos[vy] = make_pair(i2,j2);//{i2,j2};
//cout << i1 << " " << j1 << " " << i1 << " " << j2 << " " << i2 << " " << j2 << "\n";
//print_();
};
void solve(){
cin >> N >> M;
rep(i,N){
rep(j,M){
int x; cin >> x;
pos[x] = make_pair(i,j);
a[i][j] = x;
}
}
for(int i=0; i<=M*(N-1)-2; i++){
dbg(i,i+M);
if(i!=0 && i%M==0) i+=M;
int x = i/M, y = i%M, nx = pos[i].ff, ny = pos[i].ss;
if(nx==x && ny==y) x=x;
else if(x==nx){
assert(ny>=y);
rotate(mp(x,y),mp(x,ny),mp(x+1,ny));
rotate(mp(x,y),mp(x,ny),mp(x+1,ny));
//continue;
}
else if(ny<y){
rotate(mp(nx,ny),mp(nx,y),mp(x,y));
rotate(mp(nx,ny),mp(nx,y),mp(x,y));
//continue;
}
else if(ny==y){
int nyy = (ny+1)%M;
rotate(mp(nx,nyy),mp(nx,ny),mp(x,y));
//continue;
}
else if(ny>y){
rotate(mp(x,y),mp(x,ny),mp(nx,ny));
//continue;
}
int ii = i+M;
x = ii/M, y = ii%M, nx = pos[ii].ff, ny = pos[ii].ss;
dbg(x,y,nx,ny);
if(nx==x && ny==y) x=x;
else if(nx < x){
dbg(ii,x,y,nx,ny);
assert(ny>y);
rotate(mp(x,y),mp(x,ny),mp(nx,ny));
}
else if(x==nx){
assert(ny>=y);
rotate(mp(x,y),mp(x,ny),mp(x-1,ny));
rotate(mp(x,y),mp(x,ny),mp(x-1,ny));
//continue;
}
else if(ny<y){
rotate(mp(nx,ny),mp(nx,y),mp(x,y));
rotate(mp(nx,ny),mp(nx,y),mp(x,y));
//continue;
}
else if(ny==y){
int nyy = (ny+1)%M;
rotate(mp(nx,nyy),mp(nx,ny),mp(x,y));
//continue;
}
else if(ny>y){
rotate(mp(x,y),mp(x,ny),mp(nx,ny));
//continue;
}
}
//for(int i=M*(N-2); i<M*(N-1)-1; i++){
// int x = i/M, y = i%M, nx = pos[i].ff, ny = pos[i].ss;
// if(x!=nx || y!=ny){
//
// }
//}
// N*M-1, N*M-2;
if(a[N-1][M-1]!=N*M-1){
rep(i,M-1){
ans.pb({1,N-1,M-1});
ans.pb({2,M-1,1});
ans.pb({1,N-1,1});
ans.pb({2,M-1,N-1});
ans.pb({1,N-1,M-1});
}
}
cout << ans.size() << "\n";
for(auto [i,j,k] : ans) cout << i << " " << j+1 << " " << k << "\n";
}
int32_t main(void) {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int t=1; //cin >> t;
while(t--) solve();
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |