暴力解法干他。。。dfs进行处理 import java.util.Arrays; import java.util.LinkedList; public class SenvenB { public static int count = 0; public static LinkedList<Integer> 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); } }
蓝桥杯之方格填数
最新推荐文章于 2024-07-27 17:56:15 发布