To solve this problem, we need to map a single index (Cách 1) to a pair of coordinates (Cách 2) on a hexagonal grid, resembling a honeycomb structure.
Problem Breakdown
-
Hexagonal Layers: The grid is composed of concentric hexagonal layers around the origin (0,0).
- Layer 0: Contains only the origin
(0,0) with index 1.
- Layer 1: Surrounding the origin, it has
6 cells with indices from 2 to 7.
- Layer 2: The next layer contains
12 cells, with indices from 8 to 19, and so on.
-
Determining the Layer: Given an index N, we need to determine which layer it belongs to. Each layer k has a total number of cells up to it given by 1 + 3 * k * (k + 1). We can use binary search to find the smallest k such that 1 + 3 * k * (k + 1) >= N.
-
Calculating Coordinates:
- Starting Point: For a given layer
k, the starting position is (0, k).
- Direction Traversal: There are six possible directions to traverse the hexagonal grid. For each side of the hexagon, we move in a specific direction.
- Offset Calculation: Once the layer is determined, calculate the offset within that layer to find the exact coordinates.
Implementation Steps
- Binary Search: Find the appropriate layer
k for the given index N.
- Offset Identification: Determine the position within the layer by calculating the offset from the start of the layer.
- Directional Movement: Traverse the hexagon's sides in the predefined six directions to calculate the exact coordinates
(x, y).
C++ Implementation
Here's the complete C++ program that reads from BEE.INP and writes the results to BEE.OUT:
#include <bits/stdc++.h>
using namespace std;
// Define directions as per the problem's traversal order
const pair<int, int> directions[6] = { {-1,0}, {0,-1}, {1,-1}, {1,0}, {0,1}, {-1,1} };
// Function to find the layer k for a given N using binary search
int find_layer(long long N){
if(N ==1) return 0;
int left =1, right = 2000000000; // sufficient upper limit
while(left < right){
int mid = left + (right - left)/2;
// Compute total up to layer mid
// 1 + 3*mid*(mid+1)
// To prevent overflow, use long long
long long total = 1 + 3LL * mid * (mid +1);
if(total >= N){
right = mid;
}
else{
left = mid +1;
}
}
return left;
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
// Read all input into a vector
vector<long long> test_cases;
long long N;
while(cin >> N){
test_cases.push_back(N);
}
// Process each test case
for(auto N : test_cases){
if(N ==1){
cout << "0 0\n";
continue;
}
// Find layer k
int k = find_layer(N);
// Compute start index S of layer k
// S =1 +3*(k-1)*k +1 =3*(k-1)*k +2
long long S;
if(k ==0){
S =1;
}
else{
S = 3LL * (long long)(k-1) * (long long)k +2;
}
// Compute offset
long long offset = N - S;
// Each side has k cells
// s= offset /k (0-based)
// t= offset %k
long long s = offset / (long long)k;
long long t = offset % (long long)k;
// Initialize position at (0,k)
long long posx =0, posy =k;
// Add directions[0 to s-1] *k
for(int i=0; i < s; ++i){
posx += (long long)directions[i].first * (long long)k;
posy += (long long)directions[i].second * (long long)k;
}
// Add directions[s] *t
posx += (long long)directions[s].first * (long long)t;
posy += (long long)directions[s].second * (long long)t;
// Output
cout << posx << " " << posy << "\n";
}
}
Explanation of the Code
- Directions: The six possible movement directions on the hex grid are defined in the
directions array.
- Layer Calculation: The
find_layer function determines the layer k to which the index N belongs using binary search.
- Offset and Position Calculation:
- Calculate the starting index
S of layer k.
- Determine the offset within the layer.
- Identify the specific side
s and the step t on that side.
- Traverse the appropriate directions based on
s and t to find the coordinates.
- Output: The coordinates
(x, y) are printed for each test case.
Example
Based on the example provided:
- Input:
1
2
3
4
5
- Output:
0 0
0 1
-1 1
-1 0
0 -1
This output matches the expected coordinates for the given indices, demonstrating the correctness of the implementation.