栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

蓝桥杯地址怎么填(蓝桥杯填空题)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

蓝桥杯地址怎么填(蓝桥杯填空题)

暴力解法干他。。。dfs进行处理

import java.util.Arrays;
import java.util.linkedList;

public class SenvenB {
    public static int count = 0;
    public static linkedList path = new linkedList<>();

    public static void dfs(int[][] map,int row,int col){
        if(path.size() == 10){
            count++;
            return;
        }
        if(col == map[0].length){
            dfs(map,row+1,0);
            return;
        }
        for(int i = 0;i <= 9;i++){
            if(path.contains(i)){
                continue;
            }
            if(col > 0 && Math.abs(i-map[row][col-1]) == 1){
                continue;
            }
            if(row > 0 && Math.abs(i-map[row-1][col]) == 1){
                continue;
            }
            if(row > 0 && col > 0 && Math.abs(i-map[row-1][col-1]) == 1){
                continue;
            }
            if(row > 0 && col + 1 < map[0].length && Math.abs(i-map[row-1][col+1]) == 1){
                continue;
            }
            path.add(i);
            map[row][col] = i;
            dfs(map,row,col+1);
            map[row][col] = -2;
            path.removeLast();
        }
        return;
    }
    public static void main(String[] args){
        int[][] map = new int[3][4];
        for(int[] nums:map){
            Arrays.fill(nums,-2);
        }
        dfs(map,0,1);
        System.out.println(count);
    }
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/773650.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号