(POJ 2312)Battle City 优先队列 简单BFS

2024-02-05 02:18

本文主要是介绍(POJ 2312)Battle City 优先队列 简单BFS,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Battle City
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 9324 Accepted: 3086
Description

Many of us had played the game “Battle city” in our childhood, and some people (like me) even often play it on computer now.

What we are discussing is a simple edition of this game. Given a map that consists of empty spaces, rivers, steel walls and brick walls only. Your task is to get a bonus as soon as possible suppose that no enemies will disturb you (See the following picture).

Your tank can’t move through rivers or walls, but it can destroy brick walls by shooting. A brick wall will be turned into empty spaces when you hit it, however, if your shot hit a steel wall, there will be no damage to the wall. In each of your turns, you can choose to move to a neighboring (4 directions, not 8) empty space, or shoot in one of the four directions without a move. The shot will go ahead in that direction, until it go out of the map or hit a wall. If the shot hits a brick wall, the wall will disappear (i.e., in this turn). Well, given the description of a map, the positions of your tank and the target, how many turns will you take at least to arrive there?
Input

The input consists of several test cases. The first line of each test case contains two integers M and N (2 <= M, N <= 300). Each of the following M lines contains N uppercase letters, each of which is one of ‘Y’ (you), ‘T’ (target), ‘S’ (steel wall), ‘B’ (brick wall), ‘R’ (river) and ‘E’ (empty space). Both ‘Y’ and ‘T’ appear only once. A test case of M = N = 0 indicates the end of input, and should not be processed.
Output

For each test case, please output the turns you take at least in a separate line. If you can’t arrive at the target, output “-1” instead.
Sample Input

3 4
YBEB
EERE
SSTE
0 0
Sample Output

8
Source

POJ Monthly,鲁小石

题意:
给你一个m行n列的矩阵。
Y代表起点,T代表终点。B、E可以走,S、R不可以走,B的时间花费为2,E为1.
求Y到T的最短时间。

分析:
直接就是简单的BFS即可。。
(脑残的我忘了将终点情况加入下一状态中。。。。。吐血。。。。)

