博客
关于我
鸣人和佐助
阅读量:744 次
发布时间:2019-03-21

本文共 1577 字,大约阅读时间需要 5 分钟。

鸣人要追上佐助最少需要花费的时间是通过广度优先搜索(BFS)来计算的。BFS不仅记录位置和时间,还跟踪剩余的查克拉,因为不同的查克拉残 渣会影响后续的移动选择。

输入地图后,确定鸣人和佐助的初始位置。使用一个三维数组visited[m][n][max_t]记录是否已经访问过某个位置和特定的查克拉数量。如果同一位置被访问过但查克拉更高的情况,不需要重复处理,因为它可以提供更大的灵活性。

在每一步BFS处理中,考虑四个方向移动到下一个位置,判断该位置是否符合访问条件。检查下一个位置是否是通路或已经被击败的敌人,根据查克拉数量决定是否可以继续移动,并更新访问状态。

队列中每个节点包含当前位置、剩余查克拉和时间。当队列中的节点到达佐助的位置时,返回当前时间作为最少所需时间。

以下是代码示例:

#include 
#include
#include
#include
using namespace std;int main() { // 读取输入 m = ...; n = ...; T = ...; // 初始化访问数组 vector
]> visited(m+1, vector
>(n+1, vector
(T+1, false))); // BFS队列 queue
> q; // 初始状态 int sx = ...; int sy = ...; // 条件判断 if (sx == fx && sy == fy) return 0; // 初始状态入队 q.push({sx, sy, T, 0}); visited[sx][sy][T] = true; // 四个方向 int dirs[4][2] = {{1,0}, {0,1}, {-1,0}, {0,-1}}; while (!q.empty()) { auto current = q.front(); q.pop(); int x = current.first; int y = current.second; int ckl = current.third; int time = current.fourth; // 检查是否到达佐助 if (x == fx && y == fy) { return time; } // 遍历四个方向 for (auto dir : dirs) { int nx = x + dir.first; int ny = y + dir.second; // 检查是否越界 if (nx < 1 || nx > m || ny < 1 || ny > n) continue; // 检查是否是敌人 if (a[nx][ny] == '#') { // 剩余查克拉>0才能进入 if (ckl > 0) { // 打败敌人后的状态 if (!visited[nx][ny][ckl-1]) { visited[nx][ny][ckl-1] = true; q.push({nx, ny, ckl-1, time+1}); } } } else if (a[nx][ny] != '#') { // 通路或其他不需要击败的位置 if (!visited[nx][ny][ckl]) { visited[nx][ny][ckl] = true; q.push({nx, ny, ckl, time+1}); } } } } // 未找到 return -1;}

此代码实现了广度优先搜索,结合三维访问数组,记录每个位置和查克拉状态,确保找到最小时间追上。

转载地址:http://woagz.baihongyu.com/

你可能感兴趣的文章
Oracle 11g忘记sys、system、scott密码该这样修改!
查看>>
Oracle 11g数据库安装和卸载教程
查看>>
Oracle 11g数据库成功安装创建详细步骤
查看>>
Oracle 11g超详细安装步骤
查看>>
Oracle 12c中的MGMTDB
查看>>
Oracle 12c安装报错Installation failed to access the temporary location(无法访问临时位置)...
查看>>
Oracle 9i数据库管理教程
查看>>
ORACLE Active dataguard 一个latch: row cache objects BUG
查看>>
oracle avg、count、max、min、sum、having、any、all、nvl的用法
查看>>
Oracle BEQ方式连接配置
查看>>
oracle Blob保存方式,oracle 存储过程操作blob
查看>>
Oracle BMW Racing sailing vessel帆船图
查看>>
ORACLE Bug 4431215 引发的血案—原因分析篇
查看>>
Oracle cmd乱码
查看>>
Oracle Corp甲骨文公司推出Oracle NoSQL数据库2.0版
查看>>
oracle dblink 创建使用 垮库转移数据
查看>>
oracle dblink结合同义词的用法 PLS-00352:无法访问另一数据库
查看>>
Oracle dbms_job.submit参数错误导致问题(ora-12011 无法执行1作业)
查看>>
oracle dg switchover,DG Switchover fails
查看>>
Oracle E-Business Suite软件 任意文件上传漏洞(CVE-2022-21587)
查看>>