luogu1979 华容道
题目大意
小 B 最近迷上了华容道,可是他总是要花很长的时间才能完成一次。于是,他想到用编程来完成华容道:给定一种局面,华容道是否根本就无法完成,如果能完成,最少需要多少时间。
小 B 玩的华容道与经典的华容道游戏略有不同,游戏规则是这样的:
- 在一个 n*m 棋盘上有 n*m 个格子,其中有且只有一个格子是空白的,其余 n*m-1个格子上每个格子上有一个棋子,每个棋子的大小都是 1*1 的;
- 有些棋子是固定的,有些棋子则是可以移动的;
- 任何与空白的格子相邻(有公共的边)的格子上的棋子都可以移动到空白格子上。 游戏的目的是把某个指定位置可以活动的棋子移动到目标位置。
给定一个棋盘,游戏可以玩 q 次,当然,每次棋盘上固定的格子是不会变的,但是棋盘上空白的格子的初始位置、指定的可移动的棋子的初始位置和目标位置却可能不同。第 i 次玩的时候,空白的格子在第 EX_i 行第 EY_i 列,指定的可移动棋子的初始位置为第 SX_i 行第 SY_i 列,目标位置为第 TX_i 行第 TY_i 列。
假设小 B 每秒钟能进行一次移动棋子的操作,而其他操作的时间都可以忽略不计。请你告诉小 B 每一次游戏所需要的最少时间,或者告诉他不可能完成游戏。
题解
最简单的方法莫过于让空格瞎走,此法能得到70分(当初模拟时太过于紧张只想得那30分,数组开小了。。。记住,数组够,就开大)。
我们想到,要想让移动过的控制的棋子向相邻的格子$q$动,空格必须先到达$q$。移动的时间有多长呢?因为规定了控制的棋子移动过,故之间空格一定在控制的棋子的另一个相邻格子$p$中,空格所要移动的距离便是从$p$到$q$避开棋子所在位置的最短路径长度$d$。也就是说,我们用控制棋子的位置和空格的位置作为状态(棋子移动过,故空格必定与控制棋子相邻,故可以用一条连接两个格子对应节点的有向边来表示),表示空格移动一步的两个状态间的转移权值为$d+1$。在一个新图中,把状态作为状态节点,转移为状态边,虚拟一个状态起始点向所有to节点为控制节点的边对应的状态节点连一条空格初始位置到该边from点的最短距离的状态边,所有to节点为终止节点的边对应的状态节点与虚拟终止节点连一条边权为0的状态边,跑一遍Dijkstra即可。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 | #include <cstdio> #include <cstring> #include <algorithm> #include <queue> using namespace std; const int MAX_ROW = 35, MAX_COL = 35, INF = 0x3f3f3f3f; bool IsWall[MAX_ROW][MAX_COL]; int TotRow, TotCol; int StComRow, StComCol, StEmpRow, StEmpCol, TarComRow, TarComCol; struct SPGraph //shortest path { static const int MAX_NODE = MAX_ROW * MAX_COL * 4; static const int MAX_EDGE = MAX_ROW * MAX_COL * 16; struct Node; struct Edge; struct Node { Edge *Head; int Dist; bool Done; }_nodes[MAX_NODE], *Start; int TotNode; struct Edge { Node *To, *From; Edge *Next; int Weight; }_edges[MAX_EDGE]; int _eCount; struct HeapNode { Node *Cur; int Dist; HeapNode(Node *cur, int dist):Cur(cur),Dist(dist){} bool operator < ( const HeapNode& a) const { return Dist > a.Dist; } }; void Init( int n, int s) { TotNode = n; Start = _nodes + s; } void AddEdge( int u, int v, int w) { Edge *e = _edges + ++_eCount; e->To = _nodes + v; e->From = _nodes + u; e->Weight = w; e->Next = _nodes[u].Head; _nodes[u].Head = e; } void PopEdge() { Edge *e = _edges + _eCount; Edge **next = &e->From->Head; while ( true ) { if (*next == e) { *next = e->Next; _eCount--; return ; } else next = &(*next)->Next; } } void Dijkstra() { static priority_queue<HeapNode> q; for ( int i = 1; i <= TotNode; i++) { _nodes[i].Dist = INF; _nodes[i].Done = false ; } Start->Dist = 0; q.push(HeapNode(Start, 0)); while (!q.empty()) { HeapNode curHeapNode = q.top(); q.pop(); Node *cur = curHeapNode.Cur; if (cur->Done) continue ; cur->Done = true ; for (Edge *e = cur->Head; e; e = e->Next) { if (cur->Dist + e->Weight < e->To->Dist) { e->To->Dist = cur->Dist + e->Weight; q.push(HeapNode(e->To, e->To->Dist)); } } } } }s; struct OrgGraph { static const int MAX_EDGE = MAX_ROW * MAX_COL * 4; struct Node; struct Edge; struct Node { Edge *Head; int Dist; int Row, Col; }_nodes[MAX_ROW][MAX_COL]; struct Edge { Node *To, *From; Edge *Next; }_edges[MAX_EDGE]; int _eCount; void AddEdge( int row1, int col1, int row2, int col2) { Node *from = _nodes[row1] + col1, *to = _nodes[row2] + col2; Edge *e = _edges + ++_eCount; e->To = to; e->From = from; e->Next = from->Head; from->Head = e; } void GetDist_Bfs(Node *start, Node *skip) { static queue<Node*> q; while (!q.empty()) q.pop(); for ( int row = 1; row <= TotRow; row++) for ( int col = 1; col <= TotCol; col++) _nodes[row][col].Dist = -1; start->Dist = 0; q.push(start); while (!q.empty()) { Node *cur = q.front(); q.pop(); for (Edge *e = cur->Head; e; e = e->Next) { if (e->To == skip) continue ; if (e->To->Dist >= 0) continue ; e->To->Dist = cur->Dist + 1; q.push(e->To); } } } }g; void BuildG() { for ( int row = 1; row <= TotRow; row++) for ( int col = 1; col <= TotCol; col++) g._nodes[row][col].Row = row, g._nodes[row][col].Col = col; const int Dir[4][2] = { {1, 0}, {0, 1}, {-1, 0}, {0, -1} }; for ( int row = 1; row <= TotRow; row++) for ( int col = 1; col <= TotCol; col++) { if (IsWall[row][col]) continue ; for ( int i = 0; i < 4; i++) { int row1 = row + Dir[i][0], col1 = col + Dir[i][1]; if (IsWall[row1][col1]) continue ; g.AddEdge(row, col, row1, col1); } } } void BuildS() { s.Init(g._eCount + 2, g._eCount + 1); for ( int i = 1; i <= g._eCount; i++) { OrgGraph::Edge *eCur = g._edges + i; g.GetDist_Bfs(eCur->From, eCur->To); for (OrgGraph::Edge *e = eCur->To->Head; e; e = e->Next) { if (e->To->Dist == -1) continue ; s.AddEdge(eCur - g._edges, e - g._edges, e->To->Dist + 1); } } } int GetAns() { int cnt = 0; OrgGraph::Node *StCom = g._nodes[StComRow] + StComCol; OrgGraph::Node *StEmp = g._nodes[StEmpRow] + StEmpCol; OrgGraph::Node *TarCom = g._nodes[TarComRow] + TarComCol; if (StCom == TarCom) return 0; g.GetDist_Bfs(StEmp, StCom); for (OrgGraph::Edge *e1 = StCom->Head; e1; e1 = e1->Next) for (OrgGraph::Edge *e2 = e1->To->Head; e2; e2 = e2->Next) { if (e2->To != StCom) continue ; if (e2->From->Dist == -1) continue ; s.AddEdge(g._eCount + 1, e2 - g._edges, e2->From->Dist); cnt++; } for (OrgGraph::Edge *e1 = TarCom->Head; e1; e1 = e1->Next) for (OrgGraph::Edge *e2 = e1->To->Head; e2; e2 = e2->Next) { if (e2->To != TarCom) continue ; s.AddEdge(e2 - g._edges, g._eCount + 2, 0); cnt++; } s.Dijkstra(); while (cnt--) s.PopEdge(); int ans = s._nodes[g._eCount + 2].Dist; if (ans == INF) ans = -1; return ans; } int main() { memset (IsWall, true , sizeof (IsWall)); int qCnt; scanf ( "%d%d%d" , &TotRow, &TotCol, &qCnt); for ( int row = 1; row <= TotRow; row++) for ( int col = 1; col <= TotCol; col++) { int x; scanf ( "%d" , &x); IsWall[row][col] = !x; } BuildG(); BuildS(); while (qCnt--) { scanf ( "%d%d%d%d%d%d" , &StEmpRow, &StEmpCol, &StComRow, &StComCol, &TarComRow, &TarComCol); printf ( "%d\n" , GetAns()); } return 0; } |
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步