Submission
Status:
[PP-SSSSSSS]
Subtask/Task Score:
{0/100}
Score: 0
User: ZanTy
Problemset: Path Finding
Language: cpp
Time: 0.002 second
Submitted On: 2026-10-09 13:01:16
#include <iostream>
#include <vector>
#include <string>
using namespace std;
int main() {
// เพิ่มความเร็วในการรับส่งข้อมูล (Fast I/O)
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n, q;
if (!(cin >> n >> q)) return 0;
vector<int> x(q), y(q);
bool out_of_range = false;
// รับค่าตำแหน่งทั้งหมดและเช็กขอบเขต
for (int i = 0; i < q; i++) {
cin >> x[i] >> y[i];
if (x[i] < 0 || x[i] >= n || y[i] < 0 || y[i] >= n) {
out_of_range = true;
}
}
if (out_of_range) {
cout << "Out of range\n";
return 0;
}
// สร้างแผนที่เริ่มต้นด้วย '_'
vector<string> arr(n, string(n, '_'));
// วางตัวอักษร A, B, C... ลงในแผนที่ก่อน
for (int i = 0; i < q; i++) {
arr[x[i]][y[i]] = (char)('A' + i);
}
// เริ่มลากเส้นเชื่อมโยงแต่ละจุด
for (int i = 0; i < q - 1; i++) {
int x1 = x[i], y1 = y[i];
int x2 = x[i + 1], y2 = y[i + 1];
// 1. เดินแนวนอนก่อน (แกน Y เปลี่ยน) -> ลากเส้นแบบไม่รวมจุดเริ่มต้นและจุดปลายทาง
if (y2 > y1) {
for (int j = y1 + 1; j < y2; j++) {
arr[x1][j] = '>';
}
} else if (y2 < y1) {
for (int j = y1 - 1; j > y2; j--) {
arr[x1][j] = '<';
}
}
// 2. เดินแนวตั้งตาม (แกน X เปลี่ยน) ไปยังจุดปลายทางแนวนอน (y2)
if (y2 == y1) {
// กรณีไม่มีการเดินแนวนอน: ไม่ต้องทับจุดเริ่มต้น (x1) ให้เริ่มเดินจากจุดถัดไป
if (x2 > x1) {
for (int e = x1 + 1; e < x2; e++) {
arr[e][y2] = 'v';
}
} else if (x2 < x1) {
for (int e = x1 - 1; e > x2; e--) {
arr[e][y2] = '^';
}
}
} else {
// มีการเดินแนวนอนมาก่อน: ต้องทับจุดเลี้ยว (x1, y2) ด้วยสัญลักษณ์แนวตั้งด้วย
if (x2 > x1) {
for (int e = x1; e < x2; e++) {
arr[e][y2] = 'v';
}
} else if (x2 < x1) {
for (int e = x1; e > x2; e--) {
arr[e][y2] = '^';
}
}
}
}
// แสดงผลลัพธ์แผนที่ทั้งหมด
for (int i = 0; i < n; i++) {
cout << arr[i] << "\n";
}
return 0;
}