nyoj-----284坦克大战(带权值的图搜索)
坦克大战
- 描述
-
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
较为水的一题。只需要转变为权值,然后就搜索即可,当然这道题,若果采用盲深搜索的话,会tie
贴一下tle代码,记录做题的思路吧!
代码:
#include<iostream>
#include<cstring>
#include<cstdio>
#include<vector>
#include<cstdlib>
using namespace std;
const int maxn=;
int map[maxn][maxn];
//先采用深度搜索
int dir[][]={{,},{-,},{,},{,-}};
int m,n,sx,sy,ex,ey,minc,res;
void dfs(int x,int y)
{
//剪枝
if(x>&&y>&&x<=m&&y<=n)
{
if(x==ex&&y==ey)
{
if(res>minc) res=minc;
return ;
}
for(int i=;i<;i++)
{
int temp_val=map[x+dir[i][]][y+dir[i][]];
if(temp_val>)
{
minc+=temp_val;
map[x+dir[i][]][y+dir[i][]]=-0x3f3f3f3f;
dfs(x+dir[i][],y+dir[i][]);
map[x+dir[i][]][y+dir[i][]]=temp_val;
minc-=temp_val;
}
}
}
} int main()
{
int i,j;
char ss;
//freopen("test.in","r",stdin);
while(scanf("%d%d",&m,&n)!=EOF&&n+m)
{
getchar();
memset(map,,sizeof(map));
minc=;
res=0x3f3f3f;
for(i=;i<=m;i++)
{
for(j=;j<=n;j++)
{ scanf("%c",&ss);
if(ss=='E')
map[i][j]=;
else if(ss=='B')
map[i][j]=;
else if(ss=='Y')
{
sx=i;
sy=j;
}
else if(ss=='T')
{
map[i][j]=;
ex=i;
ey=j;
}
else
map[i][j]=-0x3f3f3f3f; //代表障碍物
}
getchar();
}
//algorithm functiom
dfs(sx,sy);
printf("%d\n",res);
}
return ;
}
上面的思路是tle了的,下面的是ac的,加了一个贪心的思想.
代码:时间为 4ms
#include<iostream>
#include<cstring>
#include<cstdio>
#include<vector>
#include<cstdlib>
using namespace std;
const int maxn=;
struct node
{
int val;
int minc;
};
node map[maxn][maxn];
//先采用深度搜索
int dir[][]=
{
{,}, //向右
{-,}, //向左
{,}, //向上
{,-} //向下
};
int m,n,sx,sy,ex,ey;
void dfs(int x,int y)
{
//剪枝
if(x>&&y>&&x<=m&&y<=n)
{
for(int i=;i<;i++)
{
if(map[x+dir[i][]][y+dir[i][]].val>&&map[x+dir[i][]][y+dir[i][]].minc>(map[x][y].minc+map[x+dir[i][]][y+dir[i][]].val)) //非智能 shit
{
map[x+dir[i][]][y+dir[i][]].minc=(map[x][y].minc+map[x+dir[i][]][y+dir[i][]].val);
dfs(x+dir[i][],y+dir[i][]);
}
}
}
} int main()
{
int i,j;
char ss;
// freopen("test.in","r",stdin);
while(scanf("%d%d",&m,&n)!=EOF&&n+m)
{
getchar();
memset(map,,sizeof(map));
for(i=;i<=m;i++)
{
for(j=;j<=n;j++)
{
map[i][j].minc=0x3f3f3f3f;
scanf("%c",&ss);
if(ss=='E')
map[i][j].val=;
else if(ss=='B')
map[i][j].val=;
else if(ss=='Y')
{
map[i][j].minc=;
sx=i;
sy=j;
}
else if(ss=='T')
{
map[i][j].val=;
ex=i;
ey=j;
}
else
map[i][j].val=-; //代表障碍物
}
getchar();
}
//algorithm functiom
dfs(sx,sy);
if(map[ex][ey].minc==0x3f3f3f3f)
printf("-1\n");
else
printf("%d\n",map[ex][ey].minc);
}
return ;
}
当然还可以用bfs来做,时间会更快什么的........
nyoj-----284坦克大战(带权值的图搜索)的更多相关文章
- nyoj 284 坦克大战 简单搜索
题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=284 题意:在一个给定图中,铁墙,河流不可走,砖墙走的话,多花费时间1,问从起点到终点至少 ...
- NYOJ 284 坦克大战 bfs + 优先队列
这类带权的边的图,直接广搜不行,要加上优先队列,这样得到的结果才是最优的,这样每次先找权值最小的,代码如下 #include <stdio.h> #include <iostream ...
- NYOJ 284 坦克大战 【BFS】+【优先队列】
坦克大战 时间限制:1000 ms | 内存限制:65535 KB 难度:3 描写叙述 Many of us had played the game "Battle city" ...
- nyoj 284 坦克大战 (优先队列)
题目链接:http://acm.nyist.net/JudgeOnline/status.php?pid=284 特殊数据: 5 5 BBEEY EEERB SSERB SSERB SSETB 7 非 ...
- NYOJ 284 坦克大战 (广搜)
题目链接 描述 Many of us had played the game "Battle city" in our childhood, and some people (li ...
- 51nod1459(带权值的dijkstra)
题目链接:https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1459 题意:中文题诶- 思路:带权值的最短路,这道题数据也没 ...
- HDU 1863:畅通project(带权值的并查集)
畅通project Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- 洛谷 P2024 [NOI2001]食物链——带权值的并查集维护
先上一波题目 https://www.luogu.org/problem/P2024 通过这道题复习了一波并查集,学习了一波带权值操作 首先我们观察到 所有的环都是以A->B->C-> ...
- 带权值的LCA
例题:http://poj.org/problem?id=1986 POJ1986 Distance Queries Language: Default Distance Queries Time L ...
随机推荐
- 0(Mark)(随时添加) ubuntu的一些 终端 命令整理
MARK 1 查看cpu信息cat /proc/cpiinfo 2 查看ubuntu版本:cat /etc/issue 3 查看系统是32位还是64位方法1:#查看long的位数,返回32或64 ge ...
- kaili 2.0 虚拟机修改ip
第一步:将虚拟机设置为桥接状态,并勾选上复制物理网络连接状态
- FJNU 1159 Fat Brother’s new way(胖哥的新姿势)
FJNU 1159 Fat Brother’s new way(胖哥的新姿势) Time Limit: 1000MS Memory Limit: 257792K [Description] [题目 ...
- JS——时间日期控件
原文:http://blog.sina.com.cn/s/blog_621768f30100qmfz.html 今天找到一个还不错的日历控件 下载地址:http://www.my97.net/dp/d ...
- hdu 4163 Stock Prices 水
#include<bits/stdc++.h> using namespace std; #define ll long long #define pi (4*atan(1.0)) #de ...
- android上让我放弃使用wstring来操作中英文字符串 转
android上让我放弃使用wstring来操作中英文字符串 2013-08-07 16:37:24| 分类: cocos2d|举报|字号 订阅 项目需要,需要对中英文字符串进行遍历修改等, ...
- iOS - KVO 键值观察
1.KVO KVO 是 Key-Value Observing 的简写,是键值观察的意思,属于 runtime 方法.Key Value Observing 顾名思义就是一种 observer 模式用 ...
- js 函数-Tom
函数类型 在ECMAScript 中有三种函数类型:函数声明,函数表达式和函数构造器创建的函数.每一种都有自己的特点. 函数声明 函数声明(缩写为FD)是这样一种函数: 有一个特定的名称 在源码中的位 ...
- 半平面交模板(O(n*n)&& O(n*log(n))
摘自http://blog.csdn.net/accry/article/details/6070621 首先解决问题:什么是半平面? 顾名思义,半平面就是指平面的一半,我们知道,一条直线可以将平面分 ...
- Mysql 命令行工具
1.Mysql命令行工具分为两类:服务端命令行工具和客户端命令行工具. 2.服务端工具 mysql_install_db:建库工具 mysqld_safe:Mysql服务的启动工具,mysqld_sa ...