POJ - 2312 Battle City BFS+优先队列
Battle City
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
Output
Sample Input
3 4
YBEB
EERE
SSTE
0 0
Sample Output
8
又是一道BFS+优先队列的题,和之前的WAR CHESS类似,但判断条件较少。这次主要是想作个比较,用暴力DFS跑一边,果断TLE。。BFS+优先队列只遍历最少步数之内的点一次,时间效率快很多
dfs length 861 time 1s+
bfs length 1469 time 0.016s memory 1m
Status | Time Limit Exceeded |
---|---|
Length | 861 |
Lang | G++ |
Submitted | 2017-07-22 22:22:12 |
Shared | |
RemoteRunId | 17284030 |
Status | Accepted |
---|---|
Time | 16ms |
Memory | 1028kB |
Length | 1469 |
Lang | G++ |
Submitted | 2017-07-22 22:22:58 |
Shared | |
RemoteRunId | 17284040 |
#include<stdio.h>
#include<string.h>
char a[][];
int b[][];
int n,m,f,bx,by,min;
void dfs(int x,int y,int s)
{
if(x<||y<||x>=n||y>=m) return;
if(b[x][y]==) return;
if(a[x][y]=='T'){
f=;
if(s<min) min=s;
return;
}
else if(a[x][y]=='S'){
b[x][y]=;
return;
}
else if(a[x][y]=='R'){
b[x][y]=;
return;
}
else if(a[x][y]=='B'){
s++;
}
b[x][y]=;dfs(x+,y,s+);b[x][y]=;
b[x][y]=;dfs(x,y+,s+);b[x][y]=;
b[x][y]=;dfs(x-,y,s+);b[x][y]=;
b[x][y]=;dfs(x,y-,s+);b[x][y]=;
}
int main()
{
int i,j;
while(scanf("%d%d",&n,&m)&&!(n==&&m==)){
memset(a,,sizeof(a));
memset(b,,sizeof(b));
for(i=;i<n;i++){
scanf("%s",a[i]);
for(j=;j<m;j++){
if(a[i][j]=='Y'){
bx=i;by=j;
}
}
}
f=;
min=;
dfs(bx,by,);
if(f==) printf("%d\n",min);
else printf("-1\n");
}
return ;
}
#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std; char a[][];
int b[][];
int t[][]={{,},{,},{-,},{,-}};
int n,m,bx,by; struct Node{
int x,y,s;
friend bool operator<(Node a,Node b)
{
return a.s>b.s;
}
}node; void bfs()
{
int i,f=,tx,ty;
priority_queue<Node> q;
node.x=bx;
node.y=by;
node.s=;
b[bx][by]=;
q.push(node);
while(q.size()){
for(i=;i<;i++){
tx=q.top().x+t[i][];
ty=q.top().y+t[i][];
if(tx<||ty<||tx>=n||ty>=m) continue;
if(b[tx][ty]==) continue;
b[tx][ty]=;
if(a[tx][ty]=='T'){
printf("%d\n",q.top().s+);
f=;
return;
}
else if(a[tx][ty]=='S'){
b[tx][ty]=;
continue;
}
else if(a[tx][ty]=='R'){
b[tx][ty]=;
continue;
}
else if(a[tx][ty]=='B'){
node.x=tx;
node.y=ty;
node.s=q.top().s+;
b[tx][ty]=;
q.push(node);
}
else if(a[tx][ty]=='E'){
node.x=tx;
node.y=ty;
node.s=q.top().s+;
b[tx][ty]=;
q.push(node);
}
}
q.pop();
}
if(f==) printf("-1\n");
}
int main()
{
int i,j;
while(scanf("%d%d",&n,&m)&&!(n==&&m==)){
memset(a,,sizeof(a));
memset(b,,sizeof(b));
for(i=;i<n;i++){
scanf("%s",a[i]);
for(j=;j<m;j++){
if(a[i][j]=='Y'){
bx=i;by=j;
}
}
}
bfs();
}
return ;
}
POJ - 2312 Battle City BFS+优先队列的更多相关文章
- poj 2312 Battle City【bfs+优先队列】
Battle City Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7579 Accepted: 2544 Des ...
- poj 2312 Battle City
题目连接 http://poj.org/problem?id=1840 Battle City Description Many of us had played the game "Bat ...
- B - Battle City bfs+优先队列
来源poj2312 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 ...
- poj 2312 Battle City(优先队列+bfs)
题目链接:http://poj.org/problem?id=2312 题目大意:给出一个n*m的矩阵,其中Y是起点,T是终点,B和E可以走,S和R不可以走,要注意的是走B需要2分钟,走E需要一分钟. ...
- 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 ...
- poj2312 Battle City 【暴力 或 优先队列+BFS 或 BFS】
题意:M行N列的矩阵.Y:起点,T:终点.S.R不能走,走B花费2,走E花费1.求Y到T的最短时间. 三种解法.♪(^∇^*) //解法一:暴力 //157MS #include<cstdio& ...
- POJ 1724 ROADS(BFS+优先队列)
题目链接 题意 : 求从1城市到n城市的最短路.但是每条路有两个属性,一个是路长,一个是花费.要求在花费为K内,找到最短路. 思路 :这个题好像有很多种做法,我用了BFS+优先队列.崔老师真是千年不变 ...
随机推荐
- C#注冊表操作汇总
一.注冊表基本知识 1) 结构 键->项->子项->值项(名称.类型.数据) REG_SZ 字符串 REG_BINARY 二进制 REG_DWORD ...
- 相似进程死掉Process com.midea.mmp2 died.
此异常查到网上有一篇不错的文章例如以下: 08:56:03,273 INFO – 运行Do func=[GetSeqNo] keyNam=[keynam];KeyVal=[PRYPAYBILSYSTR ...
- apktool + eclipse 动态调试APK
用了会AndBug,尽管挺强大的可是作为习惯了OD.EDB作为动态调试工具的人,自然有些不习惯,于是乎寻求新的动态调试解决方式.但大多数都是NetBeans + apktool.想着还得多下一个IDE ...
- 用EasyClient开源项目采集Windows摄像头/麦克风的音视频进行RTSP直播
EasyClient是EasyDarwin开源流媒体团队开发的一款功能丰富的开源PC客户端项目,目前支持Windows.Android版本,后续将支持ios版本,其中Windows版本的EasyCli ...
- file descriptor 0 1 2 一切皆文件 stdout stderr stdin /dev/null 沉默是金 pipes
$>emtry_or_create_a_file.f $ll>>append_a_file.f standard output input error $ls -l /usr/bin ...
- Win32对话框工程笔记
Main.cpp #include <Windows.h> #include "resource.h" INT_PTR CALLBACK dialogProc(HWND ...
- BZOJ3627: [JLOI2014]路径规划
BZOJ3627: [JLOI2014]路径规划 Description 相信大家都用过地图上的路径规划功能,只要输入起点终点就能找出一条最优路线.现在告诉你一张地图的信息,请你找出最优路径(即最短路 ...
- 【LeetCode】Rotate List
Given a list, rotate the list to the right by k places, where k is non-negative. For example:Given 1 ...
- 周期性计划(一个cron守护进程):
周期性计划(一个cron守护进程): root@ubuntu:/etc# ps -ef | grep cron root 903 1 0 16:25 ? 00:00:00 /usr/sbin/cron ...
- [CPP] Coding Style
C++ Coding Style C++很多强大的语言特性导致它的复杂,其复杂性会使得代码更容易出现bug.难于阅读和维护. 由于,本人有一点点代码洁癖,所以依照Google的C++编程规范<G ...