zoukankan      html  css  js  c++  java
  • Snake and Ladder Problem

    Given a snake and ladder board, find the minimum number of dice throws required to reach the destination or last cell from source or 1st cell. Basically, the player has total control over outcome of dice throw and wants to find out minimum number of throws required to reach last cell.

    If the player reaches a cell which is base of a ladder, the player has to climb up that ladder and if reaches a cell is mouth of the snake, has to go down to the tail of snake without a dice throw.

    O(n) BFS

    /**
     * Given a snake and ladder board, find the minimum number of dice throws required to reach the destination or last cell from source or 1st cell. Basically, the player has total control over outcome of dice throw and wants to find out minimum number of throws required to reach last cell.
     * If the player reaches a cell which is base of a ladder, the player has to climb up that ladder and if reaches a cell is mouth of the snake, has to go down to the tail of snake without a dice throw.
     * @author
     */
    import java.util.*;
    class Grid{
        int index;
        int distance;
        public Grid(int index, int distance){
            this.index = index;
            this.distance = distance;
        }
    }
    public class SnakeAndLadderProblem {
    
        public static int miniStep(int[] board){
            int min = 0;
            boolean visited[] = new boolean[board.length];
            Queue<Grid> queue = new LinkedList<Grid>();
            Grid root = new Grid(0,0);
            visited[0] = true;
            queue.offer(root);
            while(!queue.isEmpty()){
                Grid top = queue.poll();
                if(top.index == board.length -1){
                    min = top.distance;
                    break;
                }
                for(int i = top.index + 1; i <= top.index + 6 && i < board.length; i++){
                    if(visited[i] == false){
                        Grid newGrid  = null;
                        visited[i] = true;                 
                        if(board[i] !=-1)
                             newGrid = new Grid(board[i], top.distance + 1);
                        else
                             newGrid = new Grid(i, top.distance + 1);
                        queue.add(newGrid);
                    }
                }    
            }
            return min;
        }
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            int board[] = new int[30];
            for(int i = 0; i < 30; i++){
                board[i] = -1;
            }
            // Ladders
                    board[2] = 21;
                    board[4] = 7;
                    board[10] = 25;
                    board[19] = 28;
    
            // Snakes
                    board[26] = 0;
                    board[20] = 8;
                    board[16] = 3;
                    board[18] = 6;
           System.out.println("min step is "+ miniStep(board));
        }
    
    }
  • 相关阅读:
    一种可以实时检测IP地址合法性的EditText输入框
    LVDS 屏幕 M215HGE-L21 在 rk3288 上的适配过程
    轻读一下 Android 应用开发中的 assets 目录
    XML与其在Android下的解析
    Linux Shell脚本实现根据进程名杀死进程
    RSA host key has changed 错误
    Linux下安装jdk8步骤详述
    Windows/Linux javac/java编译运行引入所需的jar包
    No cached version of ..... available for offline mode.
    Java学习之InputStream中read()与read(byte[] b)
  • 原文地址:https://www.cnblogs.com/joannacode/p/6012962.html
Copyright © 2011-2022 走看看