#include #include #include #include #include #include #include #include #include #include #include #include #include #include #define MOD 1000000007ll #define INF 1000000000ll #define EPS 1e-10 #define REP(i,m) for(long long i=0; i<(ll)m; i++) #define FOR(i,n,m) for(long long i=n; i<(ll)m; i++) #define DUMP(a) for(long long dump=0; dump<(ll)a.size(); dump++) { cout< P; typedef long double ld; struct cell { int row; int col; }; enum dir { HORIZONTAL, VERTICAL, }; struct op { dir d; cell c; }; int n,k; int l[500]; int a[60][60]; op ret[500]; int rd() { static random_device rd; static mt19937 mt(rd()); return abs((int)mt()); } int counter(dir d, cell c, int lgt, bool flag) { if(c.row<0||c.row>=n||c.col<0||c.col>=n) return -INF; if(d==HORIZONTAL&&c.col+lgt>n) return -INF; if(d==VERTICAL&&c.row+lgt>n) return -INF; int ret=0; if(d==HORIZONTAL) REP(i,lgt) ret+=flag^!a[c.row][c.col+i]; if(d==VERTICAL) REP(i,lgt) ret+=flag^!a[c.row+i][c.col]; return ret; } void update(int updidx, op o, int lgt) { dir d=o.d; cell c=o.c; if(d==HORIZONTAL) REP(i,lgt) a[c.row][c.col+i]^=1; if(d==VERTICAL) REP(i,lgt) a[c.row+i][c.col]^=1; switch(updidx) { case 0: { d=(d==HORIZONTAL ? VERTICAL : HORIZONTAL); break; } case 1: { c.row--; break; } case 2: { c.row++; break; } case 3: { c.col--; break; } case 4: { c.col++; break; } default: ; } if(d==HORIZONTAL) REP(i,lgt) a[c.row][c.col+i]^=1; if(d==VERTICAL) REP(i,lgt) a[c.row+i][c.col]^=1; return; } op newop(op o, int lgt) { dir d=o.d; cell c=o.c; int cnt=counter(d,c,lgt,0); int maxupd=0,updidx=-1,buf=0; buf=counter((dir)!d,c,lgt,1)-cnt; if(buf>maxupd) { maxupd=buf; updidx=0; } buf=counter(d,{c.row-1,c.col},lgt,1)-cnt; if(buf>maxupd) { maxupd=buf; updidx=1; } buf=counter(d,{c.row+1,c.col},lgt,1)-cnt; if(buf>maxupd) { maxupd=buf; updidx=2; } buf=counter(d,{c.row,c.col-1},lgt,1)-cnt; if(buf>maxupd) { maxupd=buf; updidx=3; } buf=counter(d,{c.row,c.col+1},lgt,1)-cnt; if(buf>maxupd) { maxupd=buf; updidx=4; } if(updidx!=-1) update(updidx,o,lgt); switch(updidx) { case 0: return {(dir)!d,c}; case 1: return {d,{c.row-1,c.col}}; case 2: return {d,{c.row+1,c.col}}; case 3: return {d,{c.row,c.col-1}}; case 4: return {d,{c.row,c.col+1}}; default: return o; } } void input() { cin>>n>>k; REP(i,k) cin>>l[i]; REP(i,n) REP(j,n) { char c; cin>>c; a[i][j]=(int)(c-'0'); } } void output(op o, int lgt) { o.c.row++; o.c.col++; if(o.d==HORIZONTAL) cout<