牛客——The Chivalrous Cow(bfs板题)

The Chivalrous Cow

时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 32768K,其他语言65536K
64bit IO Format: %lld
题目描述
Farmer John traded one of his cows for a cow that Farmer Don called ‘The Knight’. This cow has the unique ability to jump around the pasture in moves that looked like a knight on a chessboard (two squares over, one up… or maybe two squares down and one over, etc.). ‘The Knight’ can’t jump on rocks or trees, but can really make her way around the pasture, which is partitioned for our purposes into an X by Y set of squares (1 <= X <= 150; 1 <= Y <= 150).
‘The Knight’ likes hay just like any other cow. Given a map of 'The Knight’s starting place, locations of the tree, shrub, rock, and other obstacles, and the location of a bale of hay, determine how many ‘hops’ the Knight must make in order to get to the hay. The Knight cow will be marked by a ‘K’ on the map; obstacles by ‘*’, and the haybale by ‘H’. Here’s a typical map:
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 could travel the path indicated by A, B, C, … to get to the hay bale in just 5 moves (other routes of length 5 might be possible):
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

Hint: This problem is probably most easily solved with a simple first-in/first-out ‘queue’ data structure implemented as a few parallel arrays.
输入描述:

  • Line 1: Two space-separated integers: X and Y

  • Lines 2…Y+1: Line Y-i+2 contains X characters with no spaces (i.e., the map is read in as shown in the text above): map row i
    输出描述:

  • Line 1: A single integer that is the minimum number of hops to get to the hay bale. It is always possible to get to the haybale.
    示例1
    输入

10 11



.

…H


.K…
…*

输出

5

ps:bfs经典模板题,敲一个模板再熟悉一下

#include<bits/stdc++.h>
using namespace std;
char a[200][200];
int dx[8]= {1,2,2, 1,-1,-2,-2,-1};//方向数组
int dy[8]= {2,1,-1,-2,-2,-1,1,2};
int dis[200][200];//标记数组
int n,m;
pair<int,int>st,ed;//起点与终点
queue<pair<int,int>>q;

bool check(int x,int y)//检查是否符合条件
{
    return x>=1 && x<=n && y>=1 && y<=m && a[x][y]!='*' && dis[x][y]==-1;//在范围内;且不是障碍物;第一次被访问
}

void st_ed()//找起点和终点
{
    for(int i=1;i<=n;i++)
    for(int j=1;j<=m;j++)
    {
        if (a[i][j]=='K')
        {
            st.first=i;
            st.second=j;
        }
        if (a[i][j]=='H')
        {
            ed.first=i;
            ed.second=j;
        }
    }
}

int bfs()
{
    memset(dis,-1,sizeof(dis));
    q.push(make_pair(st.first,st.second));//压入起点
    dis[st.first][st.second]=0;
    while(q.size())
    {
        pair<int,int> now=q.front();
        q.pop();
        for(int i=0;i<8;i++)
        {
            int x=now.first+dx[i],y=now.second+dy[i];//拓展
            if (check(x,y))//满足条件
            {
                dis[x][y]=dis[now.first][now.second]+1;
                q.push(make_pair(x,y));
                if (x==ed.first && y==ed.second)//到达终点了
                    return dis[x][y];
            }
        }
    }
    return -1;//然而并没有无解情况
}


int main(){  
    cin>>m>>n;
    for(int i=1;i<=n;i++)
            cin>>a[i]+1;
    st_ed();
    cout<<bfs()<<endl;
    /*for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++)
            cout<<dis[i][j]<<" ";
        cout<<endl;
    }*/        
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值