• TYVJ 1074 武士风度的牛(BFS)


    背景 Background
    农民John有很多牛,他想交易其中一头被Don称为The Knight的牛。这头牛有一个独一无二的超能力,在农场里像Knight一样地跳(就是我们熟悉的象棋中马的走法)。虽然这头神奇的牛不能跳到树上和石头上,但是它可以在牧场上随意跳,我们把牧场用一个x,y的坐标图来表示。
     
    描述 Description
    这头神奇的牛像其它牛一样喜欢吃草,给你一张地图,上面标注了The Knight的开始位置,树、灌木、石头以及其它障碍的位置,除此之外还有一捆草。现在你的任务是,确定The Knight要想吃到草,至少需要跳多少次。The Knight的位置用'K'来标记,障碍的位置用'*'来标记,草的位置用'H'来标记。

    这里有一个地图的例子:
                 11 | . . . . . . . . . .
                 10 | . . . . * . . . . . 
                  9 | . . . . . . . . . . 
                  8 | . . . * . * . . . . 
                  7 | . . . . . . . * . . 
                  6 | . . * . . * . . . H 
                  5 | * . . . . . . . . . 
                  4 | . . . * . . . * . . 
                  3 | . K . . . . . . . . 
                  2 | . . . * . . . . . * 
                  1 | . . * . . . . * . . 
                  0 ----------------------
                                        1 
                    0 1 2 3 4 5 6 7 8 9 0 

    The Knight 可以按照下图中的A,B,C,D...这条路径用5次跳到草的地方(有可能其它路线的长度也是5):
                 11 | . . . . . . . . . .
                 10 | . . . . * . . . . .
                  9 | . . . . . . . . . .
                  8 | . . . * . * . . . .
                  7 | . . . . . . . * . .
                  6 | . . * . . * . . . F<
                  5 | * . B . . . . . . .
                  4 | . . . * C . . * E .
                  3 | .>A . . . . D . . .
                  2 | . . . * . . . . . *
                  1 | . . * . . . . * . .
                  0 ----------------------
                                        1
                    0 1 2 3 4 5 6 7 8 9 0
     
    输入格式 InputFormat
    第一行: 两个数,表示农场的列数(<=150)和行数(<=150)

    第二行..结尾: 如题目描述的图。
     
    输出格式 OutputFormat
    一个数,表示跳跃的最小次数。
     
    样例输入 SampleInput 

    10 11
    ..........
    ....*.....
    ..........
    ...*.*....
    .......*..
    ..*..*...H
    *.........
    ...*...*..
    .K........
    ...*.....*
    ..*....*..

    样例输出 SampleOutput

    5

     
    数据范围和注释 Hint
    Hint:这类问题可以用一个简单的先进先出表(队列)来解决。
     
     
    BFS水题,但是要看清楚题目注意是先输入列再输入行
     1 #include<cstdio>
     2 #include<queue>
     3 #include<cstring>
     4 #include<stdlib.h>
     5 #include<algorithm>
     6 using namespace std;
     7 char a[151][151];
     8 int vis[151][151];
     9 int dir[8][2]={{-1,-2},{1,-2},{-2,-1},{2,-1},{-2,1},{-1,2},{2,1},{1,2}};
    10 int ans,n,m;
    11 struct node
    12 {
    13     int x,y;
    14     int step;
    15 }star;
    16 void BFS()
    17 {
    18     memset(vis,0,sizeof(vis));
    19     vis[star.x][star.y]=1;
    20     ans=0;
    21     queue<node> q;
    22     q.push(star);
    23     while(!q.empty())
    24     {
    25         node now;
    26         now=q.front();
    27         q.pop();
    28         if(a[now.x][now.y]=='H')
    29         {
    30             ans=now.step;
    31             return ;
    32         }
    33         for(int i=0;i<8;i++)
    34         {
    35             node next;
    36             next.x=now.x+dir[i][0];
    37             next.y=now.y+dir[i][1];
    38             next.step=now.step+1;
    39             if(0<=next.x&&next.x<n&&0<=next.y&&next.y<m&&a[next.x][next.y]!='*'&&!vis[next.x][next.y])
    40             {
    41                 vis[next.x][next.y]=1;
    42                 q.push(next);
    43             }
    44         }
    45     }
    46 }
    47 int main()
    48 {
    49     scanf("%d %d",&m,&n);
    50     for(int i=0;i<n;i++)
    51         scanf("%s",a[i]);
    52     for(int i=0;i<n;i++)
    53         for(int j=0;j<m;j++)
    54         {
    55             if(a[i][j]=='K')
    56             {
    57                 star.x=i;
    58                 star.y=j;
    59                 star.step=0;
    60             }
    61         }
    62     BFS();
    63     printf("%d
    ",ans);
    64     return 0;
    65 }
    View Code
  • 相关阅读:
    大数据第59天—MySQL之员工奖金-杨大伟
    大数据第58天—MySQL常用命令-杨大伟
    大数据第56天—Mysql练习题12道之十一-查出销售表中所有会员购买金额,同时分组查出退货表中所有会员的退货金额-杨大伟
    大数据第55天—Mysql练习题12道之十-查询各自区组的money排名前十的账号-杨大伟
    大数据第54天—Mysql练习题12道之九-充值日志表-杨大伟
    大数据第53天—Mysql练习题12道之八-线上服务器访问日志-杨大伟
    大数据第52天—Mysql练习题12道之七-图书管理数据库-杨大伟
    mac 破解pycharm2020.2.3
    mac连接oracle数据库
    python DPI-1047:Cannot locate a 64-bit Oracle Client library:The specified module could not be found.
  • 原文地址:https://www.cnblogs.com/clliff/p/3888782.html
Copyright © 2020-2023  润新知