• 利用分治法解决棋盘覆盖问题


    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], " 
    "[j == (1 << n) - 1]);
    			}
    			puts("");
    		}
    
    	}
    }
    


  • 相关阅读:
    搞懂树状数组
    C#接口(Interface)
    C#接口(Interface)
    C#运算符的重载
    C#和C++的区别(一)
    hdu1874 畅通工程续
    hdu1874 畅通工程续
    C#多态性
    C#多态性
    C#继承
  • 原文地址:https://www.cnblogs.com/tlnshuju/p/7018785.html
Copyright © 2020-2023  润新知