首页 > 其他分享 >2024-01-19

2024-01-19

时间:2024-01-20 10:57:41浏览次数:22  
标签:01 19 ++ System 2024 int static new public

1.排列数字,深度遍历

import java.util.Scanner;

public class Main{
    static int n, N = 10;
    static int[] path = new int[N];
    static boolean[] st = new boolean[N];//用来标志数字有没有被排列过
    
    public static void dfs(int u, int n){
        //因为这个n不能在全局搞进去,所以要弄成一个参数放进来
        if(u == n){
            for(int i = 0; i < n; i ++){
                System.out.print(path[i] + " ");//这里是第几层的意思
            }
            System.out.println();
            return;
        }
        for(int i = 1; i <= n; i ++){
            if(!st[i]){
                path[u] = i;//这里是要排列的数字
                st[i] = true;
                dfs(u + 1, n);//由于这里传进去的是u+1,所以到临界点时u==n用来判断,但是还是最多只有n-1层
                st[i] = false;//清理现场(看递归函数在什么位置)递归结束之后,紧接着就恢复现场
            }
        }
    }
    
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        n = sc.nextInt();
        dfs(0, n);//从第0层开始
    }
}

2.n皇后问题
第一种按行遍历

import java.util.Scanner;

public class Main{
    static int N = 20, n;//因为对角线的个数是2n-1
    static char[][] path = new char[N][N];
    static boolean[] col = new boolean[N], dg = new boolean[N], udg = new boolean[N]; 
    
    public static void dfs(int u){
        if(u == n){
            for(int i = 0; i < n; i ++){
                for(int j = 0; j < n; j ++){
                    System.out.print(path[i][j]);
                }
                System.out.println();
            }
            System.out.println();
            return;
        }
        for(int i = 0; i < n; i ++)//这里的i是列数,当前枚举的是第u行
        {
            if(!col[i] && !dg[u + i] && !udg[n - u + i]){
                path[u][i] = 'Q';
                col[i] = dg[u + i] = udg[n - u + i] = true;
                dfs(u + 1);
                col[i] = dg[u + i] = udg[n - u + i] = false;
                path[u][i] = '.';//要在这里加上这一串!!!
            }
        }
    }
    
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        n = sc.nextInt();
        for(int i = 0; i < n; i ++){
            for(int j = 0; j < n; j ++){
                path[i][j] = '.';
            }
        }
        dfs(0);
    }
}

第二种方法

import java.util.Scanner;

public class Main{
    static int N = 20, n;//因为对角线的个数是2n-1
    static char[][] path = new char[N][N];
    static boolean[] col = new boolean[N], dg = new boolean[N], udg = new boolean[N];
    static boolean[] row = new boolean[N];
    
    public static void dfs(int x, int y, int s)//当前的横坐标x,纵坐标y,s已经放置了的皇后的数量
    {
        if(y == n){
            y = 0;
            x ++;
        }
        if(x == n){
            if(s == n){
                for(int i = 0; i < n; i ++){
                    for(int j = 0; j < n; j ++){
                        System.out.print(path[i][j]);
                    }
                    System.out.println();
                }
                System.out.println();
            }
            return;
        }
        
        //不放皇后
        dfs(x, y + 1,s);
        
        //放皇后
        if(!row[x] && !col[y] && !dg[x + y] && !udg[n - x + y]){
            path[x][y] = 'Q';
            row[x] = col[y] = dg[x + y] = udg[n - x + y] = true;
            dfs(x, y + 1,s + 1);
            row[x] = col[y] = dg[x + y] = udg[n - x + y] = false;
            path[x][y] = '.';//要在这里加上这一串!!!
        }
    }
    
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        n = sc.nextInt();
        for(int i = 0; i < n; i ++){
            for(int j = 0; j < n; j ++){
                path[i][j] = '.';
            }
        }
        dfs(0, 0, 0);
    }
}

4.数字迷宫 只有0可以走,从左上走到右下

import java.io.*;
import java.util.*;


class Node{
    int x;
    int y;
        
    public Node(int x, int y){
        this.x = x;
        this.y = y;
    }
}

public class Main{    
    static int N = 110, n, m;
    static int[][] a = new int[N][N];
    static int[][] d = new int[N][N];
    
    public static int bfs(){
        Queue<Node> q = new LinkedList();
        int[] dx = {-1,0,1,0};
        int[] dy = {0,1,0,-1};
        q.offer(new Node(0, 0));
        
        while(!q.isEmpty()){
            Node head = q.poll();
            
            for(int i = 0; i < 4; i ++){
                int x = head.x + dx[i];
                int y = head.y + dy[i];
                
                if(x >= 0 && y >= 0 && x < n && y < m && d[x][y] == 0 && a[x][y] == 0){
                    q.offer(new Node(x,y));
                    d[x][y] = d[head.x][head.y] + 1;
                }
            }
        }
        return d[n - 1][m - 1];
    }
    
