(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

相关文章

springboot简单集成Security配置的教程

《springboot简单集成Security配置的教程》:本文主要介绍springboot简单集成Security配置的教程,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,... 目录集成Security安全框架引入依赖编写配置类WebSecurityConfig(自定义资源权限规则

如何使用Python实现一个简单的window任务管理器

《如何使用Python实现一个简单的window任务管理器》这篇文章主要为大家详细介绍了如何使用Python实现一个简单的window任务管理器,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起... 任务管理器效果图完整代码import tkinter as tkfrom tkinter i

C++中函数模板与类模板的简单使用及区别介绍

《C++中函数模板与类模板的简单使用及区别介绍》这篇文章介绍了C++中的模板机制,包括函数模板和类模板的概念、语法和实际应用,函数模板通过类型参数实现泛型操作,而类模板允许创建可处理多种数据类型的类,... 目录一、函数模板定义语法真实示例二、类模板三、关键区别四、注意事项 ‌在C++中,模板是实现泛型编程

使用EasyExcel实现简单的Excel表格解析操作

《使用EasyExcel实现简单的Excel表格解析操作》:本文主要介绍如何使用EasyExcel完成简单的表格解析操作,同时实现了大量数据情况下数据的分次批量入库,并记录每条数据入库的状态,感兴... 目录前言固定模板及表数据格式的解析实现Excel模板内容对应的实体类实现AnalysisEventLis

Java中数组转换为列表的两种实现方式(超简单)

《Java中数组转换为列表的两种实现方式(超简单)》本文介绍了在Java中将数组转换为列表的两种常见方法使用Arrays.asList和Java8的StreamAPI,Arrays.asList方法简... 目录1. 使用Java Collections框架(Arrays.asList)1.1 示例代码1.

Java8需要知道的4个函数式接口简单教程

《Java8需要知道的4个函数式接口简单教程》:本文主要介绍Java8中引入的函数式接口,包括Consumer、Supplier、Predicate和Function,以及它们的用法和特点,文中... 目录什么是函数是接口?Consumer接口定义核心特点注意事项常见用法1.基本用法2.结合andThen链

Spring Boot整合消息队列RabbitMQ的实现示例

《SpringBoot整合消息队列RabbitMQ的实现示例》本文主要介绍了SpringBoot整合消息队列RabbitMQ的实现示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的... 目录RabbitMQ 简介与安装1. RabbitMQ 简介2. RabbitMQ 安装Spring

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.