• 跳马


    跳马

    时限:1000ms 内存限制:10000K  总时限:3000ms

    描述
    在国际象棋中,马的走法与中车象棋类似,即俗话说的“马走日”,下图所示即国际象棋中马(K)在一步能到达的格子(其中黑色的格子是能到达的位置)。

    现有一200*200大小的国际象棋棋盘,棋盘中仅有一个马,给定马的当前位置(S)和目标位置(T),求出马最少需要多少跳才能从当前位置到达目标位置。
     
    输入
    本题包含多个测例。输入数据的第一行有一个整数N(1<=N<=1000),表示测例的个数,接下来的每一行有四个以空格分隔的整数,分别表示马当前位置及目标位置的横、纵坐标C(x,y)和G(x,y)。坐标由1开始。
     
    输出
    对于每个测例,在单独的一行内输出一个整数,即马从当前位置跳到目标位置最少的跳数。
     
    输入样例
    2
    1 1 2 1
    1 5 5 1
     
    输出样例
    3
    4
    #include <iostream>
    #include <queue>
    
    using namespace std;
    
    typedef struct position
    {
        int posx;
        int posy;
    } position;
    
    int main()
    {
        int num,startx,starty,endx,endy;
        cin>>num;
        while(num>0)
        {
            int map[200][200];
    
            cin>>startx>>starty>>endx>>endy;
            queue<position> minequeue;
            position startplace;
            startplace.posx = startx-1;
            startplace.posy = starty-1;
            minequeue.push(startplace);
    
            for(int i = 0;i < 200; i++)
                for(int j = 0;j < 200; j++)
                    map[i][j] = -1;
            map[starty-1][startx-1] = 0;
            while(!minequeue.empty())
            {
                position temp = minequeue.front();
                minequeue.pop();
                int posx = temp.posx;
                int posy = temp.posy;
                if(posx==endx-1&&posy==endy-1)
                {
                    cout<<map[posy][posx]<<endl;
                    break;
                }
                if(posy-1>=0&&posx-2>=0&&map[posy-1][posx-2]==-1)
                {
                    position temp1;
                    temp1.posy = posy-1;
                    temp1.posx = posx-2;
                    minequeue.push(temp1);
                    map[posy-1][posx-2] = map[posy][posx]+1;
                    //cout<<map[posy-1][posx-2]<<" ";
                }
                if(posy-2>=0&&posx-1>=0&&map[posy-2][posx-1]==-1)
                {
                    position temp2;
                    temp2.posy = posy-2;
                    temp2.posx = posx-1;
                    minequeue.push(temp2);
                    map[posy-2][posx-1] = map[posy][posx]+1;
                    //cout<<map[posy-2][posx-1]<<" ";
                }
                if(posy-2>=0&&posx+1<200&&map[posy-2][posx+1]==-1)
                {
                    position temp3;
                    temp3.posy = posy-2;
                    temp3.posx = posx+1;
                    minequeue.push(temp3);
                    map[posy-2][posx+1] = map[posy][posx]+1;
                    //cout<<map[posy-2][posx+1]<<" ";
                }
                if(posy-1>=0&&posx+2<200&&map[posy-1][posx+2]==-1)
                {
                    position temp4;
                    temp4.posy = posy-1;
                    temp4.posx = posx+2;
                    minequeue.push(temp4);
                    map[posy-1][posx+2] = map[posy][posx]+1;
                    //cout<<map[posy-1][posx+2]<<" ";
                }
                if(posy+2<200&&posx+1<200&&map[posy+2][posx+1]==-1)
                {
                    position temp5;
                    temp5.posy = posy+2;
                    temp5.posx = posx+1;
                    minequeue.push(temp5);
                    map[posy+2][posx+1] = map[posy][posx]+1;
                    //cout<<map[posy+2][posx+1]<<" ";
                }
                if(posy+1<200&&posx+2<200&&map[posy+1][posx+2]==-1)
                {
                    position temp6;
                    temp6.posy = posy+1;
                    temp6.posx = posx+2;
                    minequeue.push(temp6);
                    map[posy+1][posx+2] = map[posy][posx]+1;
                    //cout<<map[posy+1][posx+2]<<" ";
                }
                if(posy+1<200&&posx-2>=0&&map[posy+1][posx-2]==-1)
                {
                    position temp7;
                    temp7.posy = posy+1;
                    temp7.posx = posx-2;
                    minequeue.push(temp7);
                    map[posy+1][posx-2] = map[posy][posx]+1;
                    //cout<<map[posy+1][posx-2]<<" ";
                }
                if(posy+2<200&&posx-1>=0&&map[posy+2][posx-1]==-1)
                {
                    position temp8;
                    temp8.posy = posy+2;
                    temp8.posx = posx-1;
                    minequeue.push(temp8);
                    map[posy+2][posx-1] = map[posy][posx]+1;
                    //cout<<map[posy+2][posx-1]<<" ";
                }
            }
            num--;
        }
    }
    

      

    态度决定高度,细节决定成败,
  • 相关阅读:
    C#——数组
    javaScript数组移除指定对象或下标i,数组去重
    css实现左边div自适应宽度,右边宽度适应左边
    最短JS判断是否为IE6(!-[1,]&&!window.XMLHttpRequest)(转)
    java 获取微信 页面授权 获取用户openid
    鼠标滑轮滚动事件
    浏览器后退(返回)事件捕获
    一些常用的原生js方法(函数)
    简单瀑布流实现
    XMLHttpRequest对象的创建与用法
  • 原文地址:https://www.cnblogs.com/lxk2010012997/p/4428911.html
Copyright © 2020-2023  润新知