poj 2312 Battle City【bfs+优先队列】
-
Battle City
Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7579 Accepted: 2544 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
- Y是自己 B是砖墙(可以打破) S是铁墙(无法通过) R是河流(无法通过) T是敌人 当遇到B花费加2当遇到E花费加1
- /*
- 题意:小时候玩的坦克大战一样,字符Y是自己 B是砖墙可以打破
- 但是打破需要花费两点能量,S是铁墙无法通过,R是河流无法通过
- T是敌人,E是路通过需要花费一点能量,问杀死敌人需要花费多少能量
- (敌人不会动)并输出花费的能量 如果不能杀死敌人输出-1
- 题解: 开一个优先队列,用来记录坐标和花费的能量找到自己的位置坐标,然后开始向四个方向搜索,
- 每次经过空地将能量加1,经过砖墙将能量加2
- */
- #include<stdio.h>
- #include<string.h>
- #include<queue>
- using namespace std;
- #define MAX 1100
- char map[MAX][MAX];
- int vis[MAX][MAX];
- int x1,x2,y1,y2;
- int n,m;
- struct node//建立结构体优先队列
- {
- int a;
- int b;
- int cost;
- friend bool operator < (node a,node b)
- {
- return a.cost>b.cost;//表示花费能量少的优先出队
- }
- };
- void bfs()
- {
- int i,j,t;
- int move[4][2]={0,1,0,-1,-1,0,1,0};
- node beg,end;
- priority_queue<node>q;
- beg.a=x1;//起点坐标
- beg.b=y1;
- beg.cost=0;
- q.push(beg);//将起点坐标入队
- vis[x1][y1]=1;//标记走过的点,防止重复搜索
- while(!q.empty())//如果队列不为空
- {
- end=q.top();//去队顶元素
- q.pop();//删除队顶元素
- if(end.a==x2&&end.b==y2)//如果找到终点即找到敌人
- { //敌人坐标为终点坐标
- printf("%d\n",end.cost);
- return ;
- }
- for(i=0;i<4;i++)
- {
- beg.a=end.a+move[i][0];
- beg.b=end.b+move[i][1];//坐标变化即向四个方向移动
- if(!vis[beg.a][beg.b]&&0<beg.a&&beg.a<=n&&beg.b>0&&beg.b<=m&&map[beg.a][beg.b]!='S'&&map[beg.a][beg.b]!='R')
- {//当前点没有走过且坐标不超出题目要求范围 而且此点可以走
- vis[beg.a][beg.b]=1;
- if(map[beg.a][beg.b]=='B')
- //如果遇到砖墙,花费能量加2
- beg.cost=end.cost+2;
- else
- beg.cost=end.cost+1;
- // 否则就是遇见空地 花费加1
- q.push(beg);
- }
- }
- }
- printf("-1\n");
- }
- int main()
- {
- int j,i,s,t;
- while(scanf("%d%d",&n,&m)&&n!=0&&m!=0)
- {
- for(i=1;i<=n;i++)
- {
- getchar();
- for(j=1;j<=m;j++)
- {
- scanf("%c",&map[i][j]);
- if(map[i][j]=='Y')//找到起点坐标
- {
- x1=i;y1=j;
- }
- else if(map[i][j]=='T')//找到终点坐标
- {
- x2=i;y2=j;
- }
- }
- }
- memset(vis,0,sizeof(vis));
- bfs();
- }
- return 0;
- }
- 3 4
poj 2312 Battle City【bfs+优先队列】的更多相关文章
- POJ - 2312 Battle City BFS+优先队列
Battle City Many of us had played the game "Battle city" in our childhood, and some people ...
- poj 2312 Battle City
题目连接 http://poj.org/problem?id=1840 Battle City Description Many of us had played the game "Bat ...
- B - Battle City bfs+优先队列
来源poj2312 Many of us had played the game "Battle city" in our childhood, and some people ( ...
- C - Battle City BFS+优先队列
Many of us had played the game "Battle city" in our childhood, and some people (like me) e ...
- poj 2312 Battle City(优先队列+bfs)
题目链接:http://poj.org/problem?id=2312 题目大意:给出一个n*m的矩阵,其中Y是起点,T是终点,B和E可以走,S和R不可以走,要注意的是走B需要2分钟,走E需要一分钟. ...
- POJ 2312:Battle City(BFS)
Battle City Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9885 Accepted: 3285 Descr ...
- Battle City 优先队列+bfs
Many of us had played the game "Battle city" in our childhood, and some people (like me) e ...
- poj2312 Battle City 【暴力 或 优先队列+BFS 或 BFS】
题意:M行N列的矩阵.Y:起点,T:终点.S.R不能走,走B花费2,走E花费1.求Y到T的最短时间. 三种解法.♪(^∇^*) //解法一:暴力 //157MS #include<cstdio& ...
- POJ 1724 ROADS(BFS+优先队列)
题目链接 题意 : 求从1城市到n城市的最短路.但是每条路有两个属性,一个是路长,一个是花费.要求在花费为K内,找到最短路. 思路 :这个题好像有很多种做法,我用了BFS+优先队列.崔老师真是千年不变 ...
随机推荐
- C#网页版计算器程序代码
calculator.aspx.cs代码 using System; using System.Collections.Generic; using System.Linq; using System ...
- ie8中parseInt字符型数值转换数值型问题
今天在ie8中测试项目发现一个奇怪的问题,"08" "09" 强转竟然变成了: 后来发现ie8把"08" "09" 默认 ...
- 查看当前linux系统位数
linux系统也有位数之分,所以在linux上安装一些软件,比如jdk之类的就需要注意下版本. 查看linux系统位数最简单的命令(这里以redhat为例,不同版本linux命令也许不同) 命令1:g ...
- Windows phone 之Image控件
wp目前支持的图片格式为png和jpeg ,我们可以通过设置Source属性设置图片源. 还有两个属性是:Stretch,Opacity Stretch属性 image的拉伸行为有此属性决定,此属性是 ...
- crontab 基本用法
crontab格式:第1列分钟1-59第2列小时1-23(0表示子夜)第3列日1-31第4列月1-12第5列星期0-6(0表示星期天)第6列要运行的命令 还可以用一些特殊符号: *: 表示任何时刻 , ...
- Linux内存映射(mmap)系列(1)
看到同事的代码中出现了mmap.所以自己私下学习学习,研究研究..... http://www.cnblogs.com/lknlfy/archive/2012/04/27/2473804.html ( ...
- [BZOJ 1221] [HNOI2001] 软件开发 【费用流 || 三分】
题目链接:BZOJ - 1221 题目分析 算法一:最小费用最大流 首先这是一道经典的网络流问题.每天建立两个节点,一个 i 表示使用毛巾,一个 i' 表示这天用过的毛巾. 然后 i 向 T 连 Ai ...
- java + spring (jython\python\script) Error:SyntaxError: no viable alternative at character '\n'
使用Jython结合java和Python开发功能时,要是遇到如下情况: 2016-03-10 16:16:49 DEBUG [com.freedom.orion.configs.JyhtonConf ...
- 【HDOJ】2795 Billboard
线段树.注意h范围(小于等于n). #include <stdio.h> #include <string.h> #define MAXN 200005 #define lso ...
- Fragment 常见问题
1. 因为Fragment是在3.0提出的,为了兼容低版本,需要引入一个android-support-v4.jar 2. 需要实例化的activity必须 extends FragmentActiv ...