Battle City
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 6880 | Accepted: 2326 |
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?
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
题意:
给出 n,m 的地图,Y 代表所在位置,T 代表目标位置,E 代表空地, S 代表铁墙, R 代表河流, B 代表木墙,攻破木墙需要攻破为空地才能走,铁墙跟河流是无论如何都不能走的,问到达目标位置的最小步数,不能到达则输出 -1。
思路:
BFS。B 为走两步,E 为走一步,用 dis 记录到达每个位置所需的最小值,初始化为无穷大,当这个位置步数有更新的时候就进队,最后判断目标位置的步数即可。
AC:
#include <cstdio> #include <cstring> #include <algorithm> #include <queue> using namespace std; const int INF = 99999999; typedef struct { int x, y, ans; } node; char Map[305][305]; int dis[305][305]; int dir[4][2] = {1, 0, 0, 1, -1, 0, 0, -1}; int n, m, sx, sy, ex, ey; void bfs () { node from; from.x = sx; from.y = sy; from.ans = 0; dis[sx][sy] = 0; queue<node> q; q.push(from); while (!q.empty()) { node a = q.front(); q.pop(); for (int i = 0; i < 4; ++i) { node b; b.x = a.x + dir[i][0]; b.y = a.y + dir[i][1]; if (b.x >= 0 && b.x < n && b.y >= 0 && b.y < m && Map[b.x][b.y] != 'S' && Map[b.x][b.y] != 'R') { if (Map[b.x][b.y] == 'E') b.ans = a.ans + 1; else b.ans = a.ans + 2; if (b.ans < dis[b.x][b.y]) { dis[b.x][b.y] = b.ans; q.push(b); } } } } if (dis[ex][ey] == INF) printf("-1\n"); else printf("%d\n", dis[ex][ey]); } int main() { while (~scanf("%d%d", &n, &m) && (n + m)) { for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) { scanf(" %c", &Map[i][j]); dis[i][j] = INF; if (Map[i][j] == 'Y') { sx = i; sy = j; } else if (Map[i][j] == 'T') { ex = i; ey = j; } } } Map[ex][ey] = 'E'; bfs(); } return 0; }
相关推荐
在IT领域,尤其是在算法和计算机科学中,"魔方-BFS-输入_魔方bfs_魔方降群法+双向bfs_魔方降群_"这个标题涉及到的是使用广度优先搜索(Breadth First Search, BFS)解决魔方问题的高级技巧。魔方是一种三维旋转拼图...
**广度优先搜索(BFS)**是一种在图或树数据结构中进行遍历或搜索的算法,其基本思想是从根节点开始,按照层次顺序依次访问每个节点。它首先访问最近的节点,然后逐渐扩展到更远的节点。在有向图中,BFS依然按照层次...
标题中的"BFS.rar_bfs_bfs matlab_bfs matlab"提到了"BFS"以及与MATLAB相关的优化算法。BFS,全称为Breadth-First Search(广度优先搜索),通常用于图论或树结构的遍历。在MATLAB环境中,BFS可以被应用于解决各种...
代码 基于BFS广度优先搜索算法代码代码 基于BFS广度优先搜索算法代码代码 基于BFS广度优先搜索算法代码代码 基于BFS广度优先搜索算法代码代码 基于BFS广度优先搜索算法代码代码 基于BFS广度优先搜索算法代码代码 ...
该问题常用于演示搜索算法,如宽度优先搜索(BFS)、深度优先搜索(DFS)、双向宽度优先搜索(BBFS)以及A*搜索。 首先,**宽度优先搜索(BFS)**是一种按层进行搜索的算法,它会优先检查离起点近的解。在八数码...
标题"simulink BFSK仿真"表明我们要探讨的是使用Simulink进行二进制频移键控(Binary Frequency Shift Keying, BFSK)的模拟与仿真。BFSK是一种数字调制技术,通过改变载波频率来传输二进制数据(0或1)。在Simulink...
标题“bfsk程序代码matlab”指的是使用MATLAB语言实现的BFSK(Binary Frequency Shift Keying,二进制频率移键控)编码技术的程序代码。BFSK是一种数字调制方法,通过改变载波频率来传输二进制数据,其中“0”和“1...
在本文中,我们将深入探讨如何使用宽度优先搜索(BFS)算法实现经典的“贪吃蛇”游戏。贪吃蛇是一款非常流行的电子游戏,玩家需要控制一个不断移动的蛇去吃食物,每次吃到食物,蛇的长度就会增加,游戏区域也会相应...
根据给定文件中的标题“DFS \BFS生成树”、描述以及部分代码内容,我们可以提炼出以下几个关键知识点:邻接表、深度优先搜索(Depth-First Search, DFS)、广度优先搜索(Breadth-First Search, BFS)以及如何利用这...
在本话题中,我们将深入探讨BFSK的基本原理,以及如何利用MATLAB软件来模拟和计算BFSK系统的误码率曲线。 首先,让我们理解BFSK的工作原理。BFSK是FSK(频移键控)的一个变种,它使用两个不同的载波频率来代表二...
宽度优先搜索(BFS,Breadth-First Search)是一种用于遍历或搜索树或图的算法,它从根节点开始,沿着树的宽度方向进行探索,直到找到目标节点或者遍历完整个树。在图中,BFS通常用于找出两个节点间的最短路径,或者...
**标题中的“bfs.rar_BFS+c++_bfs”表明这是一个关于C++实现BFS算法的压缩包文件,可能包含了一个名为“bfs”的源代码文件。** **描述中的“this is code for bfs in c++”进一步确认了这个压缩包内代码的主要功能...
### 动态内存+BFS知识点解析 #### 一、程序概览 本程序主要通过结合动态内存分配与广度优先搜索(BFS)算法来解决一个图论问题:即在一个无向图中寻找从起点到其他各顶点的路径顺序。程序首先读入测试用例的数量,...
【标题】"bfs.tar.gz_C#BFS_bfs" 提供的是一个使用C#实现的广度优先搜索(BFS)算法的代码压缩包。这个压缩包中的内容主要是关于如何在并行计算环境中,利用CUDA(Compute Unified Device Architecture)技术来优化...
标题中的"BFS.rar_bfs"指的是使用BFS(Breadth-First Search,宽度优先搜索)算法的一个程序或项目,这个项目的压缩包被命名为"BFS.rar",可能包含了实现BFS算法的源代码、报告文档以及编译后的可执行文件。...
**广度优先搜索(BFS)算法** 广度优先搜索(BFS)是一种在图或树数据结构中进行遍历的算法,它按照从根节点开始,先访问所有相邻节点,然后依次对每个已访问节点的相邻节点进行访问的原则进行搜索。在 BFS 中,...
**BFS算法详解** 在计算机科学中,图遍历是一种重要的算法,用于探索图的所有节点。其中,广度优先搜索(Breadth-First Search,简称BFS)是一种按照节点的层次顺序遍历图的方法。BFS算法尤其适用于寻找最短路径、...
深度优先搜索(DFS,Depth-First Search)和广度优先搜索(BFS,Breadth-First Search)是图论和树形结构中常用的两种遍历算法,它们在计算机科学中有着广泛的应用,如解决迷宫问题、网络爬虫、社交网络分析、最短...