跳马

跳马

时限: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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#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--;
    }
}

  

posted @   再见,少年  Views(270)  Comments(0Edit  收藏  举报
编辑推荐:
· 智能桌面机器人:用.NET IoT库控制舵机并多方法播放表情
· Linux glibc自带哈希表的用例及性能测试
· 深入理解 Mybatis 分库分表执行原理
· 如何打造一个高并发系统?
· .NET Core GC压缩(compact_phase)底层原理浅谈
阅读排行:
· DeepSeek火爆全网,官网宕机?本地部署一个随便玩「LLM探索」
· 开发者新选择:用DeepSeek实现Cursor级智能编程的免费方案
· 【译】.NET 升级助手现在支持升级到集中式包管理
· 独立开发经验谈:如何通过 Docker 让潜在客户快速体验你的系统
· Tinyfox 发生重大改版
历史上的今天:
2013-04-15 堆排序算法
2013-04-15 排序算法
2013-04-15 贪心算法-装载问题
点击右上角即可分享
微信分享提示