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 描述 话说唐僧复得了孙行者,师徒们一心同体,共诣西方.自宝象国救了公主,承君臣送出城西,沿路饥餐渴饮,悟 ...
随机推荐
- 为什么说Objective-C是一门动态的语言?
object-c类的类型和数据变量的类型都是在运行是确定的,而不是在编译时确定.例如:多态特性,我们可以使用父类对象来指向子类对象,并且可以用来调用子类的方法.运行时(runtime)特性,我们可以动 ...
- 虚拟现实-VR-UE4-构建光照显示光照构建失败,Swarm启动失败
闲的无聊折腾,发现想构建光照的时候,总是显示失败 如下图 百度许久,有大神指出是我在编译源码的的时候没有将其中的某个模块编译进去,只需要重新编译摸个模块就好 在UE4 的sln文件下,会看到一个Unr ...
- C++学习007-使用exit退出进程
使用exit可以实现退出当前进程. 如下 在程序接收到一个字符后,就退出进程 编写环境 vs2015 int main() { int a = 10, b = 20; std::cout <&l ...
- mysql修改外部访问权限
mysql>use mysql; mysql>update user set host =’%’ where user=’root’ mysql>select host,user f ...
- C++结构体排序
在C++中,对结构体的排序方式比C语言丰富的多.在C语言中,我们主要是通过qsort进行排序操作(抛开手写排序算法不说). 在C++<algorithm>中,有一个十分强大的排序函数sor ...
- 【linux】亲测成功_CentOS7.2/rhel7.2 忘记root密码及重置root密码的方法?
本文转自:https://www.jb51.net/article/146320.htm CentOS 7 root密码的重置方式和CentOS 6完全不一样,以进入单用户模式修改root密码为例. ...
- C++ 点
2017/12/23 scoped_ptr类型的指针,只能在一个namespace中使用 1) 怎么查看元素在set中是否存在 1) istringstream类用于执行C++风格的串流的输入操作. ...
- [BinaryTree] 二叉搜索树(二叉查找树、二叉排序树)
二叉查找树(BinarySearch Tree,也叫二叉搜索树,或称二叉排序树BinarySort Tree)或者是一棵空树,或者是具有下列性质的二叉树: (1)若它的左子树不为空,则左子树上所有结点 ...
- Action中使用Json
1.前台页面中的ajax: //根据部门查询该部门下的用户列表 function doSelectDept(){ //1.获取部门 var dept = $("#toCompDept opt ...
- jquery中ajax的使用(java)
AJAX方式 js:界面 var prjContextPath='<%=request.getContextPath()%>'; $(document).ready(function() ...