NYOJ 284 坦克大战
2016-04-29 17:41
471 查看
坦克大战
时间限制:1000 ms | 内存限制:65535 KB难度:3
描述
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
来源POJ
上传者
sadsad
#include<stdio.h> #include<iostream> #include<string.h> #include<queue> #include<algorithm> #define M 30 using namespace std; char s[310][310]; bool vis[310][310]; struct node{ int x,y,step; // 坐标和时间 node(int x = 0,int y =0 ,int step= 0):x(x),y(y),step(step){};//????? bool operator < (const node &a)const{ return step > a.step; } }st,ed; int dx[4] = {0,0,-1,1}; int dy[4] = {1,-1,0,0};//四个方向 int n,m; int bfs(){ priority_queue<node>Q; Q.push(st); memset(vis,0,sizeof(vis)); while(!Q.empty()){ node now = Q.top();//目前的队首 Q.pop(); node nt; for(int i=0;i<4;i++){ nt.x = now.x + dx[i]; nt.y = now.y + dy[i]; nt.step = now.step + 1;//找四个方向的 if(nt.x >=0 && nt.x < n && nt.y>=0 && nt.y<m&&!vis[nt.x][nt.y] ){//判断不要出界 if(s[nt.x][nt.y] == 'E'){ vis[nt.x][nt.y] = 1; Q.push(nt); }//是路 加一结果入队 else if(s[nt.x][nt.y] == 'T'){ return nt.step; }//是目标返回时间 else if(s[nt.x][nt.y] == 'B'){ nt.step++; // 队列中的step不同 //因而优先队列是正解 vis[nt.x][nt.y] = 1; Q.push(nt); }//是障碍 加二结果入队 } } } return -1;//没有结果 } int main(){ while(scanf("%d%d",&n,&m)!=EOF &&(n || m)){ for(int i=0;i<n;i++) scanf("%s",s[i]); for(int i=0;i<n;i++){ for(int j=0;j<m;j++){ if(s[i][j] == 'Y') st.x = i,st.y = j; else if(s[i][j] == 'T') ed.x = i,ed.y = j; } }//找到起点和终点的坐标 st.step = 0;//起始时间置0 printf("%d\n",bfs()); } return 0; }
相关文章推荐
- 写博客啦~
- 实验八 路由器静态路由配置
- Android res资源文件夹的知识积累
- 团队绩效评估规划
- Apache Solr:全文搜索
- 特效集合
- glBlendFunc();--混合
- CentOS 7 中,创建桌面快捷方式
- Harris角点检测算法基本理解
- SDL绘制bmp
- NYOJ 20 吝啬的国度
- Gson原理简析
- bootstrap学习笔记(5)
- 各种特效集合
- 设计模式之简单工厂模式
- Python学习路-常用函数(一)
- Java程序员必须掌握的8大排序算法
- NYOJ 349 Sorting It All Out
- css3实现循环执行动画,且动画每次都有延迟
- Python使用Redis数据库