度小满8.31 Java后端笔试
第一题:暴力可以过,但我一直纠结dp,dp只能过36,有大佬帮看一下代码吗?
第二题:回溯
public static boolean backtrace(int[][] nums, int row, int column, int x, int cur){ if(row==nums.length-1&&column==nums[0].length-1){ if(cur==x){ return true; }else { return false; } } if(row==nums.length-1){ return backtrace(nums, row, column+1, x, cur+nums[row][column+1]); } if(column==nums[0].length-1){ return backtrace(nums, row+1, column, x, cur+nums[row+1][column]); } return backtrace(nums, row+1, column, x, cur+nums[row+1][column])|| backtrace(nums, row, column+1, x, cur+nums[row][column+1]); } public static String canReach(int[][] nums, int x){ boolean backtrace = backtrace(nums, 0, 0, x, nums[0][0]); if(backtrace) return "yes"; return "no"; } public static void main(String[] args) { Scanner sc = new Scanner(System.in); int group = sc.nextInt(); for(int i=0;i<group;i++){ int n = sc.nextInt(); int m = sc.nextInt(); int k = sc.nextInt(); int x = sc.nextInt(); int[][] nums = new int[n][m]; for(int j=0;j<n;j++){ for(int p=0;p<m;p++){ nums[j][p] = sc.nextInt(); } } String s = canReach(nums, x); System.out.println(s); } }
第三题:随便看了眼就去纠结第一题了。。。。