(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

相关文章

hdu1254(嵌套bfs,两次bfs)

/*第一次做这种题感觉很有压力,思路还是有点混乱,总是wa,改了好多次才ac的思路:把箱子的移动当做第一层bfs,队列节点要用到当前箱子坐标(x,y),走的次数step,当前人的weizhi(man_x,man_y),要判断人能否将箱子推到某点时要嵌套第二层bfs(人的移动);代码如下:

hdu1180(广搜+优先队列)

此题要求最少到达目标点T的最短时间,所以我选择了广度优先搜索,并且要用到优先队列。 另外此题注意点较多,比如说可以在某个点停留,我wa了好多两次,就是因为忽略了这一点,然后参考了大神的思想,然后经过反复修改才AC的 这是我的代码 #include<iostream>#include<algorithm>#include<string>#include<stack>#include<

csu 1446 Problem J Modified LCS (扩展欧几里得算法的简单应用)

这是一道扩展欧几里得算法的简单应用题,这题是在湖南多校训练赛中队友ac的一道题,在比赛之后请教了队友,然后自己把它a掉 这也是自己独自做扩展欧几里得算法的题目 题意:把题意转变下就变成了:求d1*x - d2*y = f2 - f1的解,很明显用exgcd来解 下面介绍一下exgcd的一些知识点:求ax + by = c的解 一、首先求ax + by = gcd(a,b)的解 这个

hdu2289(简单二分)

虽说是简单二分,但是我还是wa死了  题意:已知圆台的体积,求高度 首先要知道圆台体积怎么求:设上下底的半径分别为r1,r2,高为h,V = PI*(r1*r1+r1*r2+r2*r2)*h/3 然后以h进行二分 代码如下: #include<iostream>#include<algorithm>#include<cstring>#include<stack>#includ

usaco 1.3 Prime Cryptarithm(简单哈希表暴搜剪枝)

思路: 1. 用一个 hash[ ] 数组存放输入的数字,令 hash[ tmp ]=1 。 2. 一个自定义函数 check( ) ,检查各位是否为输入的数字。 3. 暴搜。第一行数从 100到999,第二行数从 10到99。 4. 剪枝。 代码: /*ID: who jayLANG: C++TASK: crypt1*/#include<stdio.h>bool h

poj 3974 and hdu 3068 最长回文串的O(n)解法(Manacher算法)

求一段字符串中的最长回文串。 因为数据量比较大,用原来的O(n^2)会爆。 小白上的O(n^2)解法代码:TLE啦~ #include<stdio.h>#include<string.h>const int Maxn = 1000000;char s[Maxn];int main(){char e[] = {"END"};while(scanf("%s", s) != EO

hdu 2602 and poj 3624(01背包)

01背包的模板题。 hdu2602代码: #include<stdio.h>#include<string.h>const int MaxN = 1001;int max(int a, int b){return a > b ? a : b;}int w[MaxN];int v[MaxN];int dp[MaxN];int main(){int T;int N, V;s

poj 1511 Invitation Cards(spfa最短路)

题意是给你点与点之间的距离,求来回到点1的最短路中的边权和。 因为边很大,不能用原来的dijkstra什么的,所以用spfa来做。并且注意要用long long int 来存储。 稍微改了一下学长的模板。 stack stl 实现代码: #include<stdio.h>#include<stack>using namespace std;const int M

poj 3259 uva 558 Wormholes(bellman最短路负权回路判断)

poj 3259: 题意:John的农场里n块地,m条路连接两块地,w个虫洞,虫洞是一条单向路,不但会把你传送到目的地,而且时间会倒退Ts。 任务是求你会不会在从某块地出发后又回来,看到了离开之前的自己。 判断树中是否存在负权回路就ok了。 bellman代码: #include<stdio.h>const int MaxN = 501;//农场数const int

poj 1258 Agri-Net(最小生成树模板代码)

感觉用这题来当模板更适合。 题意就是给你邻接矩阵求最小生成树啦。~ prim代码:效率很高。172k...0ms。 #include<stdio.h>#include<algorithm>using namespace std;const int MaxN = 101;const int INF = 0x3f3f3f3f;int g[MaxN][MaxN];int n