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 描述 话说唐僧复得了孙行者,师徒们一心同体,共诣西方.自宝象国救了公主,承君臣送出城西,沿路饥餐渴饮,悟 ...
随机推荐
- Python连接符的种类和使用区别
python的连接符主要有 加号(+).逗号(,).空格( ) .反斜线(\).join()的方式. 加号(+),demo如下: #注意,+只能连接字符串,如果一个是字符串一个是数字就会报错 pr ...
- c++ list_iterator demo
#include <iostream> #include <list> using namespace std; typedef list<int> Integer ...
- Delphi7目录结构----初学者参考
打开Delphi的安装目录,如C:\Program Files\Borland\Delphi7,你将会看到目录下包含了一些文件和文件夹: ² Source:存放的是Delpi提供的所有源 ...
- static和final的区别(转载)
Java中static 和final的区别 final定义的变量可以看做一个常量,不能被改变: final定义的方法不能被覆盖: final定义的类不能被继承. final static 就是再加上s ...
- 【廖雪峰老师python教程】——错误和调试
错误处理 try...except...finally...机制 try: print('try...') r = 10 / 0 print('result:', r) except ZeroDivi ...
- Spring框架中ModelAndView、Model、ModelMap的区别
转自:http://blog.csdn.net/liujiakunit/article/details/51733211 1. Model Model 是一个接口, 其实现类为ExtendedMode ...
- python学习总结---网络编程
网络编程 相关概念 - OSI七层模型:它从低到高分别是:物理层.数据链路层.网络层.传输层.会话层.表示层和应用层. - TCP/IP: 在OSI七层模型基础上简化抽象出来的一套网络协议簇,现在得到 ...
- Python之tornado框架实现翻页功能
1.结果如图所示,这里将html页面与网站的请求处理放在不同地方了 start.py代码 import tornado.ioloop import tornado.web from controlle ...
- 平面最近点对(HDU 1007)
题解:点击 #include <stdio.h> #include <string.h> #include <algorithm> #include <ios ...
- Linux TCP协议使用的变量
Linux /proc/sys/net/ipv4/* 变量 TCP变量:somaxconn - INTEGER listen()的backlog参数的上限,在用户态为SOMAXCONN.默认是1 ...