算法与设计实验总报告(1

您所在的位置:网站首页 asp实验报告小结 算法与设计实验总报告(1

算法与设计实验总报告(1

2024-02-12 15:16| 来源: 网络整理| 查看: 265

1. 骑士游历问题的程序:

   import java.util.Scanner;  

public class B {

    

    private boolean Travel(int firstX, int firstY, int[][] board) {  

        // 对应骑士可走的8个方向  

        int[] movex = { -2, -1, 1, 2,  2,  1, -1, -2 };  

        int[] movey = {  1,  2, 2, 1, -1, -2, -2, -1 };  

  

        // 下一步出路的位置  

 int[] nextStepX = new int[board.length];  

 int[] nextStepY = new int[board.length];  

  

        // 记录出路的个数  

 int[] exitS = new int[board.length];  

        int nextX = firstX;  

        int nextY = firstY;  

        board[nextX][nextY] = 1;  

  

for (int m = 2; m  7 || temJ < 0 || temJ > 7) {  

          continue;  

                }  

  

                // 记录下可走的方向  

                if (0 == board[temI][temJ]) {  

         nextStepX[count] = temI;  

         nextStepY[count] = temJ;  

           count++;  

                }  

            }  

  

            // 到这里,cout表示当前点有几种走法。nextStep中存储各种走法的坐标。  

            int min = -1;  

            if (count == 0) {  

                return false;  

            }  

  

            if (1 == count) {  

                min = 0;  

            } else {

           for (int i = 0; i < count; i++) {  

           for (int j = 0; j < 8; j++)

 {  

      int temI = nextStepX[i] + movex[j];  

      int temJ = nextStepY[i] + movey[j];  

                        if (temI < 0 || temI > 7 || temJ < 0 || temJ > 7) {  

                            continue;  

                        }  

                          

                        // 记录下这个位置可走的方向数  

                        if (0 == board[temI][temJ]) {  

                            exitS[i]++;  

                        }  

                    }  

                }  

  

                int tem = exitS[0];  

                min = 0;  

  

 // 从可走的方向中,寻找最少走的出路  

       for (int i = 1; i < count; i++) {  

                    if (tem > exitS[i]) {  

                        tem = exitS[i];  

                        min = i;  

                    }  

                }  

            }  

  

            // 得到最少的出路  

            nextX = nextStepX[min];  

            nextY = nextStepY[min];  

            board[nextX][nextY] = m;  

        }  

  

        return true;  

    }  

  

    public static void main(String[] args) {  

  

        int firstX, firstY;  

        System.out.println("输入起始点(0-7):");  

        Scanner scanner = new Scanner(System.in);  

  

        firstX = scanner.nextInt();  

        firstY = scanner.nextInt();  

        int[][] board = new int[8][8];  

        B knight = new B();  

  

        if (knight.Travel(firstX, firstY, board)) {  

            System.out.println("游历完成:");  

        } else {  

            System.out.println("游历失败!\n");  

        }  

  

        for (int i = 0; i < board.length; i++) {  

            for (int j = 0; j < board[0].length; j++) {  

                if (board[i][j] < 10) {  

      System.out.print(" " + board[i][j]);  

                } else {  

                    System.out.print(board[i][j]);  

                }  

                System.out.print(" ");  

            }  

            System.out.println();  

        }  

    }  

}

实例:

结果:

 

2. 行列变换问题的程序:

   import java.util.LinkedList;

import java.util.Scanner;

class graph{

static int sour, dest;//sour是图形的初始整数,dest是图形的目的整数

static int ans[]=new int[1



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3