AC代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <cmath>
#include <queue>
using namespace std;const int maxn = 310;
int m,n,sx,sy;
char g[maxn][maxn];
bool vis[maxn][maxn];
int dir[4][2] = {{0,1},{0,-1},{1,0},{-1,0}};struct tank
{int x,y,cost;friend bool operator < (const tank &a,const tank &b){return a.cost > b.cost;}
};bool check(int x,int y)
{if(x < 0 || x >= m || y < 0 || y >= n) return 0;if(g[x][y] == 'S' || g[x][y] == 'R' || vis[x][y] ) return 0;return 1;
}bool bfs()
{memset(vis,0,sizeof(vis));priority_queue<tank> q;tank now,next;now.x = sx; now.y = sy; now.cost = 0;q.push(now);vis[sx][sy] = 1;while(!q.empty()){now = q.top(); q.pop();if(g[now.x][now.y] == 'T'){printf("%d\n",now.cost);return true;}for(int i=0;i<4;i++){next.x = now.x + dir[i][0];next.y = now.y + dir[i][1];if(check(next.x,next.y)){if(g[next.x][next.y] == 'E' || g[next.x][next.y] == 'T') next.cost = now.cost + 1; //别忘了g[next.x][next.y] == 'T'else if(g[next.x][next.y] == 'B') next.cost = now.cost + 2;q.push(next);vis[next.x][next.y] = 1;}}}return false;
}int main()
{while(scanf("%d%d",&m,&n)!=EOF && m){getchar();for(int i=0;i<m;i++) gets(g[i]);int f = 0;for(int i=0;i<m;i++){if(f) break;for(int j=0;j<n;j++){if(g[i][j] == 'Y'){sx = i;sy = j;f = 1;break;}}}if(!bfs()) printf("-1\n");}return 0;
}

这篇关于(POJ 2312)Battle City 优先队列 简单BFS的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/679494

相关文章

C++初始化数组的几种常见方法(简单易懂)

《C++初始化数组的几种常见方法(简单易懂)》本文介绍了C++中数组的初始化方法,包括一维数组和二维数组的初始化,以及用new动态初始化数组,在C++11及以上版本中,还提供了使用std::array... 目录1、初始化一维数组1.1、使用列表初始化(推荐方式)1.2、初始化部分列表1.3、使用std::

如何通过Python实现一个消息队列

《如何通过Python实现一个消息队列》这篇文章主要为大家详细介绍了如何通过Python实现一个简单的消息队列,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录如何通过 python 实现消息队列如何把 http 请求放在队列中执行1. 使用 queue.Queue 和 reque

redis群集简单部署过程

《redis群集简单部署过程》文章介绍了Redis,一个高性能的键值存储系统,其支持多种数据结构和命令,它还讨论了Redis的服务器端架构、数据存储和获取、协议和命令、高可用性方案、缓存机制以及监控和... 目录Redis介绍1. 基本概念2. 服务器端3. 存储和获取数据4. 协议和命令5. 高可用性6.

JAVA调用Deepseek的api完成基本对话简单代码示例

《JAVA调用Deepseek的api完成基本对话简单代码示例》:本文主要介绍JAVA调用Deepseek的api完成基本对话的相关资料,文中详细讲解了如何获取DeepSeekAPI密钥、添加H... 获取API密钥首先,从DeepSeek平台获取API密钥,用于身份验证。添加HTTP客户端依赖使用Jav

解读Redis秒杀优化方案(阻塞队列+基于Stream流的消息队列)

《解读Redis秒杀优化方案(阻塞队列+基于Stream流的消息队列)》该文章介绍了使用Redis的阻塞队列和Stream流的消息队列来优化秒杀系统的方案,通过将秒杀流程拆分为两条流水线,使用Redi... 目录Redis秒杀优化方案(阻塞队列+Stream流的消息队列)什么是消息队列?消费者组的工作方式每

利用Python编写一个简单的聊天机器人

《利用Python编写一个简单的聊天机器人》这篇文章主要为大家详细介绍了如何利用Python编写一个简单的聊天机器人,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 使用 python 编写一个简单的聊天机器人可以从最基础的逻辑开始,然后逐步加入更复杂的功能。这里我们将先实现一个简单的

Redis延迟队列的实现示例

《Redis延迟队列的实现示例》Redis延迟队列是一种使用Redis实现的消息队列,本文主要介绍了Redis延迟队列的实现示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习... 目录一、什么是 Redis 延迟队列二、实现原理三、Java 代码示例四、注意事项五、使用 Redi

使用IntelliJ IDEA创建简单的Java Web项目完整步骤

《使用IntelliJIDEA创建简单的JavaWeb项目完整步骤》:本文主要介绍如何使用IntelliJIDEA创建一个简单的JavaWeb项目,实现登录、注册和查看用户列表功能,使用Se... 目录前置准备项目功能实现步骤1. 创建项目2. 配置 Tomcat3. 项目文件结构4. 创建数据库和表5.

使用PyQt5编写一个简单的取色器

《使用PyQt5编写一个简单的取色器》:本文主要介绍PyQt5搭建的一个取色器,一共写了两款应用,一款使用快捷键捕获鼠标附近图像的RGB和16进制颜色编码,一款跟随鼠标刷新图像的RGB和16... 目录取色器1取色器2PyQt5搭建的一个取色器,一共写了两款应用,一款使用快捷键捕获鼠标附近图像的RGB和16

四种简单方法 轻松进入电脑主板 BIOS 或 UEFI 固件设置

《四种简单方法轻松进入电脑主板BIOS或UEFI固件设置》设置BIOS/UEFI是计算机维护和管理中的一项重要任务,它允许用户配置计算机的启动选项、硬件设置和其他关键参数,该怎么进入呢?下面... 随着计算机技术的发展,大多数主流 PC 和笔记本已经从传统 BIOS 转向了 UEFI 固件。很多时候,我们也