首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >nyoj-----284坦克大战(带权值的图搜索)

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

作者头像
Gxjun
发布2018-03-22 13:25:28
发布2018-03-22 13:25:28
9770
举报
文章被收录于专栏:mlml

坦克大战

时间限制: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.样例输入

代码语言:javascript
复制
3 4
YBEB
EERE
SSTE
0 0

样例输出

代码语言:javascript
复制
8

来源POJ上传者sadsad

  较为水的一题。只需要转变为权值,然后就搜索即可,当然这道题,若果采用盲深搜索的话,会tie

贴一下tle代码,记录做题的思路吧!

代码:

代码语言:javascript
复制
 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

代码语言:javascript
复制
 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来做,时间会更快什么的........

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2014-06-11 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 坦克大战
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档