残缺棋盘 【分治】

题目链接:

  

题目大意:

  N*N的棋盘,有一格残缺(R,C),问用L形状的3连格如何填满除了残缺格的棋盘。

题目思路:

  【分治】

  把问题一般化,左上角为(x,y),棋盘大小为s*s,残缺格位置为(r,c)的填充。

  现在考虑,每一步把大的棋盘等分成4块

  A B

  C D 

  不妨假设残缺格(r,c)在A中,那么B C D人为定义残缺格为靠近中心点的位置。

  这样就凑了一个L格出来,接下来只要继续对A B C D做残缺格填充即可。

  出口是棋盘大小为2*2

  1 //
  2 //by coolxxx
  3 /*
  4 #include<iostream>
  5 #include<algorithm>
  6 #include<string>
  7 #include<iomanip>
  8 #include<map>
  9 #include<stack>
 10 #include<queue>
 11 #include<set>
 12 #include<bitset>
 13 #include<memory.h>
 14 #include<time.h>
 15 #include<stdio.h>
 16 #include<stdlib.h>
 17 #include<string.h>
 18 #include<math.h>
 19 //#include<stdbool.h>
 20 #define min(a,b) ((a)<(b)?(a):(b))
 21 #define max(a,b) ((a)>(b)?(a):(b))
 22 #define swap(a,b) ((a)^=(b),(b)^=(a),(a)^=(b))
 23 */
 24 #include<bits/stdc++.h>
 25 #pragma comment(linker,"/STACK:1024000000,1024000000")
 26 #define abs(a) ((a)>0?(a):(-(a)))
 27 #define lowbit(a) (a&(-a))
 28 #define sqr(a) ((a)*(a))
 29 #define mem(a,b) memset(a,b,sizeof(a))
 30 #define eps (1e-8)
 31 #define J 10000
 32 #define mod 1000000007
 33 #define MAX 0x7f7f7f7f
 34 #define PI 3.14159265358979323
 35 #define N 104
 36 using namespace std;
 37 typedef long long LL;
 38 double anss;
 39 LL aans;
 40 int cas,cass;
 41 int n,m,lll,ans;
 42 int r,c;
 43 int a[N][N];
 44 void work(int x,int y,int s,int r,int c)
 45 {
 46     int xx,yy;
 47     if(s==2)
 48     {
 49         lll++;
 50         if(r!=x || c!=y)a[x][y]=lll;
 51         if(r!=x || c!=y+1)a[x][y+1]=lll;
 52         if(r!=x+1 || c!=y)a[x+1][y]=lll;
 53         if(r!=x+1 || c!=y+1)a[x+1][y+1]=lll;
 54         return;
 55     }
 56     s/=2;
 57     xx=x+s;
 58     yy=y+s;
 59     if(r<xx && c<yy)
 60     {
 61         work(x,y,s,r,c);
 62         work(x,yy,s,xx-1,yy);
 63         work(xx,y,s,xx,yy-1);
 64         work(xx,yy,s,xx,yy);
 65         a[xx-1][yy]=a[xx][yy-1]=a[xx][yy]=++lll;
 66     }
 67     else if(r<xx && c>=yy)
 68     {
 69         work(x,y,s,xx-1,yy-1);
 70         work(x,yy,s,r,c);
 71         work(xx,y,s,xx,yy-1);
 72         work(xx,yy,s,xx,yy);
 73         a[xx-1][yy-1]=a[xx][yy-1]=a[xx][yy]=++lll;
 74     }
 75     else if(r>=xx && c<yy)
 76     {
 77         work(x,y,s,xx-1,yy-1);
 78         work(x,yy,s,xx-1,yy);
 79         work(xx,y,s,r,c);
 80         work(xx,yy,s,xx,yy);
 81         a[xx-1][yy-1]=a[xx-1][yy]=a[xx][yy]=++lll;
 82     }
 83     else//r>=xx && c>=yy
 84     {
 85         work(x,y,s,xx-1,yy-1);
 86         work(x,yy,s,xx-1,yy);
 87         work(xx,y,s,xx,yy-1);
 88         work(xx,yy,s,r,c);
 89         a[xx-1][yy-1]=a[xx-1][yy]=a[xx][yy-1]=++lll;
 90     }
 91 }
 92 void print()
 93 {
 94     int i,j;
 95     for(i=1;i<=n;i++,puts(""))
 96         for(j=1;j<=n;j++)
 97             printf("%d	",a[i][j]);
 98 }
 99 int main()
100 {
101     #ifndef ONLINE_JUDGE
102 //    freopen("1.txt","r",stdin);
103 //    freopen("2.txt","w",stdout);
104     #endif
105     int i,j,k,l;
106     int x,y,z;
107 //    for(scanf("%d",&cass);cass;cass--)
108 //    for(scanf("%d",&cas),cass=1;cass<=cas;cass++)
109 //    while(~scanf("%s",s))
110     while(~scanf("%d",&n))
111     {
112         lll=0;
113         scanf("%d%d",&x,&y);
114         work(1,1,n,x,y);
115         print();
116     }
117     return 0;
118 }
119 /*
120 //
121 
122 //
123 */
View Code
原文地址:https://www.cnblogs.com/Coolxxx/p/6548757.html