迷宫问题_走迷宫

迷宫问题

迷宫问题

image-20220509164130848

import java.util.*;
class pair{
    //保存坐标!
    public int x;
    public int y;
    public pair(int x,int y){
        this.x = x;
        this.y = y;
    }
}
public class Main{
    static int[][] next = {{-1,0},{1,0},{0,-1},{0,1}};
    public static boolean DFS(int[][] array,int row,int col,int curx,int cury,ArrayList<pair> result){
        if(curx==row-1&&cury==col-1){
            return true;//结束! 已经找到了该路径!
        }
        for(int i = 0;i<next.length;i++){
            int x = curx + next[i][0];
            int y = cury + next[i][1];
            if(x<0||x>=row||y<0||y>=col){
                //越界
                continue;
            }
            if(array[x][y]==0){
                //可走!
                //保存该位置!
                result.add(new pair(x,y));
                //标记该位置走过!
                array[x][y] = 1;
                if(DFS(array,row,col,x,y,result)){
                    //到达了
                    return true;
                }
                //回溯
                result.remove(result.size()-1);
                array[x][y] = 0;
            }
        }
        //未找到该路径需要回退!
        return false;
    }
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        while(sc.hasNext()){
            int row = sc.nextInt();
            int col = sc.nextInt();
            int[][] array = new int[row][col];
            for(int i = 0;i<row;i++){
                for(int j = 0;j<col;j++){
                    array[i][j] = sc.nextInt();
                }
            }
            ArrayList<pair> result = new ArrayList<>();
            result.add(new pair(0,0));
            DFS(array,row,col,0,0,result);
            for(pair cur:result){
                System.out.println("("+cur.x+","+cur.y+")");
            }
        }
    }
}

走迷宫

走迷宫

image-20220520232624340

// dfs深度优先搜索
import java.util.*;
public class Main{
    static int[][] direction = {{0,-1}, {-1,0}, {0,1}, {1,0}};
    public static void dfs(int x, int y, char[][] maze, int[][] map){
        for(int i = 0; i < 4; i++){
            int xx = x + direction[i][0];
            int yy = y + direction[i][1];
            if(xx >= 0 && xx < 10 && yy >= 0 && yy < 10 
              && maze[xx][yy] == '.' && map[xx][yy] > map[x][y]+1){
                map[xx][yy] = map[x][y] + 1;
                dfs(xx, yy, maze, map);
            }
        }
    }
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        while(sc.hasNext()){
            char[][] maze = new char[10][10];
            int[][] map = new int[10][10];
            for(int i = 0; i < 10; i++){
                String str = sc.next();
                for(int j = 0; j < 10; j++){
                    maze[i][j] = str.charAt(j);
                    map[i][j] = Integer.MAX_VALUE;
                }
            }
            map[0][1] = 0;
            dfs(0, 1, maze, map);
            System.out.println(map[9][8]);
        }
    }
}
// bfs广度优先搜索
import java.util.*;
public class Main{
    static int[][] direction = {{0,-1}, {-1,0}, {0,1}, {1,0}};
    static class Node{
        int x, y;
        public Node(int x, int y){
            this.x = x;
            this.y = y;
        }
    }
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        while(sc.hasNext()){
            char[][] maze = new char[10][10];
            int[][] map = new int[10][10];
            boolean[][] visited = new boolean[10][10];
            for(int i = 0; i < 10; i++){
                String str = sc.next();
                for(int j = 0; j < 10; j++){
                    maze[i][j] = str.charAt(j);
                    if(maze[i][j] == '#'){
                        visited[i][j] = true;
                    }
                }
            }
            Queue<Node> queue = new LinkedList<>();
            queue.offer(new Node(0, 1));
            while(!queue.isEmpty()){
                Node cur = queue.poll();
                for(int i = 0; i < 4; i++){
                    Node next = new Node(cur.x+direction[i][0], cur.y+direction[i][1]);
                    if(next.x >= 0 && next.x < 10 && next.y >= 0 && next.y < 10 
                      && !visited[next.x][next.y]){
                        queue.offer(next);
                        map[next.x][next.y] = map[cur.x][cur.y] + 1;
                        visited[next.x][next.y] = true;
                    }
                }
            }
            System.out.println(map[9][8]);
        }
    }
}

