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 描述 话说唐僧复得了孙行者,师徒们一心同体,共诣西方.自宝象国救了公主,承君臣送出城西,沿路饥餐渴饮,悟 ...
随机推荐
- Android TextView 单行文本的坑
这是android系统的一个bug,描述如下:https://code.google.com/p/android/issues/detail?id=33868 具体来说就是当一个TextView设置了 ...
- 深度学习anchor的理解
摘抄与某乎 anchor 让网络学习到的是一种推断的能力.网络不会认为它拿到的这一小块 feature map 具有七十二变的能力,能同时从 9 种不同的 anchor 区域得到.拥有 anchor ...
- tensorflow学习笔记(2)-反向传播
tensorflow学习笔记(2)-反向传播 反向传播是为了训练模型参数,在所有参数上使用梯度下降,让NN模型在的损失函数最小 损失函数:学过机器学习logistic回归都知道损失函数-就是预测值和真 ...
- zookeeper3.4.6完全分布式安装
首先在官网下载zookeeper3.4.6安装包,解压到/usr/local目录下 然后改名为zookeeper. 环境变量配置:sudo vim /etc/profile 添加环境变量如下图 然后 ...
- 【bzoj2438】[中山市选2011]杀人游戏 Tarjan
题目描述 一位冷血的杀手潜入 Na-wiat,并假装成平民.警察希望能在 N 个人里面,查出谁是杀手.警察能够对每一个人进行查证,假如查证的对象是平民,他会告诉警察,他认识的人, 谁是杀手, 谁是平民 ...
- JAVA 异常处理的认知学习过程
没有异常处理 学生时代,我编写的java代码中,很少会有try catch.最主要的原因如下: 应用的规模很小 没有不确定因素 代码可控性高 如果规模小,往往就没有复杂的逻辑链路,整个软件的分层也很浅 ...
- [Leetcode] Reorder list 重排链表
Given a singly linked list L: L 0→L 1→…→L n-1→L n,reorder it to: L 0→L n →L 1→L n-1→L 2→L n-2→… You ...
- 【ZJ选讲·BZOJ 5073】
小A的咒语 给出两个字符串A,B (len<=105) 现在可以把A串拆为任意段,然后取出不超过 x 段,按在A串中的前后顺序拼接起来 问是否可以拼出B串. [题解] ①如果遇 ...
- wya费用流
#include<bits/stdc++.h> using namespace std; #define M 1005 #define inf 0x7fffffff #define T 6 ...
- 【BZOJ 2822】[AHOI2012]树屋阶梯 卡特兰数+高精
这道题随便弄几个数就发现是卡特兰数然而为什么是呢? 我们发现我们在增加一列时,如果这一个东西(那一列)他就一格,那么就是上一次的方案数,并没有任何改变,他占满了也是,然后他要是占两格呢,就是把原来的切 ...