uva 439 Knight Moves(马移动) —— DFS + 剪枝

/**
序号:num_6
作者:MrZhang
日期:2016-5-24


题目名称: Knight Moves(马移动)
题目来源:
uva —— 439 —— Knight Moves
网址:
英文题目:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=19436
--------------------------------------
数据样例:
input:

e2 e4
a1 b2
b2 c3
a1 h8
a1 h7
h8 a1
b1 c3
f6 f6

output

To get from e2 to e4 takes 2 knight moves.
To get from a1 to b2 takes 4 knight moves.
To get from b2 to c3 takes 2 knight moves.
To get from a1 to h8 takes 6 knight moves.
To get from a1 to h7 takes 5 knight moves.
To get from h8 to a1 takes 6 knight moves.
To get from b1 to c3 takes 1 knight moves.
To get from f6 to f6 takes 0 knight moves.
*/
#include 
#include 
#include 

using namespace std;
int start_x,start_y;
int aim_x,aim_y;

int INF = 10;
int minLength;
int nowLength;

bool check(int u,int v){
    if(u >= 0 && u <= 7 && v >= 0 && v <= 7) {
        return true;
    }

    return false;
}
//!自上至下第i行
//!           1       2      3     4
int dx[10]={-2,-2,  -1,-1,  1,1,  2,2};
int dy[10]={-1, 1,  -2, 2, -2,2, -1,1};

void dfs(int x,int y){
    if(x == aim_x && y == aim_y){
        if(nowLength < minLength)
        {
            minLength = nowLength;
            return ;
        }


    }

    for(int i=0;i<8;i++){
        int u = x + dx[i], v = y +dy[i];

        if(check(u,v) && nowLength < minLength){
            nowLength ++;
            dfs(u,v);
            nowLength --;
        }
    }

}

int main(){
    char input[10];
    while(gets(input)){
        minLength = INF;
        nowLength = 0;

        start_x = input[0] - 'a';
        start_y = input[1] - '1';

        aim_x = input[3] - 'a';
        aim_y = input[4] - '1';

        dfs(start_x,start_y);

        cout<<"To get from "<= 0 && u <= 7 && v >= 0 && v <= 7
(2)有没有必要遍历?
若nowLength > minLength,就没有必要再遍历了。(此即所谓的剪枝)
*/

你可能感兴趣的:(acm,uva,439,Knight,Moves,DFS,移动)