坦克大战

时间限制: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. 2012 #5 History repeat itself

    History repeat itself Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I6 ...

  2. Logical Databases逻辑数据库

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

  3. NPOI利用多任务模式分批写入多个Excel

    接上文NPOI大数据分批写入同个Excel,这次是利用task多任务同时写入到多个Excel. Form2.cs private void btnExport_Click(object sender, ...

  4. 测算Redis处理实际生产请求的QPS/TPS

    测算Redis处理实际生产请求的QPS/TPS Benchmark工具 redis发布版本中自带了redis-benchmark性能测试工具; 示例: 使用50个并发连接,发出100000个请求,每个 ...

  5. Python基础学习笔记(一)入门

    参考资料: 1. <Python基础教程> 2. http://www.runoob.com/python/python-chinese-encoding.html 3. http://w ...

  6. [转]ubuntu环境变量配置文件

    参考网址:http://www.bkjia.com/Linuxjc/1008127.html Ubuntu通常使用的几个配置文件主要有下面这几个: /etc/environment./etc/prof ...

  7. 高处胜寒 php中奖概率算法,可用于刮刮卡,大转盘等抽奖算法

    <?php /* * 经典的概率算法, * $proArr是一个预先设置的数组, * 假设数组为:array(100,200,300,400), * 开始是从1,1000 这个概率范围内筛选第一 ...

  8. maven核心概念4

    一.Maven坐标 1.1.什么是坐标? 在平面几何中坐标(x,y)可以标识平面中唯一的一点. 1.2.Maven坐标主要组成 groupId:组织标识(包名) artifactId:项目名称 ver ...

  9. 【Todo】【转载】深度学习&神经网络 科普及八卦 学习笔记 & GPU & SIMD

    上一篇文章提到了数据挖掘.机器学习.深度学习的区别:http://www.cnblogs.com/charlesblc/p/6159355.html 深度学习具体的内容可以看这里: 参考了这篇文章:h ...

  10. android 自定义进度条颜色

    android 自定义进度条颜色 先看图 基于产品经理各种自定义需求,经过查阅了解,下面是自己对Android自定义进度条的学习过程!   这个没法了只能看源码了,还好下载了源码, sources\b ...