题解:P4356 [CERC2015] Looping Labyrinth
Ramona216
·
·
题解
#include <bits/stdc++.h>
using namespace std;
// 定义最大网格大小
const int MAX_N = 105;
const int MAX_M = 105;
// 网格状态:true 表示障碍物('#'),false 表示可通行('.')
bool obstacle[MAX_N][MAX_M];
// 坐标转换结构体,包含块坐标和块内坐标
struct Coordinate {
int blockX, blockY; // 块的坐标
int innerX, innerY; // 块内的坐标
};
// 计算全局坐标 (x, y) 对应的块坐标和块内坐标
Coordinate calculateCoordinate(int x, int y, int n, int m) {
Coordinate coord;
// 计算块坐标,对于负坐标进行调整
coord.blockX = (x >= 0) ? (x / n) : ((x + 1) / n - 1);
coord.blockY = (y >= 0) ? (y / m) : ((y + 1) / m - 1);
// 计算块内坐标,并确保其在合法范围内
coord.innerX = x - coord.blockX * n;
assert(coord.innerX >= 0 && coord.innerX < n);
coord.innerY = y - coord.blockY * m;
assert(coord.innerY >= 0 && coord.innerY < m);
return coord;
}
// 全局变量用于记录方向向量
int directionU = 0, directionV = 0;
// 插入并更新方向向量
void insertDirection(int deltaU, int deltaV) {
// 如果已经处于不可确定状态,直接返回
if (directionU == -998244353) return;
// 忽略无意义的方向
if (deltaU == 0 && deltaV == 0) return;
// 标准化方向向量,使其唯一
if (deltaU < 0) {
deltaU = -deltaU;
deltaV = -deltaV;
} else if (deltaU == 0 && deltaV < 0) {
deltaV = -deltaV;
}
// 约简方向向量
int gcd_val = __gcd(deltaU, abs(deltaV));
deltaU /= gcd_val;
deltaV /= gcd_val;
// 如果新方向与当前方向相同,则无需更改
if (deltaU == directionU && deltaV == directionV) return;
// 如果已经有一个方向,且新方向不同,则设为不可确定状态
if (directionU || directionV) {
directionU = directionV = -998244353; // 表示冲突状态
}
// 否则,设置新的方向
else {
directionU = deltaU;
directionV = deltaV;
}
}
// 访问标记数组和块的父坐标
bool visited[MAX_N][MAX_M];
pair<int, int> parentBlock[MAX_N][MAX_M];
// 检查节点是否有效,并更新访问状态
bool isValid(const Coordinate& coord, int n, int m) {
// 如果块内有障碍物,则无效
if (obstacle[coord.innerX][coord.innerY]) return false;
// 如果该块未被访问过,标记为已访问并记录父块坐标
if (!visited[coord.innerX][coord.innerY]) {
visited[coord.innerX][coord.innerY] = true;
parentBlock[coord.innerX][coord.innerY] = {coord.blockX, coord.blockY};
return true;
}
// 如果已访问过,更新方向向量
insertDirection(coord.blockX - parentBlock[coord.innerX][coord.innerY].first,
coord.blockY - parentBlock[coord.innerX][coord.innerY].second);
return false;
}
// 四个方向的移动向量:下、上、右、左
const int directions[4][2] = { {1, 0}, {-1, 0}, {0, 1}, {0, -1} };
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, m;
cin >> n >> m;
// 读取网格信息
for (int i = 0; i < n; ++i) {
// 读取每一行,跳过非 '.' 或 '#' 的字符
char c;
cin >> c;
for (int j = 0; j < m; ++j) {
obstacle[i][j] = (c == '#');
if (j < m - 1) cin >> c; // 读取下一个字符,避免越界
}
}
// 使用 BFS 遍历所有可达的块
queue<pair<int, int>> Q;
Coordinate start = {0, 0, 0, 0};
if (isValid(start, n, m)) {
Q.emplace(0, 0);
}
while (!Q.empty()) {
pair<int, int> current = Q.front();
Q.pop();
// 尝试向四个方向移动
for (int i = 0; i < 4; ++i) {
int newX = current.first + directions[i][0];
int newY = current.second + directions[i][1];
Coordinate newCoord = calculateCoordinate(newX, newY, n, m);
if (isValid(newCoord, n, m)) {
Q.emplace(newX, newY);
}
}
}
// 处理查询
int T;
cin >> T;
while (T--) {
int x, y;
cin >> x >> y;
Coordinate queryCoord = calculateCoordinate(x, y, n, m);
// 如果查询块未被访问,输出 "no"
if (!visited[queryCoord.innerX][queryCoord.innerY]) {
cout << "no\n";
continue;
}
// 根据方向向量的不同情况进行判断
if (directionU == 0 && directionV == 0) {
// 无需方向一致性,直接比较块坐标
if (queryCoord.blockX != parentBlock[queryCoord.innerX][queryCoord.innerY].first ||
queryCoord.blockY != parentBlock[queryCoord.innerX][queryCoord.innerY].second) {
cout << "no\n";
} else {
cout << "yes\n";
}
continue;
}
if (directionU == -998244353 && directionV == -998244353) {
// 方向冲突,所有已访问的块均为可达
cout << "yes\n";
continue;
}
// 计算查询块与其父块之间的方向向量
int deltaU = queryCoord.blockX - parentBlock[queryCoord.innerX][queryCoord.innerY].first;
int deltaV = queryCoord.blockY - parentBlock[queryCoord.innerX][queryCoord.innerY].second;
// 如果无方向向量差异,直接可达
if (deltaU == 0 && deltaV == 0) {
cout << "yes\n";
continue;
}
// 标准化方向向量
if (deltaU < 0) {
deltaU = -deltaU;
deltaV = -deltaV;
} else if (deltaU == 0 && deltaV < 0) {
deltaV = -deltaV;
}
// 约简方向向量
int gcd_val = __gcd(deltaU, abs(deltaV));
deltaU /= gcd_val;
deltaV /= gcd_val;
// 比较方向向量是否一致
if (deltaU == directionU && deltaV == directionV) {
cout << "yes\n";
continue;
}
// 否则,不可达
cout << "no\n";
}
return 0;
}