Battle City
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 7208 | Accepted: 2427 |
Description

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
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
Sample Input
3 4
YBEB
EERE
SSTE
0 0
Sample Output
8
Source
POJ Monthly,鲁小石
#include<stdio.h>
#include<string.h>
#include<queue>
#include<algorithm>
using namespace std;
char map[1010][1010];
int vis[1010][1010],m,n,x,y;
int dx[4]={0,1,-1,0};
int dy[4]={1,0,0,-1};
struct node
{
int x,y;
int step;
friend bool operator < (node n1,node n2)
{
return n1.step>n2.step;
}
}p,temp;
int judge(node s)
{
if(s.x<0||s.x>=m||s.y<0||s.y>=n)
return 1;
if(map[s.x][s.y]=='S'||map[s.x][s.y]=='R')
return 1;
if(vis[s.x][s.y])
return 1;
return 0;
}
int bfs()
{
priority_queue<node>q;
p.x=x;p.y=y;
p.step=0;
memset(vis,0,sizeof(map));
vis[x][y]=1;
q.push(p);
while(!q.empty())
{
p=q.top();
q.pop();
for(int i=0;i<4;i++)
{
temp.x=p.x+dx[i];
temp.y=p.y+dy[i];
if(judge(temp)) continue;
if(map[temp.x][temp.y]=='B')
temp.step=p.step+2;
else temp.step=p.step+1;
if(map[temp.x][temp.y]=='T')
return temp.step;
vis[temp.x][temp.y]=1;
q.push(temp);
}
}
return -1;
}
int main()
{
while(scanf("%d%d",&m,&n),m||n)
{
int i,j;
for(i=0;i<m;i++)
{
scanf("%s",map[i]);
for(j=0;j<n;j++)
{
if(map[i][j]=='Y')
{
x=i;y=j;
}
}
}
printf("%d\n",bfs());
}
return 0;
}
Battle City的更多相关文章
- poj 2312 Battle City
题目连接 http://poj.org/problem?id=1840 Battle City Description Many of us had played the game "Bat ...
- poj 2312 Battle City【bfs+优先队列】
Battle City Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7579 Accepted: 2544 Des ...
- B - Battle City bfs+优先队列
来源poj2312 Many of us had played the game "Battle city" in our childhood, and some people ( ...
- 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 ...
- POJ - 2312 Battle City BFS+优先队列
Battle City 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 ...
- poj2312 Battle City 【暴力 或 优先队列+BFS 或 BFS】
题意:M行N列的矩阵.Y:起点,T:终点.S.R不能走,走B花费2,走E花费1.求Y到T的最短时间. 三种解法.♪(^∇^*) //解法一:暴力 //157MS #include<cstdio& ...
- poj 2312 Battle City(优先队列+bfs)
题目链接:http://poj.org/problem?id=2312 题目大意:给出一个n*m的矩阵,其中Y是起点,T是终点,B和E可以走,S和R不可以走,要注意的是走B需要2分钟,走E需要一分钟. ...
随机推荐
- JavaScript异步加载方案
(1) defer,只支持IE defer属性的定义和用法(我摘自w3school网站) defer 属性规定是否对脚本执行进行延迟,直到页面加载为止. 有的 javascript 脚本 docume ...
- Css小动画
html页面: <!DOCTYPE html><html lang="en"><head> <meta charset="UTF ...
- 【Oracle】redo与undo
一 .redo(重做信息) 是Oracle在线(或归档)重做日志文件中记录的信息,万一出现失败时可以利用这些数据来“重放”(或重做)事务.Oracle中记录这些信息的文件叫做redo log file ...
- python tips:dict的key顺序
python3.6+版本中,dict的键值保持插入有序. t = list(range(10)) b = t[:] d = dict(zip(t, b)) print(list(d.items())) ...
- 02 java学习安装jdk及其环境配置
SUN公司1995年正式推出的一款语言 其实之前,Sun公司1991年,James Gosling等人就开始开发Oak语言,希望用于控制嵌入有效电视交换盒,1994年更名为Java,之前来自 与jav ...
- 单链表每k个节点一组进行反转(最后不足k个也反转)
一道面试题,第一次碰到这道题的时候 要求10分钟之内手写代码实现,当时没写出来,后来花点时间把过程梳理一遍,也挺简单的....... 思路就是在原来单链表反转的基础上,加几个控制参数,记录几个关键节点 ...
- Day 20 python基础总复习
一.计算机基础 1.1 计算机基础之编程 编程语言是人与计算机之间交流的介质 编程就是写一堆文件 编程为了奴隶计算机,解放劳动力 1.2 计算机组成原理 CPU 控制器:控制硬件 运算器:逻辑运算和算 ...
- JavaScript for循环元素取下标问题
<ul> <li>fg</li> <li>gd</li> <li>gds</li> <li>ghe< ...
- 【剑指Offer】46、圆圈中最后剩下的数
题目描述: 每年六一儿童节,牛客都会准备一些小礼物去看望孤儿院的小朋友,今年亦是如此.HF作为牛客的资深元老,自然也准备了一些小游戏.其中,有个游戏是这样的:首先,让小朋友们围成一个大圈.然后 ...
- css image-set 让浏览器自动切换1x,2x图片
方法一: <img src="img.png" srcset="path/img.png 2x,path/img.png.png 3x"/> 方法二 ...