nyoj-----284坦克大战(带权值的图搜索)

坦克大战

时间限制: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代码,记录做题的思路吧!

代码:

 1 #include<iostream>
2 #include<cstring>
3 #include<cstdio>
4 #include<vector>
5 #include<cstdlib>
6 using namespace std;
7 const int maxn=302;
8 int map[maxn][maxn];
9 //先采用深度搜索
10 int dir[4][2]={{0,1},{-1,0},{1,0},{0,-1}};
11 int m,n,sx,sy,ex,ey,minc,res;
12 void dfs(int x,int y)
13 {
14 //剪枝
15 if(x>0&&y>0&&x<=m&&y<=n)
16 {
17 if(x==ex&&y==ey)
18 {
19 if(res>minc) res=minc;
20 return ;
21 }
22 for(int i=0;i<4;i++)
23 {
24 int temp_val=map[x+dir[i][0]][y+dir[i][1]];
25 if(temp_val>0)
26 {
27 minc+=temp_val;
28 map[x+dir[i][0]][y+dir[i][1]]=-0x3f3f3f3f;
29 dfs(x+dir[i][0],y+dir[i][1]);
30 map[x+dir[i][0]][y+dir[i][1]]=temp_val;
31 minc-=temp_val;
32 }
33 }
34 }
35 }
36
37 int main()
38 {
39 int i,j;
40 char ss;
41 //freopen("test.in","r",stdin);
42 while(scanf("%d%d",&m,&n)!=EOF&&n+m)
43 {
44 getchar();
45 memset(map,0,sizeof(map));
46 minc=0;
47 res=0x3f3f3f;
48 for(i=1;i<=m;i++)
49 {
50 for(j=1;j<=n;j++)
51 {
52
53 scanf("%c",&ss);
54 if(ss==‘E‘)
55 map[i][j]=1;
56 else if(ss==‘B‘)
57 map[i][j]=2;
58 else if(ss==‘Y‘)
59 {
60 sx=i;
61 sy=j;
62 }
63 else if(ss==‘T‘)
64 {
65 map[i][j]=1;
66 ex=i;
67 ey=j;
68 }
69 else
70 map[i][j]=-0x3f3f3f3f; //代表障碍物
71 }
72 getchar();
73 }
74 //algorithm functiom
75 dfs(sx,sy);
76 printf("%d\n",res);
77 }
78 return 0;
79 }

上面的思路是tle了的,下面的是ac的,加了一个贪心的思想.

代码:时间为 4ms

 1 #include<iostream>
2 #include<cstring>
3 #include<cstdio>
4 #include<vector>
5 #include<cstdlib>
6 using namespace std;
7 const int maxn=302;
8 struct node
9 {
10 int val;
11 int minc;
12 };
13 node map[maxn][maxn];
14 //先采用深度搜索
15 int dir[4][2]=
16 {
17 {0,1}, //向右
18 {-1,0}, //向左
19 {1,0}, //向上
20 {0,-1} //向下
21 };
22 int m,n,sx,sy,ex,ey;
23 void dfs(int x,int y)
24 {
25 //剪枝
26 if(x>0&&y>0&&x<=m&&y<=n)
27 {
28 for(int i=0;i<4;i++)
29 {
30 if(map[x+dir[i][0]][y+dir[i][1]].val>0&&map[x+dir[i][0]][y+dir[i][1]].minc>(map[x][y].minc+map[x+dir[i][0]][y+dir[i][1]].val)) //非智能 shit
31 {
32 map[x+dir[i][0]][y+dir[i][1]].minc=(map[x][y].minc+map[x+dir[i][0]][y+dir[i][1]].val);
33 dfs(x+dir[i][0],y+dir[i][1]);
34 }
35 }
36 }
37 }
38
39 int main()
40 {
41 int i,j;
42 char ss;
43 // freopen("test.in","r",stdin);
44 while(scanf("%d%d",&m,&n)!=EOF&&n+m)
45 {
46 getchar();
47 memset(map,0,sizeof(map));
48 for(i=1;i<=m;i++)
49 {
50 for(j=1;j<=n;j++)
51 {
52 map[i][j].minc=0x3f3f3f3f;
53 scanf("%c",&ss);
54 if(ss==‘E‘)
55 map[i][j].val=1;
56 else if(ss==‘B‘)
57 map[i][j].val=2;
58 else if(ss==‘Y‘)
59 {
60 map[i][j].minc=0;
61 sx=i;
62 sy=j;
63 }
64 else if(ss==‘T‘)
65 {
66 map[i][j].val=1;
67 ex=i;
68 ey=j;
69 }
70 else
71 map[i][j].val=-1; //代表障碍物
72 }
73 getchar();
74 }
75 //algorithm functiom
76 dfs(sx,sy);
77 if(map[ex][ey].minc==0x3f3f3f3f)
78 printf("-1\n");
79 else
80 printf("%d\n",map[ex][ey].minc);
81 }
82 return 0;
83 }

