骑士走棋盘(c/python)

骑士走棋盘:等价于中国象棋中马走日
《骑士走棋盘(c/python)》

算法思路:骑士所要走的下一步:为下一步再做选择时,选择能走的步数最少的一步。使用这个方法,在不使用递归的情况下,可以有较高的几率找出走法(有时可能也找不到走法)。

C代码

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>

int traval(int x, int y);
int board[8][8] = { 0 };
int main()
{
    int startX, startY;//起点
    int i, j; 
    printf("please input startpoint:");
    scanf("%d%d", &startX, &startY);

    if (traval(startX, startY))
    {
        printf("success\n");
        for (i = 0; i < 8; i++)
        {
            for (j = 0; j < 8; j++)
            {
                printf("%3d", board[i][j]);
            }
            printf("\n");
        }
    }
    else
        printf("fail");
    system("pause");
}

int traval(int x, int y)
{
    int move_x[8] = { -2, -1, 1, 2, 2, 1, -1, -2 }; //可移动的八个方向
    int move_y[8] = { 1, 2, 2, 1, -1, -2, -2, -1 };

    int next_x[8] = { 0 };  //存储下一步的可能的位置
    int next_y[8] = { 0 };

    //记录出处个数
    int exit[8] = { 0 };
    int count;
    int i, j, n;
    i = x;
    j = y;
    int temp_i, temp_j;
    int min;
    //起始位置
    board[i][j] = 1;
    for (int m = 2; m <= 64; m++)
    {
        for (n = 0; n < 8; n++)
        {
            exit[n] = 0;
        }
        n = 0;
        for (int k = 0; k < 8; k++)
        {
            temp_i = i + move_x[k];
            temp_j = j + move_y[k];

            if (temp_i < 0 || temp_j < 0 || temp_i>7 || temp_j>7)
            {
                continue;
            }
            if (board[temp_i][temp_j] == 0)
            {
                next_x[n] = temp_i;
                next_y[n] = temp_j;
                n++;
            }
        }
        count = n;  //下一步可走的方向
        if (count == 0)
            return 0;
        else if (count == 1)
            min = 0;   //如果可走方向只有一个,则该方向为下一步走的方向
        else
        {
            //查找下一个位置中出路数
            for (n = 0; n < count; n++)
            {
                for (int k = 0; k < 8; k++)
                {
                    temp_i = next_x[n] + move_x[k];
                    temp_j = next_y[n] + move_y[k];
                    if (temp_i < 0 || temp_j < 0 || temp_i>7 || temp_j>7)
                    {
                        continue;
                    }
                    if (board[temp_i][temp_j] == 0)
                    {
                        exit[n]++;
                    }
                }
            }
            int temp = exit[0];
            min = 0;
            //找到最小出处的方向,最为下一步的走向
            for (n = 0; n < count; n++)
            {
                if (temp > exit[n])
                {
                    temp = exit[n];
                    min = n;
                }
            }
        }
        //走最少出处的方向
        i = next_x[min];
        j = next_y[min];
        board[i][j] = m;
    }

    return 1;

}

python代码

# -*- coding: utf-8 -*-
""" Created on Sat Nov 4 20:22:59 2017 @author: yangwenbin """
import sys
import numpy as np

board=np.zeros((8,8))
def print_board(start_x,start_y):
    if traval(start_x,start_y)==1:
        print ("success\n")
        for i in range(board.shape[0]):
            for j in range(board.shape[1]):
                print ("%c%d"%(' ',int(board[i][j])),end=' ')
                pass
            print('\n')
            pass
        pass
    pass

def traval(x,y):
    #可移动的八个方向
    move_x=np.array([-2, -1, 1, 2, 2, 1, -1, -2])
    move_y=np.array([1, 2, 2, 1, -1, -2, -2, -1 ])

    #下一步的位置坐标
    next_i=np.zeros(8)
    next_j=np.zeros(8)

    #出处个数
    exit_out=np.zeros(8)
    i,j=x,y
    board[int(i)][int(j)]=1
    for m in range(2,65):
        for n in range(8):
            exit_out[n]=0
            pass
        n=0
        for k in range(8):
            temp_i=int(i)+move_x[k]
            temp_j=int(j)+move_y[k]
            if temp_i<0 or temp_j<0 or temp_i>7 or temp_j>7:
                continue
            if board[int(temp_i)][int(temp_j)]==0:
                next_i[n]=temp_i
                next_j[n]=temp_j
                n+=1
                pass
            pass
        count=n
        if count==0:
            return 0
        elif count==1:
            min_=0
            pass
        else:
            #记录下一个位置的出处数目
            for n in range(count):
                for k in range(8):
                    temp_i=next_i[n]+move_x[k]
                    temp_j=next_j[n]+move_y[k]
                    if temp_i<0 or temp_j<0 or temp_i>7 or temp_j>7:
                        continue
                    if board[int(temp_i)][int(temp_j)]==0:
                        exit_out[n]+=1
                        pass
                    pass
                pass
            min_=0
            temp=exit_out[0]
            for n in range(count):
                if temp>exit_out[n]:
                    temp=exit_out[n]
                    min_=n
                    pass
                pass
            pass

        i=next_i[min_]
        j=next_j[min_]
        board[int(i)][int(j)]=m
        pass
    return 1

if __name__=="__main__":
    start_x=sys.argv[1]
    start_y=sys.argv[2]
    print_board(start_x,start_y)
    原文作者:骑士周游问题
    原文地址: https://blog.csdn.net/qq_34637408/article/details/78461577
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