#分治#洛谷 1228 地毯填补问题
分析
考虑迷宫规格明示分治,
也就是每次分成左上左下右上右下处理
代码
#include <cstdio>
#define rr register
using namespace std;
int n,x,y;
inline void dfs(int lx,int rx,int ly,int ry,int zx,int zy){
if (lx+1==rx&&ly+1==ry){
if (zx==lx&&zy==ly) printf("%d %d 1\n",rx,ry);
if (zx==lx&&zy==ry) printf("%d %d 2\n",rx,ly);
if (zx==rx&&zy==ly) printf("%d %d 3\n",lx,ry);
if (zx==rx&&zy==ry) printf("%d %d 4\n",lx,ly);
return;
}
rr int midx=(lx+rx)>>1,midy=(ly+ry)>>1;
rr int opt=(zx>midx)<<1|(zy>midy);
switch (opt){
case 0:{
dfs(lx,midx,ly,midy,zx,zy);
printf("%d %d 1\n",midx+1,midy+1);
dfs(lx,midx,midy+1,ry,midx,midy+1);
dfs(midx+1,rx,ly,midy,midx+1,midy);
dfs(midx+1,rx,midy+1,ry,midx+1,midy+1);
break;
}
case 1:{
dfs(lx,midx,ly,midy,midx,midy);
dfs(lx,midx,midy+1,ry,zx,zy);
printf("%d %d 2\n",midx+1,midy);
dfs(midx+1,rx,ly,midy,midx+1,midy);
dfs(midx+1,rx,midy+1,ry,midx+1,midy+1);
break;
}
case 2:{
dfs(lx,midx,ly,midy,midx,midy);
dfs(lx,midx,midy+1,ry,midx,midy+1);
dfs(midx+1,rx,ly,midy,zx,zy);
printf("%d %d 3\n",midx,midy+1);
dfs(midx+1,rx,midy+1,ry,midx+1,midy+1);
break;
}
case 3:{
dfs(lx,midx,ly,midy,midx,midy);
dfs(lx,midx,midy+1,ry,midx,midy+1);
dfs(midx+1,rx,ly,midy,midx+1,midy);
dfs(midx+1,rx,midy+1,ry,zx,zy);
printf("%d %d 4\n",midx,midy);
break;
}
}
}
signed main(){
scanf("%d%d%d",&n,&x,&y),n=1<<n;
dfs(1,n,1,n,x,y); return 0;
}