Battle City
2015-08-13 21:19
316 查看
Battle City
Description
Many of us had played the game "Battle city" in our childhood, and some people (like me) even often play it on computer now.
![](http://poj.org/images/2312_1.jpg)
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).
![](http://poj.org/images/2312_2.jpg)
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
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.
Output
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.
Sample Input
Sample Output
Source
POJ Monthly,鲁小石
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 7208 | Accepted: 2427 |
Many of us had played the game "Battle city" in our childhood, and some people (like me) even often play it on computer now.
![](http://poj.org/images/2312_1.jpg)
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).
![](http://poj.org/images/2312_2.jpg)
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
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.
Output
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.
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; }
相关文章推荐
- How to check which certificate was used to sign my app
- CodeForces-556B Case of Fake Numbers
- POJ 2312 Battle City
- zoj 2734 Exchange Cards【dfs+剪枝】
- swift 常用代码
- (转)Android中用OpenGL ES Tracer分析绘制过程
- STM32学习笔记——中断
- vim 配置vimrc
- ZOJ 3080 ChiBi [图论]
- 路由交换基本命令+一些笔记
- C# 它 抽象类和接口
- android Graphics(一):概述及基本几何图形绘制
- JAVA wait(), notify(),sleep详解
- (转)Android性能优化案例研究(下)
- 黑马程序员——Java基础--GUI(图形化界面)
- HDU--4821(字符串哈希)
- 【HDU】5383 Yu-Gi-Oh!【费用流】
- C语言中,头文件的作用,头文件和源文件的关系(转)
- [leetcode] Palindrome Number 回文数判断
- n皇后问题