T1 [JZOJ1432] 输油管道
题目描述
请你帮忙设计一个从城市 $M$ 到城市 $Z$ 的输油管道,现在已经把整个区域划分为 $R$ 行 $C$ 列,每个单元格可能是空的也可能是以下 $7$ 种基本管道之一:
油从城市 $M$ 流向 $Z$,'$+$' 型管道比较特殊,因为石油必须在两个方向(垂直和水平)上传输,如下图所示:
现在恐怖分子弄到了输油管道的设计图,并把其中一个单元格中的管道偷走了,请你帮忙找到偷走的管道的位置以及形状。
数据保证石油的流向是唯一的,只有一个管道跟 $M$ 和 $Z$ 相连,除此此外,保证没有多余的管道,也就是说所有的管道在加进被偷的管道后一定都会被用上。
数据保证有解而且是唯一的。
数据范围
$1 \leq R,C \leq 25$
分析
开局打150行搜索的我就是个铁憨憨,这题浪费了好多时间啊...
由于起点和终点之间的管道是唯一的,所以只要枚举每个单元格中的管道,如果管道通向了空地,那么这片空地就是被拆除管道的位置
然后通过判断该位置需要连接的方向,就可以得到管道形状
如果是起点或终点旁边的管道被拆除,就需要进行一些特判
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <queue> using namespace std; #define ll long long #define inf 0x3f3f3f3f #define N 30 int n, m, x, y, x1, y1, x2, y2, s, e; int book[4], d[4][2] = {0, 1, 1, 0, 0, -1, -1, 0}; char g[N][N]; void check(int i, int j, int dir) { int dx = i + d[dir][0], dy = j + d[dir][1]; if (g[dx][dy] == '.') x = dx, y = dy, book[(dir + 2) % 4] = 1; else if (g[dx][dy] == 'M') s = 1; else if (g[dx][dy] == 'Z') e = 1; } int main() { scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) { scanf(" %c", &g[i][j]); if (g[i][j] == 'M') x1 = i, y1 = j; if (g[i][j] == 'Z') x2 = i, y2 = j; } for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) { if (g[i][j] == '|') check(i, j, 1), check(i, j, 3); else if (g[i][j] == '-') check(i, j, 0), check(i, j, 2); else if (g[i][j] == '+') check(i, j, 0), check(i, j, 1), check(i, j, 2), check(i, j, 3); else if (g[i][j] == '1') check(i, j, 0), check(i, j, 1); else if (g[i][j] == '2') check(i, j, 0), check(i, j, 3); else if (g[i][j] == '3') check(i, j, 2), check(i, j, 3); else if (g[i][j] == '4') check(i, j, 1), check(i, j, 2); } if (!s && !e) { if (x1 == x2) printf("%d %d -", x1, (y1 + y2) >> 1); else printf("%d %d |", (x1 + x2) >> 1, y1); } else { if (!s) { for (int i = 0; i < 4; i++) if (x1 + d[i][0] == x && y1 + d[i][1] == y) book[(i + 2) % 4] = 1; } else if (!e) { for (int i = 0; i < 4; i++) if (x2 + d[i][0] == x && y2 + d[i][1] == y) book[(i + 2) % 4] = 1; } if (book[0] && book[1] && book[2] && book[3]) printf("%d %d +", x, y); else if (book[1] && book[3]) printf("%d %d |", x, y); else if (book[0] && book[2]) printf("%d %d -", x, y); else if (book[0] && book[1]) printf("%d %d 1", x, y); else if (book[0] && book[3]) printf("%d %d 2", x, y); else if (book[2] && book[3]) printf("%d %d 3", x, y); else if (book[1] && book[2]) printf("%d %d 4", x, y); } return 0; }