zoukankan      html  css  js  c++  java
  • 【例8.3】最少步数

    【例8.3】最少步数

    链接:http://ybt.ssoier.cn:8088/problem_show.php?pid=1330
    时间限制: 1000 ms         内存限制: 65536 KB

    【题目描述】

    在各种棋中,棋子的走法总是一定的,如中国象棋中马走“日”。有一位小学生就想如果马能有两种走法将增加其趣味性,因此,他规定马既能按“日”走,也能如象一样走“田”字。他的同桌平时喜欢下围棋,知道这件事后觉得很有趣,就想试一试,在一个(100*100)的围棋盘上任选两点A、B,A点放上黑子,B点放上白子,代表两匹马。棋子可以按“日”字走,也可以按“田”字走,俩人一个走黑马,一个走白马。谁用最少的步数走到左上角坐标为(1,1)的点时,谁获胜。现在他请你帮忙,给你A、B两点的坐标,想知道两个位置到(1,1)点可能的最少步数。

    【输入】

    A、B两点的坐标。

    【输出】

    最少步数。

    【输入样例】

    12 16
    18 10
    

    【输出样例】

    8
    9
    #include<iostream>
    #include<cstring>
    #include<cstdio>
    #include<queue>
    using namespace std;
    int zl[12][2]={{1,2},{1,-2},{-1,2},{-1,-2},{-2,-1},{-2,1},{2,1},{2,-1},{2,2},{-2,-2},{2,-2},{-2,2}};
    int Ax,Ay,Bx,By;
    bool mp[105][105];
    struct node{
        int x,y,step;
        node(){}
        node(const int x,const int y,const int step):x(x),y(y),step(step){}
    };
    
    int bfs(int u,int v)
    {
        queue <node>Q;
        Q.push(node(u,v,0));
        mp[u][v]=1;
        while(!Q.empty())
        {
            node nw=Q.front();
            Q.pop();
            for(int i=0;i<12;i++)
            {
                int x=nw.x+zl[i][0],y=nw.y+zl[i][1];
                if(x>0&&x<=100&&y>0&&y<=100&&!mp[x][y])
                {
                    Q.push(node(x,y,nw.step+1));
                    mp[x][y]=1;
                    if(x==1&&y==1)return nw.step+1;
                }
            }
        }
        return -1;
    }
    int main()
    {
        cin>>Ax>>Ay>>Bx>>By;
        cout<<bfs(Ax,Ay)<<endl;
        memset(mp,0,sizeof(mp));
        cout<<bfs(Bx,By)<<endl;
    }
  • 相关阅读:
    去掉滚动条
    一些input用法
    jquery-ui datepicker
    js修改样式
    js时间
    跳转到页面的某个anchor
    事件传递
    flex对象.show()的时候display变成block
    html中传递信息
    button disable and enable
  • 原文地址:https://www.cnblogs.com/EdSheeran/p/8017868.html
Copyright © 2011-2022 走看看