hihocoder1322希尔伯特曲线(163周)

hihocoder1322 : 希尔伯特曲线(163周)

题目链接

思路:

看图,对每个Hn迭代到H(n-1) 直到迭代到1就ok,判断在哪个区间就好了。一定一定要注意数据的范围!!

ac代码:

// hihocoder1324.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"

#include<iostream>
#include<cstdio>
#include<string>
#include<algorithm>
#include<queue>
#include<vector>
#include<cstring>
#include<stdio.h>
#include<map>
#include<cmath>
#include<unordered_map>
#include<unordered_set>

using namespace std;


int main()
{
	int n, x, y;
	scanf("%d %d %d", &n, &x, &y);
	long long mypow[31];
	mypow[0] = 1;
	for (int i = 1; i <= n; i++)
	{
		mypow[i] = 2 * mypow[i - 1];
	}

	long long res = 0, temp;
	for (int i = n; i >= 1; i--)
	{
		if (x > mypow[i - 1] && y > mypow[i - 1])              //3
		{
			res += mypow[i] * mypow[i] / 2;

			x -= mypow[i - 1];
			y -= mypow[i - 1];
		}
		else if (x <= mypow[i - 1] && y > mypow[i - 1])        //2
		{
			res += mypow[i] * mypow[i] / 4;

			y -= mypow[i - 1];
		}
		else if (x <= mypow[i - 1] && y <= mypow[i - 1])       //1
		{
			swap(x, y);

		}
		else if (x > mypow[i - 1] && y <= mypow[i - 1])        //4
		{
			res += mypow[i] * mypow[i] * 3 / 4;

			temp = x;
			x = mypow[i - 1] + 1 - y;
			y = 2 * mypow[i - 1] + 1 - temp;
		}
	}

	printf("%lld
", res + 1);
		
	
    return 0;
}


原文地址:https://www.cnblogs.com/weedboy/p/7360169.html