//bfs2
// write your code here
import java.util.*;
class pare {
    int x;
    int y;
    int step;
    public pare(int x,int y,int step){
        this.x = x;
        this.y = y;
        this.step = step;
    }
}
public class Main{
    public static int[][] next = {{0,1},{0,-1},{1,0},{-1,0}};
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        char[][] map = new char[10][10];
        while(sc.hasNext()){
            for(int i = 0;i<10;i++){
                String tmp = sc.nextLine();
                for(int j = 0;j<10;j++){
                    map[i][j] = tmp.charAt(j);
                }
            }
            pare start = new pare(0,1,0);
            System.out.println(bfs(map,10,10,start));
        }
    }
    public static int bfs(char[][] map,int row,int col,pare start){
        //广度优先!
        Queue<pare> q = new LinkedList<>();
        q.offer(start);
        while (!q.isEmpty()){
            pare cur = q.poll();
            if(cur.x==9&&cur.y==8){
                return cur.step;
            }
            for (int i = 0; i <next.length; i++) {
                int x = cur.x + next[i][0];
                int y = cur.y + next[i][1];
                int step = cur.step + 1;
                if(x>=0&&x<row&&y>=0&&y<col&&map[x][y]=='.'){
                    //标记已经走过!
                    map[x][y] = '#';
                    //入队!
                    q.offer(new pare(x,y,step));
                }
            }
        }
        return 0;
    }
}
#笔试#
全部评论
迷宫和人生一样要勇往直前
点赞 回复 分享
发布于 2022-08-27 13:03 河南

相关推荐

06-16 17:18
点赞 评论 收藏
分享
头像
06-16 23:59
已编辑
北京体育大学 测试工程师
一、主从库读写测试步骤1.&nbsp;环境准备:-&nbsp;搭建主从架构(1主1从或多从),确保主从复制正常(可通过&nbsp;SHOW&nbsp;SLAVE&nbsp;STATUS&nbsp;查看IO和SQL线程状态)。-&nbsp;主库配置&nbsp;server-id=1&nbsp;,从库配置&nbsp;server-id=2&nbsp;,主库开启二进制日志(&nbsp;log-bin&nbsp;)。2.&nbsp;读写测试核心操作:-&nbsp;写操作测试:在主库执行增删改(如&nbsp;INSERT/UPDATE/DELETE&nbsp;),观察从库数据是否同步(可通过定时查询或触发器验证)。-&nbsp;读操作测试:在从库执行查询(如&nbsp;SELECT&nbsp;),确认数据与主库一致,同时测试从库负载能力(可通过压测工具如&nbsp;sysbench&nbsp;模拟并发读)。-&nbsp;异常场景测试:-&nbsp;主库宕机:验证从库是否可切换为读写模式(需手动或通过中间件如MHA自动切换)。-&nbsp;网络延迟:模拟主从网络波动,观察数据同步延迟(&nbsp;Seconds_Behind_Master&nbsp;参数)。3.&nbsp;关键指标记录:-&nbsp;同步延迟时间、读写性能差异、高并发下的一致性表现。二、主从复制原理1.&nbsp;核心机制(基于二进制日志):-&nbsp;主库(Master):将数据变更记录到二进制日志(Binlog),包括DDL和DML操作。-&nbsp;从库(Slave):通过两个线程实现复制:-&nbsp;IO线程:连接主库,读取Binlog并写入本地中继日志(Relay&nbsp;Log)。-&nbsp;SQL线程:解析中继日志,在从库执行相同操作,实现数据同步。2.&nbsp;复制模式(3种):-&nbsp;异步复制:主库写入Binlog后立即返回,不等待从库确认(性能高,一致性差)。-&nbsp;半同步复制:主库等待至少一个从库确认接收Binlog后返回(性能和一致性平衡)。-&nbsp;全同步复制:主库等待所有从库确认后返回(一致性高,性能低)。3.&nbsp;常见问题与面试考点:-&nbsp;主从延迟:高并发下从库同步慢,解决方案:升级硬件、读写分离、分库分表。-&nbsp;数据一致性:异步复制可能丢失数据,可通过半同步或中间件(如Canal)弥补。-&nbsp;切换与故障恢复:手动切换主从时需锁定主库(&nbsp;FLUSH&nbsp;TABLES&nbsp;WITH&nbsp;READ&nbsp;LOCK&nbsp;),避免数据不一致。三、面试场景-&nbsp;结合场景说优势:主从架构用于读写分离,减轻主库压力,适合读多写少场景(如电商商品页查询)。-&nbsp;提扩展知识:主从是分布式基础,可延伸到集群架构(如MGR、InnoDB&nbsp;Cluster)或中间件(MyCat、ShardingSphere)。
点赞 评论 收藏
分享
评论
1
1
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务