当然还可以用bfs来做,时间会更快什么的........

nyoj-----284坦克大战(带权值的图搜索),布布扣,bubuko.com

时间: 2024-10-12 03:42:09

nyoj-----284坦克大战(带权值的图搜索)的相关文章

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

坦克大战 时间限制: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 co

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

这类带权的边的图,直接广搜不行,要加上优先队列,这样得到的结果才是最优的,这样每次先找权值最小的,代码如下 1 #include <stdio.h> 2 #include <iostream> 3 #include <queue> 4 #include <string.h> 5 using namespace std; 6 typedef struct Node{ 7 int x, y; 8 int step; 9 friend bool operator

nyoj 284 坦克大战【bfs】

坦克大战 时间限制: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 co

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

题目链接:http://acm.nyist.net/JudgeOnline/status.php?pid=284 特殊数据: 5 5 BBEEY EEERB SSERB SSERB SSETB 7 非优先队列: 1 2 #include<iostream> 3 #include<cstdio> 4 #include<algorithm> 5 #include<cstring> 6 #include<string> 7 #include<cm

NYOJ 284 坦克大战 &amp;&amp; POJ 2312 Battle City (广搜+优先队列)

链接:click here~~ 题意: 描述 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 space

HDU 1863:畅通工程(带权值的并查集)

畅通工程 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 16075    Accepted Submission(s): 6677 Problem Description 省政府"畅通工程"的目标是使全省任何两个村庄间都可以实现公路交通(但不一定有直接的公路相连,只要能间接通过公路可达即可).经过调查评估,得到的统计表中列出

hdu 4771 Stealing Harry Potter&#39;s Precious (2013亚洲区杭州现场赛)(搜索 bfs + dfs) 带权值的路径

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4771 题目意思:'@'  表示的是起点,'#' 表示的是障碍物不能通过,'.'  表示的是路能通过的: 目的:让你从 '@' 点出发,然后每个点只能走一次,求出最小的距离: 解题思路:先用 bfs 求解出任意两点之间的距离,用 ans[i][j],表示点 i 到点  j 的距离: 然后用 dfs 递归求出从起点经过所有点的距离中,比较出最小的: AC代码: 1 #include<iostream>

【20140809】VC++坦克大战带地图编辑器(游戏大赛一等奖)

VC++坦克大战,曾是游戏大赛一等奖作品,带音乐.带地图编辑器:游戏可以单人玩和双人玩.想信你在小时候玩过这游戏,很过瘾!地图编辑器是用VB写的,有了它你可以自己制造地图导入坦克游戏中.本游戏源码在VC6下顺利编译,需要注意,请将生成的文件放到根目录下也就是和声音资源及地图资源的目录是平级的,这样才能确保运行. 软件截图: 地图编辑器截图: 源码下载地址:点击下载 [20140809]VC++坦克大战带地图编辑器(游戏大赛一等奖)

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

畅通project Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 16075    Accepted Submission(s): 6677 Problem Description 省政府"畅通project"的目标是使全省不论什么两个村庄间都能够实现公路交通(但不一定有直接的公路相连,仅仅要能间接通过公路可达就可以).经过