Tổ ong có một dạng cấu trúc đặc biệt, gồm những hình lục giác đều (tạm gọi là các túi mật) xếp kề nhau. Có hai cách thông thường để xác định vị trí một túi mật trong tổ ong:
• Cách 1: sử dụng một chỉ số
• Cách 2: sử dụng hệ toạ độ gồm hai chỉ số
Quan sát ví dụ sau để biết cách đánh chỉ số của hai cách:
Cách 1 Cách 2
Yêu cầu: Cho trước một toạ độ túi mật được thể hiện bằng cách 1. Hãy viết một chương trình (BEE.CPP) thể hiện toạ độ trên bằng cách 2.
Dữ liệu: Vào từ file văn bản BEE.INP, gồm nhiều test, mỗi test trên một dòng là một số nguyên thể hiện toạ độ của túi mật bằng cách 1.
Kết quả: Ghi ra file văn bản BEE.OUT, mỗi kết quả trên một dòng, cho biết toạ độ được thể hiện bằng cách 2 tương ứng của các túi mật trong dữ liệu vào.
Ví dụ:
BEE.INP BEE.OUT
1
2
3
4
5 0 0
0 1
-1 1
-1 0
0 -1
solve by c++
Assistant
To solve the problem of converting a single index (Cách 1) to a pair of axial coordinates (Cách 2) in a hexagonal grid structure, we can follow a systematic approach. Below is a detailed explanation of the solution along with the C++ implementation.
Problem Breakdown
Hexagonal Grid Structure:
The grid consists of concentric layers or "rings" around the central cell.
The center is indexed as 1 with coordinates (0, 0).
Each subsequent ring k adds 6k cells around the previous ring.
Index to Coordinates Conversion:
Identify the Ring (k):
For a given index n, find the smallest k such that 1 + 3*k*(k+1) >= n.
Calculate the Offset:
Determine the position of n within ring k by calculating the offset from the start of the ring.
Determine the Side and Position on the Side:
Each ring has six sides. Determine which side of the ring n falls on and its position on that side.
Compute Coordinates:
Starting from the initial position (0, k), move along the sides to reach the exact coordinates.
C++ Implementation
Below is the C++ program that implements the above logic. The program reads input from a file named BEE.INP and writes the output to BEE.OUT.
cpp
#include <bits/stdc++.h>
using namespace std;
int main(){
// Open input and output files
ifstream fin("BEE.INP");
ofstream fout("BEE.OUT");
long long n;
// Read each test case from the input file
while(fin >> n){
if(n ==1){
fout << "0 0\n";
continue;
}
// Find the layer k where the cell n is located
long long k=1;
while(1){
long long Ck =1 + 3*k*(k+1);
if(n <=Ck) break;
k++;
}
// Calculate the cumulative cells up to the previous layer
long long Ckm1 =1 + 3*(k-1)*k;
long long offset =n - Ckm1;
// Determine the side and position on the side
long long side = (offset -1)/k;
long long pos_in_side = (offset -1)%k;
// Define direction vectors for the six sides
vector<pair<long long, long long>> dirs = { {-1,0}, {0,-1}, {1,-1}, {1,0}, {0,1}, {-1,1} };
// Initialize coordinates at the start of the layer
long long x=0, y=k;
// Move to the correct side
for(long long i=0;i<side;i++){
x += dirs[i].first *k;
y += dirs[i].second *k;
}
// Move to the correct position on the side
x += dirs[side].first * pos_in_side;
y += dirs[side].second * pos_in_side;
// Output the result
fout << x << " " << y << "\n";
}
return 0;
}
Explanation of the Code
File Handling:
The program uses ifstream to read from BEE.INP and ofstream to write to BEE.OUT.
Finding the Correct Layer (k):
Starting from k = 1, the program checks if the index n falls within the cumulative count 1 + 3*k*(k+1).
It increments k until the condition is satisfied.
Calculating Offset and Determining Side:
The offset is calculated as n - (1 + 3*(k-1)*k).
The side is determined by integer division of (offset - 1) by k.
The position on the side is the remainder of the same division.
Direction Vectors:
The six directions correspond to the six sides of the hexagon, and they are defined in the dirs vector.
Computing Coordinates:
Starting from (0, k), the program moves along the sides based on the side number and position on the side to reach the final coordinates.
Output:
The final (x, y) coordinates are written to BEE.OUT.
Example Verification
Using the example provided:
Input:
code
1
2
3
4
5
Output:
code
0 0
0 1
-1 1
-1 0
0 -1
This matches the expected output, confirming the correctness of the solution.