博客
关于我
POJ 2312:Battle City(BFS)
阅读量:217 次
发布时间:2019-02-28

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

为了解决这个问题,我们需要计算从起点到终点的最短路径。这个问题可以通过广度优先搜索(BFS)来解决,但需要考虑移动和射击两种动作,并记录破坏的砖墙情况。

方法思路

  • 问题分析:网格中包含起点、终点、钢墙、砖墙、河流和空地。移动和射击会影响路径,砖墙破坏后会变成空地。
  • 状态表示:每个状态包括坐标和破坏的砖墙位置集合,以避免重复处理相同的状态。
  • 动作处理
    • 移动:四个方向移动,检查是否可以通过。
    • 射击:四个方向射击,破坏路径中的砖墙,生成新状态。
  • 优化策略:使用优先队列处理状态,确保最短路径优先处理。
  • 解决代码

    #include 
    #include
    #include
    #include
    #include
    #include
    #include
    using namespace std;// 方向数组,四个方向:上下左右int dir[4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};// 砖墙破坏情况的位掩码,每一位表示一个砖墙是否被破坏typedef struct { int x, y; unordered_set
    bricks;} State;// 比较两个状态的距离,前面优先级高的先处理bool operator<(const State& a, const State& b) { return a.x + a.y * 300 < b.x + b.y * 300;}void bfs(int m, int n, char ch[m][n], char start, char target, int y, int tX, int tY) { // 记录访问状态,每个状态包括位置和破坏的砖墙位置 map
    visited; queue
    q; State start_state = {y, tX}; visited[start_state.x * m + start_state.y] = 0; q.push(start_state); while (!q.empty()) { State current = q.front(); q.pop(); if (current.x == tY && current.y == tX) { return visited[current.x * m + current.y]; } // 射击四个方向 for (int i = 0; i < 4; ++i) { int dx = dir[i][0], dy = dir[i][1]; int nx = current.x + dx, ny = current.y + dy; if (nx < 0 || nx >= n || ny < 0 || ny >= m) continue; if (ch[nx][ny] == 'S' || ch[nx][ny] == 'R') continue; // 射击路径,破坏砖墙 vector
    > path; int x = nx, y = ny; while (true) { path.push_back({x, y}); if (ch[x][y] == 'B') { // 破坏这个砖墙 unordered_set
    bricks; bricks.insert(x * m + y); // 生成新的状态 State new_state = {x, y}; new_state.bricks = bricks; int key = new_state.x * m + new_state.y; if (visited.find(key) == visited.end()) { visited[key] = visited[current.x * m + current.y] + 1; q.push(new_state); } } if (ch[x][y] == 'T' || ch[x][y] == 'E' || ch[x][y] == 'S' || ch[x][y] == 'R') break; if (x == 0 || x == m - 1 || y == 0 || y == n - 1) break; x += dx; y += dy; } } // 移动四个方向 for (int i = 0; i < 4; ++i) { int dx = dir[i][0], dy = dir[i][1]; int nx = current.x + dx, ny = current.y + dy; if (nx < 0 || nx >= n || ny < 0 || ny >= m) continue; if (ch[nx][ny] == 'S' || ch[nx][ny] == 'R') continue; // 检查是否可以移动到这里 if (ch[nx][ny] == 'E' || ch[nx][ny] == 'T' || ch[nx][ny] == 'B') { State new_state = {nx, ny}; if (visited.find(new_state.x * m + new_state.y) == visited.end()) { visited[new_state.x * m + new_state.y] = visited[current.x * m + current.y] + 1; q.push(new_state); } } } } return -1;}int main() { #include
    #include
    #include
    #include
    #include
    #include
    #include
    #include
    using namespace std; int n, m; char ch[m][n]; char start = 'Y', target = 'T'; int y = -1, tX = -1, tY = -1; while (true) { if (n == 0 && m == 0) break; // 读取输入 vector
    row; for (int i = 0; i < m; ++i) { row.push_back(getchar()); } for (int i = 0; i < n; ++i) { ch[i%n][i/m] = row[i]; } // 寻找起点和终点 y = -1; tX = -1; tY = -1; for (int i = 0; i < m; ++i) { for (int j = 0; j < n; ++j) { if (ch[i][j] == 'Y') y = i; if (ch[i][j] == 'T') tX = j, tY = i; } } if (y == -1 || tX == -1) { // 无法找到起点或终点 continue; } // BFS int res = bfs(m, n, ch, start, target, y, tX, tY); if (res != -1) { cout << res << endl; } else { cout << "-1" << endl; } // 读取下一组输入 if (n == 0 && m == 0) break; for (int i = 0; i < m; ++i) { getchar(); } } return 0;}

    代码解释

  • 读取输入:读取网格并找到起点和终点。
  • BFS初始化:将起点加入队列,并记录访问状态。
  • 处理射击:四个方向射击,破坏砖墙并生成新状态。
  • 处理移动:四个方向移动,检查可通行性并加入队列。
  • 终止条件:找到终点或队列为空,返回结果。
  • 转载地址:http://dcbp.baihongyu.com/

    你可能感兴趣的文章
    Non-final field ‘code‘ in enum StateEnum‘
    查看>>
    none 和 host 网络的适用场景 - 每天5分钟玩转 Docker 容器技术(31)
    查看>>
    None还可以是函数定义可选参数的一个默认值,设置成默认值时实参在调用该函数时可以不输入与None绑定的元素...
    查看>>
    NoNodeAvailableException None of the configured nodes are available异常
    查看>>
    Vue.js 学习总结(16)—— 为什么 :deep、/deep/、>>> 样式能穿透到子组件
    查看>>
    nopcommerce商城系统--文档整理
    查看>>
    NOPI读取Excel
    查看>>
    NoSQL&MongoDB
    查看>>
    NoSQL介绍
    查看>>
    NoSQL数据库概述
    查看>>
    Notadd —— 基于 nest.js 的微服务开发框架
    查看>>
    NOTE:rfc5766-turn-server
    查看>>
    Notepad ++ 安装与配置教程(非常详细)从零基础入门到精通,看完这一篇就够了
    查看>>
    Notepad++在线和离线安装JSON格式化插件
    查看>>
    notepad++最详情汇总
    查看>>
    notepad++正则表达式替换字符串详解
    查看>>
    notepad如何自动对齐_notepad++怎么自动排版
    查看>>
    Notes on Paul Irish's "Things I learned from the jQuery source" casts
    查看>>
    Notification 使用详解(很全
    查看>>
    NotImplementedError: Cannot copy out of meta tensor; no data! Please use torch.nn.Module.to_empty()
    查看>>