Sicily 1936. Knight Moves

本文探讨了国际象棋中骑士的走法及其与马的相似性,并通过实例展示了如何利用宽度优先搜索(宽搜)算法解决从起点到终点的问题。

开始时看不明题目,后来才知道原来说的是国际象棋中的骑士,其走法与中国象棋中的马相似,但没有绊马脚这一说法。那么从起点到终点,可以运用宽搜来解决。

Run Time: 0.05sec

Run Memory: 312KB

Code length: 3045Bytes

SubmitTime: 2011-12-24 20:17:55

// Problem#: 1936
// Submission#: 1123094
// The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License
// URI: http://creativecommons.org/licenses/by-nc-sa/3.0/
// All Copyright reserved by Informatic Lab of Sun Yat-sen University
#include <iostream>
#include <queue>
#include <string>
#include <cstring>
using namespace std;

struct Coor {
    int x;
    int y;
    Coor( int i, int j ) { x = i; y = j; }
};

int main()
{
    int T;
    int count, step;;
    string start, end;
    int sx, sy, ex, ey;
    bool visited[ 8 ][ 8 ];

    cin >> T;
    while ( T-- ) {
        cin >> start >> end;
        sx = start[ 1 ] - '1';
        sy = start[ 0 ] - 'a';
        ex = end[ 1 ] - '1';
        ey = end[ 0 ] - 'a';

        memset( visited, false, sizeof( visited ) );
        queue<Coor> q;
        Coor temp( sx, sy );
        q.push( temp );
        visited[ sx ][ sy ] = true;
        step = 0;
        while ( !visited[ ex ][ ey ] ) {
            count = q.size();
            step++;
            while ( !visited[ ex ][ ey ] && count-- ) {
                temp = q.front();
                if ( temp.x - 2 >= 0 && temp.y - 1 >= 0 && !visited[ temp.x - 2 ][ temp.y - 1 ] ) {
                    q.push( Coor( temp.x - 2, temp.y - 1 ) );
                    visited[ temp.x - 2 ][ temp.y - 1 ] = true;
                }
                if ( temp.x - 2 >= 0 && temp.y + 1 < 8 && !visited[ temp.x - 2 ][ temp.y + 1 ] ) {
                    q.push( Coor( temp.x - 2, temp.y + 1 ) );
                    visited[ temp.x - 2 ][ temp.y + 1 ] = true;
                }
                if ( temp.x - 1 >= 0 && temp.y - 2 >= 0 && !visited[ temp.x - 1 ][ temp.y - 2 ] ) {
                    q.push( Coor( temp.x - 1, temp.y - 2 ) );
                    visited[ temp.x - 1 ][ temp.y - 2 ] = true;
                }
                if ( temp.x - 1 >= 0 && temp.y + 2 < 8 && !visited[ temp.x - 1 ][ temp.y + 2 ] ) {
                    q.push( Coor( temp.x - 1, temp.y + 2 ) );
                    visited[ temp.x - 1 ][ temp.y + 2 ] = true;
                }
                if ( temp.x + 1 < 8 && temp.y - 2 >= 0 && !visited[ temp.x + 1 ][ temp.y - 2 ] ) {
                    q.push( Coor( temp.x + 1, temp.y - 2 ) );
                    visited[ temp.x + 1 ][ temp.y - 2 ] = true;
                }
                if ( temp.x + 1 < 8 && temp.y + 2 < 8 && !visited[ temp.x + 1 ][ temp.y + 2 ] ) {
                    q.push( Coor( temp.x + 1, temp.y + 2 ) );
                    visited[ temp.x + 1 ][ temp.y + 2 ] = true;
                }
                if ( temp.x + 2 < 8 && temp.y - 1 >= 0 && !visited[ temp.x + 2 ][ temp.y - 1 ] ) {
                    q.push( Coor( temp.x + 2, temp.y - 1 ) );
                    visited[ temp.x + 2 ][ temp.y - 1 ] = true;
                }
                if ( temp.x + 2 < 8 && temp.y + 1 < 8 && !visited[ temp.x + 2 ][ temp.y + 1 ] ) {
                    q.push( Coor( temp.x + 2, temp.y + 1 ) );
                    visited[ temp.x + 2 ][ temp.y + 1 ] = true;
                }
                q.pop();
            }
        }
        cout << "To get from " << start << " to " << end << " takes " << step << " knight moves.\n";
    }

    return 0;

}                                 


 

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值