HDU多校4 - 6813 Last Problem(构造)

题目链接:点击查看

题目大意:给出一个无限大的二维平面,需要在平面内进行染色,每次可以选择一个点 ( x , y ) 将其染色为 n 的前提是,相邻四个格子必须分别已经染了 n - 1 , n - 2 , n - 3 , n - 4 四种颜色,染色可以覆盖,构造出一种合理的染色方案,使得可以在平面上出现颜色 n 

题目分析:参考博客:https://www.cnblogs.com/graytido/p/13406750.html

本来看题解觉得好难的一道构造题,搜到了上面大佬的博客,构造题嘛,就别问那么多为什么了,能想出来就能 AC

自上而下构造如此三角形:

换句话说,对于某个时刻,若想让 ( x , y ) 变为 n ,则需要满足:

然后 dfs 搜就好了

代码:
 

#include<iostream>
#include<cstdio>
#include<string>
#include<ctime>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<stack>
#include<climits>
#include<queue>
#include<map>
#include<set>
#include<sstream>
#include<cassert>
#include<bitset>
using namespace std;
  
typedef long long LL;
  
typedef unsigned long long ull;
  
const int inf=0x3f3f3f3f;
 
const int N=2e3+100;

const int b[5][2]={0,0,0,-1,-1,0,1,0,0,1};

int maze[N][N];

int cnt=0;

void dfs(int x,int y,int val)
{
	if(val<=0)
		return;
	for(int i=1;i<=4;i++)
	{
		int xx=x+b[i][0],yy=y+b[i][1];
		if(maze[xx][yy]!=val-i)
			dfs(xx,yy,val-i);
	}
	maze[x][y]=val;
	printf("%d %d %d\n",x,y,val);
}

int main()
{
#ifndef ONLINE_JUDGE
//  freopen("data.in.txt","r",stdin);
//  freopen("data.out.txt","w",stdout);
#endif
//  ios::sync_with_stdio(false);
	int n;
	scanf("%d",&n);
	dfs(1000,1000,n);















    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_45458915/article/details/107704762