Algorithm --> 求出A到B的最小步数

求出A到B的最小步数

给定象棋盘,以及位置A和B, 求出从A到B的最小步数

Input
2             -->case 个数
9 9          -->棋盘大小
3 5 2 8    --> 起始位置
20 20
2 3 7 9

Output
2

5

代码:

#include <cstdio>
#include <iostream>
#include <string.h>

using namespace std;
#define MAX 1000

//八个方向
int o[8][2] = { { 1, 2 }, { 1, -2 }, { -1, 2 }, { -1, -2 }, { -2, -1 }, { -2, 1 }, { 2, -1 }, { 2, 1 } };  

int R, C;
int _sX, _sY, _eX, _eY;  //起始坐标
int graph[MAX][MAX];
int Answer;

void DFS( int sX, int sY, int cnt )
{
    if( sX < 1 || sX > R || sY < 1 || sY > C )
    {
        return;
    }

    if( sX == _eX && sY == _eY )
    {
        if( Answer > cnt )     //求取最小步数 Answer
        {
            Answer = cnt;
        }
    }

    if( graph[sX][sY] == 0 || graph[sX][sY] > cnt )  //当前格子没走或者值比cnt大,取较小值
    {
        graph[sX][sY] = cnt;
        for( int i = 0; i < 8; i++ )
        {
            DFS( sX + o[i][0], sY + o[i][1], cnt + 1 );
        }
    }
}

int main( int argc, char** argv )
{
    int tc, T;

    freopen( "input_chess.txt", "r", stdin );

    cin >> T;
    for( tc = 0; tc < T; tc++ )
    {
        memset( graph, 0, sizeof( graph ) );

        cin >> R >> C;
        cin >> _sX >> _sY >> _eX >> _eY;

        graph[_sX][_sY] = 1;

        Answer = MAX;

        DFS( _sX, _sY, 0 );

        cout << Answer << endl;
    }

    return 0;
}

输入文件:

5
10 10
1 1 10 10
20 20
2 3 7 9
30 30
2 15 29 29
40 40
2 3 1 40
45 45
40 10 27 40
时间: 2024-07-30 00:06:58

Algorithm --> 求出A到B的最小步数的相关文章

(hdu step 4.2.4)A strange lift(求从起点到终点的最小步数,限制条件是:在一维的情况下)

题目: A strange lift Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 709 Accepted Submission(s): 348   Problem Description There is a strange lift.The lift can stop can at every floor as you want, a

c编程:求出4&amp;#215;4矩阵中最大和最小元素值及其所在行下标和列下标,求出两条主对角线元素之和。

//求出4×4矩阵中最大和最小元素值及其所在行下标和列下标,求出两条主对角线元素之和 #include <stdio.h> int main() { int sum=0; int max,min; int max1,max2;//记录最大值的坐标 int min1,min2;//记录最小值的坐标 int i,j; int a[4][4]; //为数组赋值 for(i=0;i<4;i++) { for(j=0;j<4;j++) { scanf("%d",&

c编程:求出4&#215;4矩阵中最大和最小元素值及其所在行下标和列下标,求出两条主对角线元素之和。

//求出4×4矩阵中最大和最小元素值及其所在行下标和列下标,求出两条主对角线元素之和 #include <stdio.h> int main() { int sum=0; int max,min; int max1,max2;//记录最大值的坐标 int min1,min2;//记录最小值的坐标 int i,j; int a[4][4]; //为数组赋值 for(i=0;i<4;i++) { for(j=0;j<4;j++) { scanf("%d",&

hdu1085(求出最小不连续的值)

题目意思: 给出面值为1.2.5的个数n1,n2,n3求出最小不连续的值 http://acm.hdu.edu.cn/showproblem.php?pid=1085 题目分析: 方法一.多重背包问题的变形,状态转化方程为 if(a[i]==1) a[i+j*v]=1; (0<j<num[i],v为面值的值,v表示个数) 方法二.由于数据较小直接暴力 对于每一个值,从0到n模拟所有价值,并记录这个值,可以满足 AC代码: /** *1.类似背包问题 *if(a[i]==1) a[i+j*v]=

hdoj 2435 There is a war 【求原图最小割已经分成的两个点集 + 枚举两点集里面的点建新边 求残量网络的最大最小割】

There is a war Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 993    Accepted Submission(s): 283 Problem Description There is a sea. There are N islands in the sea. There are some directional

给定入栈序列,求出合法的出栈序列的个数

思想:1.利用全排列函数next_permutation()求出所有可能的序列 2.从中选出所有正确的序列 #include <iostream> #include <stack> #include <vector> #include <algorithm> using namespace std; //判断序列是否是合法的出栈序列 bool IsPopOrder(const int* pPush,const int* pPop,int n) { if (p

求出所有的正整数对 使他们最大公约数为n,最小公倍数为m

题目大概是这样的:点击打开链接 大意就是 求出所有的正整数对 使他们最大公约数为n,最小公倍数为m.(1 <= n, m <= 10^10) 可以将问题转化为 : 设a,b就是那个整数对,n, a, b, m, 这4个数都是可以被n整除的,可以都除以n, 题目转化为求出 最大公约数为1, 最小公倍数为m/n的对数 . 也就是求出在1到m/n里 乘积为m/n且互质的对数.可以在O(sqrt (m/n) )内解决. #include <algorithm> #include <c

One Person Game(扩展欧几里德求最小步数)

One Person Game Time Limit: 2 Seconds      Memory Limit: 65536 KB There is an interesting and simple one person game. Suppose there is a number axis under your feet. You are at point A at first and your aim is point B. There are 6 kinds of operations

(hdu step 4.2.3)Knight Moves(求从起点是否能够到达终点的最小步数)

题目: Knight Moves Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 453 Accepted Submission(s): 326   Problem Description A friend of you is doing research on the Traveling Knight Problem (TKP) where