IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 数据结构与算法 -> Warnsdorff‘s algorithm 完成骑兵游行(Knight tour)问题 -> 正文阅读

[数据结构与算法]Warnsdorff‘s algorithm 完成骑兵游行(Knight tour)问题

问题描述:

在一个8x8(或者nxn)的棋盘上,一个骑兵(马)走日(对角)能否遍历整个棋盘。

http://en.wikipedia.org/wiki/Knight%27s_tour

Warnsdorff's algorithm: Heruistic?剪枝,排除不需要的回溯路

规则如下两条:

1)我们可以从棋盘上任意一处开始移动

2)我们每次移动到最近,最狭隘(周围没遍历过点最少)的点(be more greedy!)

算法的基本结构:

1.?任意选取点P为棋盘上起点

2.?标记P为1

3.?重复以下动作从2-64:

? ? ? ? 1. S?作为P能移动到的点的set

? ? ? ? 2.?标记P?为S里最小accesibility

? ? ? ? 3.?标记P?为现在的移动次数

4.?返回标记的数组

import java.util.*;
import java.util.concurrent.ThreadLocalRandom;

public class knight_tour {
	
	public static final int N = 8;
	
    //cx,cy记录可以移动的位置 8 种
	public static final int cx[] = {1, 1, 2, 2, -1, -1, -2, -2};
    public static final int cy[] = {2, -2, 1, -1, 2, -2, 1, -1};
	
    class Cell {
        int x;
        int y;
     
        public Cell(int x, int y)
        {
            this.x = x;
            this.y = y;
        }
    }
    
    //判断是否在棋盘内
    boolean limits(int x, int y) {
        return ((x >= 0 && y >= 0) && (x < N && y < N));
    }
    
    //判断是否能走
    boolean isempty(int a[], int x, int y){
        return (limits(x, y)) && (a[y * N + x] < 0);
    }
    
    //找到每个点的degree 方便选择剪枝
    int getDegree(int a[], int x, int y) {
        int count = 0;
        for (int i = 0; i < N; ++i)
            if (isempty(a, (x + cx[i]),(y + cy[i])))
                count++;
        return count;
    }
    
    // 选择下一个位置
    Cell nextMove(int a[], Cell cell)
    {
        int min_deg_idx = -1, c,
            min_deg = (N + 1), nx, ny;
 
        //随机选一个开始点
        int start = ThreadLocalRandom.current().nextInt(1000) % N;
        for (int count = 0; count < N; ++count) {
            int i = (start + count) % N;
            nx = cell.x + cx[i];
            ny = cell.y + cy[i];
            if ((isempty(a, nx, ny)) && (c = getDegree(a, nx, ny)) < min_deg) {
                min_deg_idx = i;
                min_deg = c;
            }
        }
 
        if (min_deg_idx == -1)
            return null;
 
        nx = cell.x + cx[min_deg_idx];
        ny = cell.y + cy[min_deg_idx];
 
        a[ny * N + nx] = a[(cell.y) * N + (cell.x)] + 1;
        cell.x = nx;
        cell.y = ny;
 
        return cell;
    }
    
    void print(int a[]) {
        for (int i = 0; i < N; ++i) {
            for (int j = 0; j < N; ++j)
                System.out.printf("%d\t", a[j * N + i]);
            System.out.printf("\n");
        }
    }
    
    boolean neighbour(int x, int y, int xx, int yy) {
        for (int i = 0; i < N; ++i)
            if (((x + cx[i]) == xx) &&((y + cy[i]) == yy))
                return true;
        return false;
    }
    
    boolean findClosedTour() {
        int a[] = new int[N * N];
        for (int i = 0; i < N * N; ++i)
            a[i] = -1;
 
        int sx = 3;
        int sy = 2;

        Cell cell = new Cell(sx, sy);
 
        a[cell.y * N + cell.x] = 1;
        Cell ret = null;
        for (int i = 0; i < N * N - 1; ++i)
        {
            ret = nextMove(a, cell);
            if (ret == null)
                return false;
        }
        if (!neighbour(ret.x, ret.y, sx, sy))
            return false;
 
        print(a);
        return true;
    }
    
    public static void main(String[] args)
    {
        while (!new knight_tour().findClosedTour())
        {
            ;
        }
    }
}

  数据结构与算法 最新文章
【力扣106】 从中序与后续遍历序列构造二叉
leetcode 322 零钱兑换
哈希的应用:海量数据处理
动态规划|最短Hamilton路径
华为机试_HJ41 称砝码【中等】【menset】【
【C与数据结构】——寒假提高每日练习Day1
基础算法——堆排序
2023王道数据结构线性表--单链表课后习题部
LeetCode 之 反转链表的一部分
【题解】lintcode必刷50题<有效的括号序列
上一篇文章      下一篇文章      查看所有文章
加:2021-07-27 16:29:50  更:2021-07-27 16:31:20 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/8 9:26:05-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码