NC15445 wyh的吃鸡
题目
题目描述
最近吃鸡游戏非常火,你们wyh学长也在玩这款游戏,这款游戏有一个非常重要的过程,就是要跑到安全区内,否则就会中毒持续消耗血量,我们这个问题简化如下
假设地图为n*n的一个图,图中有且仅有一块X的联通快代表安全区域,有一个起点S代表缩圈的时候的起点,图中C代表的是车(保证车的数量小于等于100),标记为.的代表空地,可以任意通过,O代表障碍物不能通过。每次没有车的时候2s可以走一个格(只能走自己的上下左右4个方向),有车的话时间为1s走一个格
现在告诉你最多能坚持的时间为t秒,问你在t秒内(含t秒)能否从s点到达安全区域,能的话输出YES,并且输出最短时间,不能的话输出NO
输入描述
输入第一行一个整数T(1<=T<=10)
接下来有T组测试数据,每组测试数据输入2个数n和k(1<=n<=100,1<=k<=10^9)
接下来n行,每行n个字符,代表对应的n*n的地图,每个字符都是上面的一种,并且保证只有一个起点,只有一块安全区域。
输出描述
对于每组测试数据,先输出能否到达,能的话输出YES,然后换行输出最短时间,如果不能的话直接输出NO
示例1
输入
3 2 3 .X S. 2 3 .X SC 2 4 .X S.
输出
NO YES 3 YES 4
题解
方法一
知识点:BFS,优先队列。
因为车的花费和走路花费不一致,会导致时间线混乱,因此可以考虑用优先队列维护时间线扩展,使得每次都是扩展最短时间的点,维持有序性。
同时,因为车的状态是持续的,非一次性的,因此状态需要多一维记录是否有车。
细节上,因为车的状态带来的花费影响是下一步才开始,不像传送门是即刻的,因此可以在扩展后直接锁点。
时间复杂度
空间复杂度
方法二
知识点:BFS。
高维状态和方法一一样,但不使用优先队列维护时间线有序,因此锁点操作通过步数实现。如果当前扩展的步数小于扩展到点的历史步数,那就用本次步数覆盖这个状态的步数。注意要遍历完全才能得出答案。
时间复杂度
空间复杂度
代码
方法一
#include <bits/stdc++.h> #define ll long long using namespace std; int n, t; char dt[107][107]; bool vis[107][107][2]; const int dir[4][2] = { {1,0},{-1,0},{0,1}, { 0,-1 } }; struct node { int x; int y; bool c; int step; friend bool operator<(const node &a, const node &b) { return a.step > b.step; } }; int bfs(node st) { priority_queue<node> pq; pq.push(st); vis[st.x][st.y][st.c] = 1; while (!pq.empty()) { node cur = pq.top(); pq.pop(); if (dt[cur.x][cur.y] == 'X') return cur.step; for (int i = 0;i < 4;i++) { int xx = cur.x + dir[i][0]; int yy = cur.y + dir[i][1]; bool cc = cur.c || dt[xx][yy] == 'C'; int sstep = cur.step + (cur.c ? 1 : 2);///和之前一次有关 if (xx < 0 || xx >= n || yy < 0 || yy >= n || vis[xx][yy][cc] || dt[xx][yy] == 'O' || sstep > t)continue; vis[xx][yy][cc] = 1;///因为不是在这次就改变时间线,所以依旧可以在扩展时直接锁定点 pq.push({ xx,yy,cc,sstep }); } } return 1e9 + 10; } bool solve() { cin >> n >> t; node st; for (int i = 0;i < n;i++) { for (int j = 0;j < n;j++) { cin >> dt[i][j]; if (dt[i][j] == 'S') st.x = i, st.y = j, st.c = 0, st.step = 0; vis[i][j][0] = vis[i][j][1] = 0; } } int ans = bfs(st); if (ans > t) return false; else cout << "YES\n" << ans << '\n'; return true; } int main() { std::ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); int t = 1; cin >> t; while (t--) { if (!solve()) cout << "NO" << '\n'; } return 0; }
方法二
#include <bits/stdc++.h> #define ll long long using namespace std; int n, t; char dt[107][107]; int step[107][107][2]; const int dir[4][2] = { {1,0},{-1,0},{0,1}, { 0,-1 } }; struct node { int x, y; bool c; }; int bfs(node st, vector<node> &ed) { queue<node> q; q.push(st); step[st.x][st.y][0] = 0; while (!q.empty()) { node cur = q.front(); q.pop(); if (dt[cur.x][cur.y] == 'X') continue; for (int i = 0;i < 4;i++) { int xx = cur.x + dir[i][0]; int yy = cur.y + dir[i][1]; int delta = cur.c ? 1 : 2; if (xx < 0 || xx >= n || yy < 0 || yy >= n || dt[xx][yy] == 'O')continue; bool cc = cur.c || dt[xx][yy] == 'C'; if (step[xx][yy][cc] <= step[cur.x][cur.y][cur.c] + delta) continue; ///不能用vis锁定点,因为扩展时间线混乱,不按照时间顺序扩展 ///有可能时间晚的车先占了格子,早的车没扩展过来就无法覆盖了,因此只能用距离覆盖 step[xx][yy][cc] = step[cur.x][cur.y][cur.c] + delta; q.push({ xx,yy,cc }); } } int ans = 1e9; for (auto e : ed) ans = min({ ans, step[e.x][e.y][0],step[e.x][e.y][1] });///目的地是个连通块 return ans; } bool solve() { cin >> n >> t; node st; vector<node> ed; for (int i = 0;i < n;i++) { for (int j = 0;j < n;j++) { cin >> dt[i][j]; if (dt[i][j] == 'S') st.x = i, st.y = j, st.c = 0; if (dt[i][j] == 'X') ed.push_back({ i,j,0 }); step[i][j][0] = step[i][j][1] = 1e9; } } int ans = bfs(st, ed); if (ans > t) return false; else cout << "YES\n" << ans << '\n'; return true; } int main() { std::ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); int t = 1; cin >> t; while (t--) { if (!solve()) cout << "NO" << '\n'; } return 0; }
本文来自博客园,作者:空白菌,转载请注明原文链接:https://www.cnblogs.com/BlankYang/p/16484981.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