跳到正文

题解

P1299

035966_L3约 6 分钟
原文
文章信息与版本

文章信息

发布
2022年6月29日
收录
2025年12月12日
更新
2026年11月16日
篇幅
约 6 分钟
获赞
3
题目
P1299

版本

  1. v1当前

这里提供细节最少的解法。


首先,转化坐标。为了方便可以这么转化:

原坐标转化后的坐标
−1000-100066
−999-99988
…\ldots…\ldots
−x-x−2x+2006-2x+2006
…\ldots…\ldots
−1-120042004
0020062006
1120082008
…\ldots…\ldots
xx2x+20062x+2006
…\ldots…\ldots
99999940044004
1000100040064006

当然,原坐标 xx 也可根据个人喜好转化成 2x+20042x+2004,2x+20102x+2010,2x+20222x+2022 等。

于是,奇数坐标被空了下来,于是可以用它表示小块。例如,样例中裁出的小方块可以用 (2007,2007)(2007,2007) 表示。

于是,处理完后可直接上 BFS(DFS 会因递归层数过多而 MLE)+ Floodfill(洪水填充算法),但需要注意下面的细节:

需要将留在纸上的部分单独优先处理,它是正常的 4\color{red}4 连通。在转化后的坐标中,点 (x,y)(x,y) 的相邻点有:

  • (x,y+1).\color{blue}(x,y+1).
  • (x,y−1).\color{blue}(x,y-1).
  • (x+1,y).\color{blue}(x+1,y).
  • (x−1,y).\color{blue}(x-1,y).

但是,被裁下的部分是 8\color{red}8 连通(例如,(2007,2007)(2007,2007) 与 (2007,2009)(2007,2009) 实际上相邻)。请注意,这不是通常意义下的 88 连通。在转化后的坐标中,点 (x,y)(x,y) 的相邻点有:

  • (x,y+1).\color{blue}(x,y+1).
  • (x,y−1).\color{blue}(x,y-1).
  • (x+1,y).\color{blue}(x+1,y).
  • (x−1,y).\color{blue}(x-1,y).
  • (x,y+2).\color{red}(x,y+2).
  • (x,y−2).\color{red}(x,y-2).
  • (x+2,y).\color{red}(x+2,y).
  • (x−2,y).\color{red}(x-2,y).

最后,由于队列过大,开 int 会 MLE,而坐标数值不大(显然小于 50005000),因此要用 short 卡空间。

然后,就没有其他细节了。


AC Code:

(压缩后仅 798 Bytes,或成本题最短解?)

C++
#include<iostream>
using namespace std;
bool s[4012][4012];
short f(short x)//转化坐标
{
	return 2*x+2006;
}
short queuex[17000012],queuey[17000012];//队列
int fff=1,t=0;//注意要开 int
void ff(bool mw)
/*
mv = 0:留在纸上的部分
mv = 1:被裁下的部分
*/
{
	while(fff<=t)
	{
		short cnt=t-fff+1;
		for(short i=1;i<=cnt;i++)
		{
			short xx=queuex[fff],yy=queuey[fff];
			fff++;
			s[xx][yy]=true;
			if(!s[xx+1][yy]) t++,queuex[t]=xx+1,queuey[t]=yy,s[xx+1][yy]=true;
			if(!s[xx-1][yy]) t++,queuex[t]=xx-1,queuey[t]=yy,s[xx-1][yy]=true;
			if(!s[xx][yy-1]) t++,queuex[t]=xx,queuey[t]=yy-1,s[xx][yy-1]=true;
			if(!s[xx][yy+1]) t++,queuex[t]=xx,queuey[t]=yy+1,s[xx][yy+1]=true;
			if(mw)
			{
				if(!s[xx+2][yy]) t++,queuex[t]=xx+2,queuey[t]=yy,s[xx+2][yy]=true;
				if(!s[xx-2][yy]) t++,queuex[t]=xx-2,queuey[t]=yy,s[xx-2][yy]=true;
				if(!s[xx][yy-2]) t++,queuex[t]=xx,queuey[t]=yy-2,s[xx][yy-2]=true;
				if(!s[xx][yy+2]) t++,queuex[t]=xx,queuey[t]=yy+2,s[xx][yy+2]=true;
			}
		}
	}
}
int main()
{
	short n;
	cin>>n;
	for(short i=1;i<=n;i++)
	{
		short a,b,c,d;
		cin>>a>>b>>c>>d;
		if(a==c)//纵向
		{
			for(short i=f(min(b,d));i<=f(max(b,d));i++)
				s[f(a)][i]=true;
		}
		if(b==d)//横向
		{
			for(short i=f(min(a,c));i<=f(max(a,c));i++)
				s[i][f(b)]=true;
		}
	}
	for(short i=0;i<=4011;i++)//外圈围两层(一层不够),防止越界
		s[0][i]=s[i][0]=s[4011][i]=s[i][4011]=s[1][i]=s[i][1]=s[4010][i]=s[i][4010]=true;
	short ans=0;
	t++;
	queuex[t]=2;
	queuey[t]=2;
	ff(0);//留在纸上的部分
	for(short i=3;i<=4009;i++)
		for(short j=2;j<=4009;j++)
		{
			if(!s[i][j]) ans++;
			if(!s[i][j])
			{
				t++;
				queuex[t]=i;
				queuey[t]=j;
				ff(1);//被裁下的部分
			}
		}
	cout<<ans;
	return 0;
}
原文
存档评论2 条抓取于