AcWing1131. 拯救大兵瑞恩
解题思路
代码
#include <iostream>
#include <cstring>
#include <queue>
using namespace std;
const int N = 11;
int k, n, m, p, r;
struct Point { int x, y, key; } ;
int g[N][N][N][N], key[N][N], f[N][N][1 << N];
int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1};
int bfs()
{
memset(f, 0x3f, sizeof f);
queue <Point> q;
f[1][1][key[1][1]] = 0, q.push({1, 1, key[1][1]});
while (q.size())
{
auto t = q.front(); q.pop();
if (t.x == n && t.y == m) return f[t.x][t.y][t.key];
for (int i = 0; i < 4; i ++ )
{
int x = t.x + dx[i], y = t.y + dy[i];
if (x < 1 || x > n || y < 1 || y > m) continue ;
int& door = g[t.x][t.y][x][y];
if (door == 0) continue ;
if (door >= 1 && !(t.key >> door & 1)) continue ;
int st = t.key | key[x][y];
if (f[x][y][st] > f[t.x][t.y][t.key] + 1)
{
f[x][y][st] = f[t.x][t.y][t.key] + 1;
q.push({x, y, st});
}
}
}
return -1;
}
int main()
{
scanf("%d%d%d", &n, &m, &p);
scanf("%d", &k);
memset(g, 0xcf, sizeof g);
while (k -- )
{
int x1, y1, x2, y2, c;
scanf("%d%d%d%d%d", &x1, &y1, &x2, &y2, &c);
g[x1][y1][x2][y2] = g[x2][y2][x1][y1] = c;
}
scanf("%d", &r);
while (r -- )
{
int x, y, q;
scanf("%d%d%d", &x, &y, &q);
key[x][y] |= 1 << q;
}
printf("%d\n", bfs());
return 0;
}
这种写法是看了haaai大佬的写法后才搞明白的,
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)