439 - Knight Moves (UVA)

题目链接如下:

Online Judge

UVA439 骑士的移动 - 锦依卫议事堂 - 洛谷博客 这里有好几个特别厉害的解法...先存着慢慢看。

我的代码如下:

cpp 复制代码
#include <iostream>
#include <deque>
#include <string>
// #define debug

struct node{
    std::string loc;
    int step;
};
std::string s1, s2;
int dx[] = {2, 1, -1, -2, -2, -1, 1, 2};
int dy[] = {1, 2, 2, 1, -1, -2, -2, -1};

int calStep(){
    if (s1 == s2){
        return 0;
    }
    node temp;
    temp.loc = s1;
    temp.step = 0;
    std::deque<node> dq;
    dq.push_back(temp);
    while (!dq.empty()){
        node curr = dq.front();
        dq.pop_front();
        for (int u = 0; u < 8; ++u){
            node tmp;
            tmp.loc.push_back(curr.loc[0] + dx[u]);
            tmp.loc.push_back(curr.loc[1] + dy[u]);
            if (tmp.loc == s2){
                return curr.step + 1;
            }
            if (tmp.loc[0] >= 'a' && tmp.loc[0] <= 'h' && tmp.loc[1] >= '1' && tmp.loc[1] <= '8'){
                tmp.step = curr.step + 1;
                dq.push_back(tmp);
            }
        }
    }
}

int main(){
    #ifdef debug
    freopen("0.txt", "r", stdin);
    freopen("1.txt", "w", stdout);
    #endif
    while (std::cin >> s1 >> s2){
        printf("To get from %s to %s takes %d knight moves.\n", s1.c_str(), s2.c_str(), calStep());
    }
    #ifdef debug
    fclose(stdin);
    fclose(stdout);
    #endif
    return 0;
}
相关推荐
棱镜Coding4 分钟前
LeetCode-Hot100 30.两两交换链表中的节点
算法·leetcode·链表
2301_7903009610 分钟前
C++与量子计算模拟
开发语言·c++·算法
汽车仪器仪表相关领域19 分钟前
经典指针+瞬态追踪:MTX-A模拟废气温度(EGT)计 改装/赛车/柴油车排气温度监测实战全解
大数据·功能测试·算法·机器学习·可用性测试
如果你想拥有什么先让自己配得上拥有27 分钟前
斐波那契黄金分割自然界演化以及金融上的共振?
算法·金融
灰色小旋风29 分钟前
力扣第1题:两数之和(C++)
c++·算法
民乐团扒谱机31 分钟前
机器学习 第二弹 和AI斗智斗勇 机器学习核心知识点全解析(GBDT/XGBoost/LightGBM/随机森林+调参方法)
算法·决策树·机器学习
智驱力人工智能34 分钟前
实线变道检测 高架道路安全治理的工程化实践 隧道压实线监测方案 城市快速路压实线实时预警 压实线与车牌识别联动方案
人工智能·opencv·算法·安全·yolo·边缘计算
We་ct38 分钟前
LeetCode 3. 无重复字符的最长子串:滑动窗口最优解演进与解析
前端·算法·leetcode·typescript
沉默-_-38 分钟前
备战蓝桥杯--栈
数据结构·算法·力扣·
weixin1997010801639 分钟前
B2Bitem_get - 获取商标详情接口对接全攻略:从入门到精通
java·大数据·算法