坦克大战

时间限制: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. 【Unity3D游戏开发】之Sprite Packer使用方法 (九)[转]

    前置说明: 我们用来做sprite 的图片,通常会留有很多空白的地方,我们在画完了sprite之后,这些地方很可能就没有什么作用了.如果想避免这些资源上的浪费,我们可以把各个sprite做成图集,把图 ...

  2. [转]Unity 脚本生命周期流程图

    渲染 OnPreCull: 在相机剔除场景之前调用此函数.相机可见的对象取决于剔除.OnPreCull 函数调用发生在剔除之前. OnBecameVisible/OnBecameInvisible:  ...

  3. HTTP协议:header标头说明

    原文地址   http://blog.chinaunix.net/uid-7374279-id-4518834.html Header 解释 示例 Accept-Ranges 表明服务器是否支持指定范 ...

  4. Django中的分页

    直接看代码吧,还算比较简单: 先确认数据量有多少 根据页面显示数据的多少来分割数据,得到页面的开始数据和结束数据 根据开始和截止数据去切片数据,并且得到总共的页码数 根据一页显示多少页码和当前页码数, ...

  5. 青岛理工大学第五届ACM交流赛 部分题解

    A:后缀维护si*pi的最小值,查询的时候二分,判断后缀和当前两个部分就行. #include <bits/stdc++.h> using namespace std; typedef l ...

  6. 关于oracle sql developer乱码的问题

    写了一个sql查询,我擦居然乱码了 然后双击下这个框,居然又是中文: 有谁能够告诉我这是什么鬼

  7. python_way day14 CSS

    python_way day14 CSS 层叠样式表 一.CSS作用域: 二.css标签选择器 三.css样式 一.css作用域: 基本用法:style="样式" <body ...

  8. iOS - Swift NSRect 位置和尺寸

    前言 结构体,这个结构体用来表示事物的坐标点和宽高度. public typealias NSRect = CGRect public struct CGRect { public var origi ...

  9. netty概念

    Netty的ChannelFuture在Netty中的所有的I/O操作都是异步执行的,这就意味着任何一个I/O操作会立刻返回,不保证在调用结束的时候操作会执行完成.因此,会返回一个ChannelFut ...

  10. ecnu1624求交集多边形面积

    链接 本来在刷hdu的一道题..一直没过,看到谈论区发现有凹的,我这种方法只能过凸多边形的相交面积.. 就找来这道题试下水. 两个凸多边形相交的部分要么没有 要么也是凸多边形,那就可以把这部分单独拿出 ...