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 ...
随机推荐
- Html basic tag
The <p> tag defines a paragraph. http://www.w3schools.com/tags/tag_p.asp The <td> tag de ...
- Servlet&jsp基础:第二部分
声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...
- C# 线程(三):如何操纵一个线程
From : http://kb.cnblogs.com/page/42529/ 下面我们就动手来创建一个线程,使用Thread类创建线程时,只需提供线程入口即可.(线程入口使程序知道该让这个线程干什 ...
- Webbrowser控件判断网页加载完毕的简单方法 (转)
摘自:http://blog.csdn.net/cometnet/article/details/5261192 一般情况下,当ReadyState属性变成READYSTATE_COMPLETE时,W ...
- Python基础学习笔记(一)入门
参考资料: 1. <Python基础教程> 2. http://www.runoob.com/python/python-chinese-encoding.html 3. http://w ...
- C# int.Parse()与int.TryParse()
int i = -1;bool b = int.TryParse(null, out i);执行完毕后,b等于false,i等于0,而不是等于-1,切记. int i = -1;bool b = in ...
- Object Pascal 方法与技巧
4 方法与技巧 4.1 设置代码模板 代码模板是Delphi 的代码感知特性的一种,通过它可以快速.高效和正确地输入代码.代码模板将一些常用的语句块保存在模板中,然后程序员只要在代码编辑器中按下“Ct ...
- iOS开发之Xcode6 之手动添加Pch预编译文件
参考文档 http://blog.csdn.net/crazyzhang1990/article/details/44243343 红色部分为本人自己补充注意事项 在Xcode6之前,创建一个新工程x ...
- 【浏览器渲染原理】渲染树构建之渲染树和DOM树的关系(转载 学习中。。。)
在DOM树构建的同时,浏览器会构建渲染树(render tree).渲染树的节点(渲染器),在Gecko中称为frame,而在webkit中称为renderer.渲染器是在文档解析和创建DOM节点后创 ...
- Android开发面试经——1.常见人事面试问题
Android开发(29) 版权声明:本文为寻梦-finddreams原创文章,请关注:http://blog.csdn.net/finddreams 做为程序员,我们都是有梦想的人,有时候当我 ...