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+优先队列.崔老师真是千年不变 ...
随机推荐
- 新一代AJAX API:FETCH
AJAX半遮半掩的底层API是饱受诟病的一件事情. XMLHttpRequest 并不是专为Ajax而设计的. 虽然各种框架对 XHR 的封装已经足够好用, 但我们可以做得更好.更好用的API是 fe ...
- OpenWrt:路由器上的Linux
官网:https://openwrt.org/ 适于嵌入式设备的一个Linux发行版,可刷无线路由器. 相对原厂固件而言,OpenWrt不是一个单一.静态的固件,而是提供了一个可添加软件包的可写的文件 ...
- Hadoop集群_Hadoop安装配置
1.集群部署介绍 1.1 Hadoop简介 Hadoop是Apache软件基金会旗下的一个开源分布式计算平台.以Hadoop分布式文件系统(HDFS,Hadoop Distributed Filesy ...
- 目标检测之harr---角点检测harr 的opencv实现
本系列文章由@浅墨_毛星云 出品,转载请注明出处. 文章链接: http://blog.csdn.net/poem_qianmo/article/details/29356187 作者:毛星云(浅墨) ...
- dsoframer注冊说明及在VC2010使用
一.dsoframer在XP.win7和win8中的注冊方法. 从微软站点下载DsoFrmaer_KB311765_x86.exe,双击解开后得到的dsoframer.ocx等文件. (一)XP注冊 ...
- objective-c中#import和@class的差别
在Objective-C中,能够使用#import和@class来引用别的类型, 可是你知道两者有什么差别吗? @class叫做forward-class, 你常常会在头文件的定义中看到通过@cla ...
- Html.DropDownListFor的选项值为字符型问题
我快要疯了.asp.net mvc的这个DropDownListFor,无论在服务器端如何设置,设置哪个值被选中,结果到了页面输出,选中值根本没有被选中,没有任何一个值被选中,下拉框只冷冰冰地显示一个 ...
- jquery 获取cookie的值 中文乱码的问题
1.说明 测试环境asp.net mvc4,前台获取cookie的值需要引用js文件: <script src="JS/jquery.cookie.js"></ ...
- 九度OJ 1094:String Matching(字符串匹配) (计数)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:1259 解决:686 题目描述: Finding all occurrences of a pattern in a text is a p ...
- Android笔记之启动界面的设置
默认情况下,启动界面是白屏 我们自定义一个启动界面如下,3秒钟后进入主界面并结束启动页 SplashActivity.java package com.bu_ish.myapp; import and ...