利用分治法解决棋盘覆盖问题
2017-06-15 16:56 tlnshuju 阅读(547) 评论(0) 编辑 收藏 举报1.问题描写叙述:点击打开链接
2.代码:
#define _CRT_SECURE_NO_WARNINGS #include<iostream> #include<algorithm> #include<cassert> #include<string> #include<sstream> #include<set> #include<vector> #include<stack> #include<map> #include<queue> #include<deque> #include<cstdlib> #include<cstdio> #include<cstring> #include<cmath> #include<ctime> #include<cctype> #include<functional> using namespace std; #define me(s) memset(s,0,sizeof(s)) #define pb push_back typedef long long ll; typedef unsigned int uint; typedef unsigned long long ull; typedef pair <int, int> P; const int N = 1 << 11; int A[N][N]; int n, a, b; int idx; void solve(int r1, int r2, int c1, int c2, int x, int y) { if (r2-r1==1)return; int t = idx++; int mr = (r1 + r2)/2, mc = (c1 + c2)/2; if (x<mr&&y<mc)solve(r1, mr, c1, mc, x, y); else solve(r1, mr, r2, mc, mr-1,mc-1), A[mr-1][mc-1] = t; if (x<mr&&y >= mc)solve(r1, mr, mc, c2, x, y); else solve(r1, mr, mc, c2, mr-1,mc), A[mr-1][mc] = t; if (x >= mr&&y<mc)solve(mr, r2, c1, mc, x, y); else solve(mr, r2, c1, mc, mr,mc-1), A[mr][mc-1] = t; if (x >= mr&&y >= mc)solve(mr, r2, mc, c2, x, y); else solve(mr, r2, mc, c2, mr,mc), A[mr][mc] = t; } int main() { while (~scanf("%d%d%d", &n, &a, &b)) { me(A); idx = 1; solve(0, (1 << n), 0, (1 << n), a - 1, b - 1); A[a - 1][b - 1] = 0; for (int i = 0; i<(1 << n); i++) { for (int j = 0; j<(1 << n); j++) { printf("%2d%c", A[i][j], " \n"[j == (1 << n) - 1]); } puts(""); } } }