跳到正文

题解

P1911的题解

kele7约 4 分钟
原文
文章信息与版本

文章信息

发布
2022年8月28日
收录
2025年12月29日
更新
2026年11月11日
篇幅
约 4 分钟
题目
P1911

版本

  1. v2当前
  2. v1

P1911——一道简单的分治题


举个例子

在 n=2,x=1,y=1n=2,x=1,y=1 时:

01
11

然而 在 n=3,x=1,y=1n=3,x=1,y=1 时:

0122
1132
4335
4455

在 n=4,x=1,y=1n=4,x=1,y=1 时:

0122771010
113278810
43356689
445516699
1414111616171818
1413111117171918
1513131220191921
1515121220202121
仔细观察一下

一个长度为 2n2^n 的表格

如图:

01224455
11324335
43351132
44550122
22100144
23111134
53342335
55442255

(中间的 00 组成一个 LL 型军营)

是四个长度为 2n−12^{n-1} 的表格组成的

上代码

C++
#include<bits/stdc++.h>
using namespace std;
int n,xx,yy,table[2000][2000],cnt,q[4000000],tot,dx[4]={0,1,0,1},dy[4]={0,0,1,1},x_[4]={1,0,1,0},y_[4]={1,1,0,0};
void f_(int x,int y,int c,int a,int b)
{
	if(!c)
	{
		return;
	}
	c--;
	cnt++;
	int t=cnt;
	for(int i=0;i<4;i++)
	{
		int sx=x+(dx[i]<<c),sy=y+(dy[i]<<c),f=sx+(x_[i]<<c)-x_[i],g=sy+(y_[i]<<c)-y_[i];
		if(a>=sx&&a<sx+(1<<c)&&b>=sy&&b<sy+(1<<c))
		{
			f_(sx,sy,c,a,b);
		}
		else
		{
			f_(sx,sy,c,f,g);
			table[f][g]=t;
		}
	}
}
int main()
{
	cin>>n>>xx>>yy;
	f_(1,1,n,xx,yy);
	for(int i=1;i<=1<<n;i++)
	{
		for(int l=1;l<=1<<n;l++)
		{
			if(i!=xx||l!=yy)
			{
				if(!q[table[i][l]])
				{
					tot++;
					q[table[i][l]]=tot;
				}
				cout<<q[table[i][l]]<<" ";
			}
			else
			{
				cout<<"0 ";
			}
			if(l==(1<<n))
			{
				cout<<"\n";
			}
		}
	}
}
原文