版本比较
题解:CF1765I Infinite Chess
正文
增加 19 行删除 19 行
30 行未改动313233343536353637383940414041424344454645464748495051505152535455565657585960616263646565666768697071717273737475757677787952 行未改动13213313413513613713613713813914014114214314214314414514614714814915015115215315315415515615715715815916016110 行未改动30 行未改动
priority_queue<node> qu; // 优先队列用于Dijkstra算法
// 插入函数,用于在列数的两边插入多个值进行离散化处理。void insert(int x) { for (int i = -18; i <= 18; i++) cnt++, c[cnt] = x + i; void insert(int x) { for (int i = -18; i <= 18; i++) cnt++, c[cnt] = x + i;}
// 获取x在离散化后的数组中的位置。int GET(int x) { return lower_bound(c + 1, c + cnt + 1, x) - c; int GET(int x) { return lower_bound(c + 1, c + cnt + 1, x) - c;}
// 更新x的离散化位置。void update(int& x) { x = GET(x); void update(int& x) { x = GET(x);}
// 判断坐标是否在合法范围内bool judge(int x, int y) { return 1 <= x && x <= cnt && 1 <= y && y <= 8; bool judge(int x, int y) { return 1 <= x && x <= cnt && 1 <= y && y <= 8;}
// 将坐标(x, y)加入队列。void add(int x, int y, int d) { if (judge(x, y) && ban[x][y] == 0 && dis[x][y] > d) if (judge(x, y) && ban[x][y] == 0 && dis[x][y] > d) dis[x][y] = d, qu.push((node){x, y, d});}
int main() { ios::sync_with_stdio(false); cin.tie(0); cin >> sx >> sy >> ex >> ey >> n; // 输入起点、终点和白棋数量。 insert(sx), insert(ex); // 插入起点和终点的列数。 for (int i = 1; i <= n; i++) for (int i = 1; i <= n; i++) cin >> z[i] >> x[i] >> y[i], insert(x[i]); // 插入白棋的位置。 sort(c + 1, c + cnt + 1); cnt = unique(c + 1, c + cnt + 1) - (c + 1); // 离散化。 update(sx); update(ex); for (int i = 1; i <= n; i++) for (int i = 1; i <= n; i++) update(x[i]); for (int i = 1; i <= n; i++) for (int i = 1; i <= n; i++) cover[x[i]][y[i]] = 1; // 标记被占据的格子。 for (int i = 1; i <= n; i++) for (int i = 1; i <= n; i++) ban[x[i]][y[i]] = 1; // 标记禁止的格子。 for (int i = 1; i <= n; i++) { // 根据棋子的类型标记被攻击的格子。 if (z[i] == 'K')52 行未改动 break; else ban[x[i] - mutli][y[i] - mutli] = true; } if (z[i] == 'N') for (int j = 0; j < 8; j++) if (z[i] == 'N') for (int j = 0; j < 8; j++) if (judge(x[i] + dx[j], y[i] + dy[j])) ban[x[i] + dx[j]][y[i] + dy[j]] = true; }
// 初始化距离数组 for (int i = 0; i <= cnt + 1; i++) for (int j = 0; j <= 9; j++) for (int i = 0; i <= cnt + 1; i++) for (int j = 0; j <= 9; j++) dis[i][j] = 1e9; dis[sx][sy] = 0, ban[ex][ey] = 0; qu.push((node){sx, sy, 0});
// Dijkstra算法 while (qu.size()) { node top = qu.top(); qu.pop(); int nx = top.x, ny = top.y, dist = top.dist; if (dis[nx][ny] ^ dist) continue; if (nx == ex && ny == ey) if (nx == ex && ny == ey) return cout << dis[nx][ny] << '\n', 0; add(nx, ny - 1, dist + 1); add(nx, ny + 1, dist + 1); add(nx - 1, ny, dist + c[nx] - c[nx - 1]); add(nx - 1, ny, dist + c[nx] - c[nx - 1]); add(nx + 1, ny, dist + c[nx + 1] - c[nx]); // 上下左右 if(c[nx] - c[nx - 1] == 1) { add(nx - 1, ny - 1, dist + 1); add(nx - 1, ny + 1, dist + 1);10 行未改动