programming-challenges Bee Maja (111204) 题解

本题可以通过找规律解决,事实上每个格子的值可以通过相邻的两个格子算出来的。我画了一个大大的图,其实也花了不少时间。

#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <map>
#include <set>
#include <stack>
#include <assert.h>
#include <algorithm>
#include <math.h>
#include <ctime>
#include <functional>
#include <string.h>
#include <stdio.h>
#include <numeric>
#include <float.h>

using namespace std;

const int MAXNUM = 100000; 

struct Node {
	int i1, i2; 
	Node() : i1(0), i2(0) {}
	Node(int ai1, int ai2) : i1(ai1), i2(ai2) {}
};

Node addNode(const Node &n1, const Node &n2, int level) {
	Node newNode; 
	if (n1.i1 == level && n2.i1 == level) {
		newNode.i1 = level + 1; 
	}
	else if (n1.i1 == -level && n2.i1 == -level) {
		newNode.i1 = -level - 1; 
	}
	else if (n1.i1 > 0 || n2.i1 > 0) {
		newNode.i1 = max(n1.i1, n2.i1);
	}
	else {
		newNode.i1 = min(n1.i1, n2.i1);
	}

	if (n1.i2 == level && n2.i2 == level) {
		newNode.i2 = level + 1;
	}
	else if (n1.i2 == -level && n2.i2 == -level) {
		newNode.i2 = -level - 1;
	}
	else if (n1.i2 > 0 || n2.i2 > 0) {
		newNode.i2 = max(n1.i2, n2.i2);
	}
	else {
		newNode.i2 = min(n1.i2, n2.i2);
	}

	return newNode;
}


vector<Node> vNodes; 

int main() {
	vNodes.push_back(Node(0, 0));
	vNodes.push_back(Node(0, 1));
	vNodes.push_back(Node(-1, 1));
	vNodes.push_back(Node(-1, 0));
	vNodes.push_back(Node(0, -1));
	vNodes.push_back(Node(1, -1));
	vNodes.push_back(Node(1, 0));

	int minorLoop = 0; 
	int lastMinorLoop = 0; 
	while (vNodes.size() <= MAXNUM) {
		lastMinorLoop++; 
		minorLoop = lastMinorLoop * 6; 

		int startIndex = vNodes.size() - minorLoop; 

		for (int i = 1; i <= minorLoop; i++) {
			int delta = i - 1; 
			if (i == 1) {
				vNodes.push_back(addNode(vNodes[startIndex], vNodes.back(), lastMinorLoop));
			}
			else {
				vNodes.push_back(addNode(vNodes[startIndex + delta], vNodes[startIndex + delta - 1], lastMinorLoop));
			}

			if ((i) % lastMinorLoop == 0) {
				vNodes.push_back(addNode(vNodes[startIndex + delta],  vNodes[startIndex + delta], lastMinorLoop)); 
			}

			if (vNodes.size() > MAXNUM) break;
		}
	}

	int nodeId = 0; 

	while (cin >> nodeId) {
		cout << vNodes[nodeId - 1].i1 << " " << vNodes[nodeId - 1].i2 << endl;
	}

	return 0; 
}


你可能感兴趣的:(Algorithm,C++)