坦克大战

时间限制: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

较为水的一题。只需要转变为权值,然后就搜索即可,当然这道题,若果采用盲深搜索的话,会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坦克大战(带权值的图搜索)的更多相关文章

  1. nyoj 284 坦克大战 简单搜索

    题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=284 题意:在一个给定图中,铁墙,河流不可走,砖墙走的话,多花费时间1,问从起点到终点至少 ...

  2. NYOJ 284 坦克大战 bfs + 优先队列

    这类带权的边的图,直接广搜不行,要加上优先队列,这样得到的结果才是最优的,这样每次先找权值最小的,代码如下 #include <stdio.h> #include <iostream ...

  3. NYOJ 284 坦克大战 【BFS】+【优先队列】

    坦克大战 时间限制:1000 ms  |  内存限制:65535 KB 难度:3 描写叙述 Many of us had played the game "Battle city" ...

  4. nyoj 284 坦克大战 (优先队列)

    题目链接:http://acm.nyist.net/JudgeOnline/status.php?pid=284 特殊数据: 5 5 BBEEY EEERB SSERB SSERB SSETB 7 非 ...

  5. NYOJ 284 坦克大战 (广搜)

    题目链接 描述 Many of us had played the game "Battle city" in our childhood, and some people (li ...

  6. 51nod1459(带权值的dijkstra)

    题目链接:https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1459 题意:中文题诶- 思路:带权值的最短路,这道题数据也没 ...

  7. HDU 1863:畅通project(带权值的并查集)

    畅通project Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

  8. 洛谷 P2024 [NOI2001]食物链——带权值的并查集维护

    先上一波题目 https://www.luogu.org/problem/P2024 通过这道题复习了一波并查集,学习了一波带权值操作 首先我们观察到 所有的环都是以A->B->C-> ...

  9. 带权值的LCA

    例题:http://poj.org/problem?id=1986 POJ1986 Distance Queries Language: Default Distance Queries Time L ...

随机推荐

  1. 【转载】在Linux中使用VS Code编译调试C++项目

    原文:在Linux中使用VS Code编译调试C++项目 最近项目需求,需要在Linux下开发C++相关项目,经过一番摸索,简单总结了一下如何通过VS Code进行编译调试的一些注意事项. 关于VS ...

  2. 【转载】STL"源码"剖析-重点知识总结

    原文:STL"源码"剖析-重点知识总结 STL是C++重要的组件之一,大学时看过<STL源码剖析>这本书,这几天复习了一下,总结出以下LZ认为比较重要的知识点,内容有点 ...

  3. Datatypes In SQLite Version 3

    http://www.sqlite.org/datatype3.html http://stackoverflow.com/questions/7337882/what-is-the-differen ...

  4. 如何选择正确的DevOps工具

    坦白的讲:世界上没有哪种工具能够像DevOps这么神奇(或敏捷,或精益).DevOps在开发和运营团队之间建立了完美的合作与沟通,因此与其说这是一种神奇的工具,不如说是一种文化的转变. 然而,团队之间 ...

  5. [HDU5907]Find Q(水)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5907 记下每块大小,然后n*(n+1)/2 #include <bits/stdc++.h> ...

  6. web后门top

    看到了一个博客  觉得关键点很有用 1)出现频率最高的DDoS后门文件名 abc.php, xl.php, Xml.php, dedetag.class.php, counti.php, plase. ...

  7. dup2()函数的使用,

    #define STR "xiamanman\n"#define STR_LEN 10#define STDOUT 1 #include <stdio.h>#inclu ...

  8. [C和指针]第三部分

    声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...

  9. STORM_0004_windows下zookeeper的伪集群的搭建

    -----------------------------------------------------START------------------------------------------ ...

  10. HTML笔记(二) 在HTML中使用CSS

    外部CSS: <head> <link rel="stylesheet" type="text/css" href="mystyle ...