小紅書:筆試題(棋盤最短路徑,筆記本草稿棧,迷宮游戲)


1. 棋盤最短路徑問題

題目描述:

題目描述:
假設以一個n*m的矩陣作為棋盤,每個棋位對應一個二維坐標 (x, y)。你有一顆棋子位於左上起點(0, 0),現在需要將其移動到右下底角 (n-1, m-1),棋子可以向相鄰的上下左右位置移動,每個坐標最多只能經過一次。棋盤中散布着若干障礙,障礙物不能跨越,只能繞行,問是否存在到達右下底角的路線?若存在路線,輸出所需的最少移動次數;若不存在,輸出0。 Input 第一行三個正整數n,m和k,代表棋盤大小與障礙物個數  1< n、m < 100,  k < n*m 第二行至第k+1行,每行為兩個整數x和y,代表k個障礙物的坐標。

輸入
輸入三個正整數n,m和k,代表棋盤大小與障礙物個數  1< n、m < 100,  k < n*m
第二行至第k+1行,每行為兩個整數x和y,代表k個障礙物的坐標。

輸出
輸出從起點到終點的最短路徑的長度,如果不存在,即輸出0

 

代碼:

import java.util.Arrays;
import java.util.Scanner;
 
public class Main {
    public static int min = Integer.MAX_VALUE;
    public static boolean find = false;
    public static void main( String[] args ) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int m = sc.nextInt();
        int k = sc.nextInt();//障礙
        boolean[][] go = new boolean[n][m];//棋盤
        boolean[][] visit = new boolean[n][m];//走過的痕跡
        //將所有的位置設置為true
        for(int i=0;i<n;i++) {
            Arrays.fill(go[i], true);
        }
        //有棋子障礙處設置為false
        for(int i=0;i<k;i++) {
            int a = sc.nextInt();
            int b = sc.nextInt();
            go[a][b] = false;
        }
        findPath(0, 0, 0, visit, go, n - 1, m - 1);
        //如果找到了輸出結果,沒路輸出0
        if(find) {
            System.out.println(min);
        }else {
            System.out.println(0);
        }
    }
 
    private static void findPath( int row , int col , int s, boolean[][] visit, boolean[][] go, int targetRow, int targetCol ) {
        if(row == targetRow && col == targetCol) {//到達目標位置
            find = true;
            if(s < min) {
                min = s;
            }
            return;
        }
        if(col < targetCol && !visit[row][col+1] && go[row][col+1]) { //
            visit[row][col+1] = true;
            findPath(row, col + 1, s + 1, visit, go, targetRow, targetCol);
            visit[row][col+1] = false;
        }
        if(row < targetRow && !visit[row+1][col] && go[row+1][col]) { //
            visit[row+1][col] = true;
            findPath(row + 1, col, s + 1, visit, go, targetRow, targetCol);
            visit[row+1][col] = false;
        }
        if(col > 0 && !visit[row][col-1] && go[row][col-1]) { //
            visit[row][col-1] = true;
            findPath(row, col - 1, s + 1, visit, go, targetRow, targetCol);
            visit[row][col-1] = false;
        }
        if(row > 0 && !visit[row-1][col] && go[row-1][col]) { //
            visit[row-1][col] = true;
            findPath(row - 1, col, s + 1, visit, go, targetRow, targetCol);
            visit[row-1][col] = false;
        }
    }
}

 

2. 筆記草稿

代碼1,利用棧

import java.util.Scanner;
import java.util.Stack;

public class Main{
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String str = sc.nextLine();
        String results = getRealContent(str);
        System.out.println(results);
    }
    public static String getRealContent(String str) {
        StringBuilder sb = new StringBuilder("");
        char flag = '(';
        Stack<Character> stack = new Stack<Character>();
        char[] chars = str.toCharArray();
        //遍歷每個數組將其放入棧中
        for (int i = 0; i < chars.length; i++) {
            if (chars[i] =='(') {//入棧
                stack.push(chars[i]);
            }else if (chars[i] ==')') {//彈出和這個)對應的(以及他們中間的內容
                while(!stack.isEmpty() && stack.peek()!= flag){
                    stack.pop();
                }
                stack.pop();
            }else if (chars[i] =='<') {//刪除前面的一個內容
                if(!stack.empty() && stack.peek()!=flag){
                    stack.pop();
                }
                
            }else{
                stack.push(chars[i]);
            }
        }
        while(!stack.empty()){
            sb.append(stack.pop());
        }
        
        return sb.reverse().toString();
    }
}

代碼2,括號加減

import java.util.Scanner;
 
public class Main {
    public static void main( String[] args ) {
        Scanner sc = new Scanner(System.in);
        String s = sc.next();
        String res = "";
        int count = 0, len = s.length();
        for(int i=0;i<len;i++) {
            char c = s.charAt(i);
            if(c == '(') {
                count ++;
            }else if(c == ')') {
                count --;
            }else if(c == '<') {
                if(res.length() > 0 && count == 0 && res.charAt(res.length() - 1) != ')') {
                    res = res.substring(0, res.length() - 1);
                }
            }else if(count == 0) {
                res += c;
            }
        }
        System.out.println(res);
    }
}

 

3. 迷宮游戲

import java.util.*;
/**
 * 5
 * .#...
 * ..#S.
 * .E###
 * .....
 * .....
 */
public class B {
    static int[][] d = {
            {-1, 0},
            {0, 1},
            {1, 0},
            {0, -1}
    };
    static int startX = 0;
    static int startY = 0;
    static int endX = 0;
    static int endY = 0;
    static Set<Integer> stepList = new HashSet<>();
 
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        in.nextLine();
        char[][] board = new char[n][n];
        for (int i = 0; i < n; i++) {
            String line = in.nextLine();
            for (int j = 0; j < n; j++) {
                board[i][j] = line.charAt(j);
                if (board[i][j] == 'S') {
                    startX = i;
                    startY = j;
                } else if (board[i][j] == 'E') {
                    endX = i;
                    endY = j;
                }
            }
        }
 
        shortestLength(board, n);
 
        if (stepList.size() == 0) {
            System.out.println(-1);
            return;
        }
        int minStep = Integer.MAX_VALUE;
        for (Integer integer : stepList) {
            if (integer < minStep) {
                minStep = integer;
            }
        }
        System.out.println(minStep);
 
    }
 
    private static void shortestLength(char[][] board, int n) {
 
        boolean[][] visited = new boolean[n][n];
        visited[startX][startY] = true;
        shortestLength(board, n, startX, startY, 0, visited);
 
    }
 
    private static void shortestLength(char[][] board, int n, int startX, int startY, int step, boolean[][] visited) {
        if (startX == endX && startY == endY) {
            stepList.add(step);
            return;
        }
        for (int i = 0; i < 4; i++) {
            int newX = startX + d[i][0];
            int newY = startY + d[i][1];
            if (newX < 0) {
                newX = n - 1;
            } else if (newX==n) {
                newX = 0;
            }
            if (newY <0) {
                newY = n-1;
            }else if (newY==n){
                newY = 0;
            }
            if (board[newX][newY] !='#' && !visited[newX][newY]) {
                visited[newX][newY] = true;
                shortestLength(board, n, newX, newY, step + 1, visited);
                visited[newX][newY] = false;
            }
        }
    }
 
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM