NYOJ 284 坦克大战 (广搜)
描述
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?
输入
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.
输出
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.
样例输入
3 4
YBEB
EERE
SSTE
0 0
样例输出
8
分析:
因为涉及到当前点往下走的时候是优先走空地还是能够被击破的墙(两个花费的时间不一样),所以应该用优先队列来处理,刚开始做的时候没有考虑到这一点。
代码:
include<stdio.h>
include
include
include<string.h>
using namespace std;
int m,n;
int sx,sy,ex,ey;
char Tu[301][301];
int bj[301][301];
int Next[4][2]= {{-1,0},{0,1},{1,0},{0,-1}};
int init()
{
memset(Tu,'R',sizeof(Tu));
memset(bj,'0',sizeof(bj));
}
struct Node
{
int x;
int y;
int step;
friend bool operator < (Node a,Node b)
{
return a.step>b.step;
}
};
void bfs(int s,int e,int step)
{
priority_queueq;
Node now,next;
now.x=s;
now.y=e;
now.step=step;
q.push(now);
while(!q.empty())
{
now=q.top();
// cout<<"now.step==="<<now.step<<" now.x="<<now.x<<" now.y=="<<now.y<<endl;
if(now.x==ex&&now.y==ey)
{
printf("%d\n",now.step);
return ;
}
q.pop();
for(int i=0; i<4; i++)
{
next.x=now.x+Next[i][0];
next.y=now.y+Next[i][1];
if(bj[next.x][next.y]==1||next.x<0||next.x>=m||next.y<0||next.y>=n||Tu[next.x][next.y]=='R'||Tu[next.x][next.y]=='S')
continue;
if(Tu[next.x][next.y]=='T'||Tu[next.x][next.y]=='E')
{
next.step=now.step+1;
bj[next.x][next.y]=1;
q.push(next);
}
if(Tu[next.x][next.y]=='B')
{
next.step=now.step+2;
bj[next.x][next.y]=1;
q.push(next);
}
}
}
printf("-1\n");
}
int main()
{
while(~scanf("%d%d",&m,&n),n,m)
{
init();
for(int i=0; i<m; i++)
{
getchar();
for(int j=0; j<n; j++)
{
scanf("%c",&Tu[i][j]);
if(Tu[i][j]=='Y')
{
sx=i;
sy=j;
}
if(Tu[i][j]=='T')
{
ex=i;
ey=j;
}
}
}
// printf("%d %d %d %d \n",sx,sy,ex,ey);
bfs(sx,sy,0);
}
return 0;
}
NYOJ 284 坦克大战 (广搜)的更多相关文章
- nyoj 284 坦克大战 简单搜索
题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=284 题意:在一个给定图中,铁墙,河流不可走,砖墙走的话,多花费时间1,问从起点到终点至少 ...
- NYOJ 284 坦克大战 bfs + 优先队列
这类带权的边的图,直接广搜不行,要加上优先队列,这样得到的结果才是最优的,这样每次先找权值最小的,代码如下 #include <stdio.h> #include <iostream ...
- NYOJ 284 坦克大战 【BFS】+【优先队列】
坦克大战 时间限制:1000 ms | 内存限制:65535 KB 难度:3 描写叙述 Many of us had played the game "Battle city" ...
- nyoj 284 坦克大战 (优先队列)
题目链接:http://acm.nyist.net/JudgeOnline/status.php?pid=284 特殊数据: 5 5 BBEEY EEERB SSERB SSERB SSETB 7 非 ...
- nyoj 613 免费馅饼 广搜
免费馅饼 时间限制:1000 ms | 内存限制:65535 KB 难度:3 描述 都说天上不会掉馅饼,但有一天gameboy正走在回家的小径上,忽然天上掉下大把大把的馅饼.说来gameboy ...
- NYOJ 483 Nightmare 【广搜】+【无标记】
版权声明:长风原创 https://blog.csdn.net/u012846486/article/details/31032479 Nightmare 时间限制:1000 ms | 内存限制: ...
- nyoj 592 spiral grid(广搜)
题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=592 解决以下问题后就方便用广搜解: 1.将数字坐标化,10000坐标为(0,0),这样就 ...
- nyoj 523 双向广搜
题目链接: http://acm.nyist.net/JudgeOnline/problem.php?pid=523 #include<iostream> #include<cstd ...
- nyoj 999——师傅又被妖怪抓走了——————【双广搜】
师傅又被妖怪抓走了 时间限制:1000 ms | 内存限制:65535 KB 难度:3 描述 话说唐僧复得了孙行者,师徒们一心同体,共诣西方.自宝象国救了公主,承君臣送出城西,沿路饥餐渴饮,悟 ...
随机推荐
- EF使用报错说缺少引用
在程序中已经引用了EF,也引用了System.Data,但是一起报这个错误: 在类前面也已经写了 using System.Data.Entity,百思不得其解,最后才发 ...
- Android学习笔记之,调用系统图库,添加自定义字体,屏幕截图
新年开始的第一天就来学习了慕课迎春活动中的Android心愿分享一课,学到了几个知识点,在此记录一下. 1.调用系统图库调用系统图库用的是intent,步骤为弹出系统图库选择器,选择图片后获取到所选择 ...
- 揭秘css
这是我看到非常好的一本电子教程,可以当参考手册使用,链接
- APPium-python实例(记录)
https://github.com/appium/sample-code/tree/master/sample-code/examples/python
- Laxcus大数据分布计算演示实例
Laxcus大数据管理系统提供了基于Diffuse/Converge分布算法的计算能力.算法的具体介绍详见<Laxcus:大数据处理系统>一文.本图展示了在集群环境下的随机数产生.排序.显 ...
- 语法测试cnblogs使用Markdown
参考自作业部落Cmd Markdown 编辑器 https://www.zybuluo.com 欢迎使用 Cmd Markdown 编辑阅读器 什么是 Markdown Markdown 是一种方便记 ...
- 官方文档 恢复备份指南八 RMAN Backup Concepts
本章内容 Consistent and Inconsistent RMAN Backups Online Backups and Backup Mode Backup Sets Image Copie ...
- PHP 用Symfony VarDumper Component 调试
Symfony VarDumper 类似 php var_dump() 官方文档写的安装方法 : 按照步骤 就可以在 running any PHP code 时候使用了 In order to h ...
- Spring Boot(一)入门篇
SpringBoot概述 Spring Boot的诞生简化了Spring应用开发,SpringBoot提供对Spring容器.第三方插件等很多服务的管理.对于大部分Spring应用,无论是简单的web ...
- 绝对定位后a、button等hover状态样式不显示问题
<div class="operate"> <el-button>提交项目</el-button> <el-button type=&qu ...