    public static void main(String[] args)throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
        
        
        String[] s = br.readLine().split(" ");
        n = Integer.parseInt(s[0]);
        m = Integer.parseInt(s[1]);
        for(int i = 0; i < n; i ++){
            String[] st = br.readLine().split(" ");
            for(int j = 0; j < m; j ++){
                a[i][j] = Integer.parseInt(st[j]);
                d[i][j] = 0;
            }
        }
        System.out.print(bfs());
        bw.flush();
    }
}

标签:01,19,++,System,2024,int,static,new,public
From: https://www.cnblogs.com/wusuoweiju/p/17974381

相关文章

  • Petrozavodsk Summer 2019. Day 9. MEX Foundation Contest【杂题】
    比赛链接A.TheOnePolynomialMan给定模数\(p\)和\(0\simp-1\)的两个集合\(U,V\),求有多少个有序对\((a,b)\)满足:\(f(a,b)=\prod\limits_{z\inV}\left(\frac{(2a+3b)^2+5a^2}{(3a+b)^2}+\frac{(2a+5b)^2+3b^2}{(3a+2b)^2}-z\right)\equiv0\pmo......
  • 2024-01-20(今日动态)
    1.安装PyCharm:https://blog.csdn.net/m0_46374969/article/details/131292897。2.安装PyQT5可视化的QT5Designer界面:https://zhuanlan.zhihu.com/p/653664303。https://www.jb51.net/python/2873999dc.htm。 3.(问题现象):Smart-Plane的T8U6无法烧录程序。(解决):终极原因:SCL和SWD......
  • shopify URL如何实现301跳转以及验证方法
    需要APP:TinyIMG步骤:1、在shopify后台打开插件“TinyImg”2、点击“改善SEO”,然后再点击“停止因链接断开而失去销售”3、点击“创建URL”重定向在上图中,按照指示分别填写所对应的URL,即可实现URL的重定向了。如何验证301跳转成功当我们设置URL重定向之后,如何验证其是否成......
  • 洛谷入门赛 #19 题解
    比赛传送门A-分饼干I将三盒饼干按数量排序。若较小的两盒饼干数之和大于另一盒饼干,则将较小的两盒饼干奖励给第一名,另一盒奖励给第二名;若较大的一盒饼干数大于另外两盒之和,则将较大的一盒奖励给第一名,另外两盒奖励给第二名。B-分饼干II每个人分到的饼干数都不同,即可以看......
  • [NOIP1998 普及组] 三连击
    [NOIP1998普及组]三连击题目背景本题为提交答案题,您可以写程序或手算在本机上算出答案后,直接提交答案文本,也可提交答案生成程序。题目描述将共个数分成组,分别组成个三位数,且使这个三位数构成的比例,试求出所有满足条件的个三位数。输入格式无输出格式若干行,每行个......
  • STM32CubeMX教程19 I2C - MPU6050驱动
    1、准备材料正点原子stm32f407探索者开发板V2.4STM32CubeMX软件(Version6.10.0)野火DAP仿真器keilµVision5IDE(MDK-Arm)ST-LINK/V2驱动XCOMV2.6串口助手逻辑分析仪nanoDLA2、实验目标使用STM32CubeMX软件配置STM32F407开发板的I2C1与MPU6050芯片通信,读取MPU6050的三轴加......
  • 算法模板 v1.3.1.20240120
    算法模板v1.1.1.20240115:之前的历史版本已经不可寻,创建了第一份算法模板。v1.2.1.20240116:删除“编译”-“手动开栈”与“编译”-“手动开O优化”;将“编译”-“CF模板”中的第20行代码cin>>T;注释;删除“读写”及其目录下的内容;删除“图论”-“欧拉图”-“混合图”;删除“图论”-......
  • 2024.1.20闲话——路灯花
    昨天我跟好哥们去超市。超市里面突然出现一个人自称是十班的,把一瓶东鹏的咖啡塞我朋友手里,说这个肯定中奖。然后我就想起来植物大战僵尸里砸罐子那个路灯花。之前我们班的人买东鹏的电解质水的时候拿瓶底对着电灯然后看上面的字是否中奖。当时我嘲讽他们为路灯花。恰好我哥......
  • 每日一题 2024-1-20 按分隔符拆分字符串
    1.题目(1239)原题链接给你一个字符串数组\(words\)和一个字符\(separator\),请你按\(separator\)拆分\(words\)中的每个字符串。返回一个由拆分后的新字符串组成的字符串数组,不包括空字符串。注意\(separator\)用于决定拆分发生的位置,但它不包含在结果字符串中。拆分......
  • 【2015~2024】大牛直播SDK演化史
    大牛直播SDK的由来大牛直播SDK始于2015年,最初我们只是想做个低延迟的RTMP推拉流解决方案,用于移动单兵等毫秒级延迟的场景下,我们先是实现了Android平台RTMP直播推送模块,当我们用市面上可以找到的RTMP播放器测试时延的时候,居然都要6-7秒延迟,这在直播场景下,几乎是不可接受的,所以我们有......