<第一题AC,第二题貌似测试用例有问题>
第一题:求种树的种数:
package org.example; import java.util.Scanner; public class Main1 { static boolean[][] input, isPlace; static int N, M; static long result; public static void main(String[] args) { Scanner sc = new Scanner(System.in); N = sc.nextInt(); M = sc.nextInt(); input = new boolean[N][M]; isPlace = new boolean[N][M]; result = 0; boolean flag = false; for (int i = 0; i < N; i++) { for (int j = 0; j < M; j++) { input[i][j] = sc.nextInt() == 1 ? true : false; } } BackTrace(0, 0); System.out.println(result); } public static void BackTrace(int x, int y) { if (x == N) { result += 1; return; } if (!input[x][y]) { if (y == M - 1) { BackTrace(x + 1, 0); } else { BackTrace(x, y + 1); } } else { if (isOk(x, y)) { isPlace[x][y] = true; if (y == M - 1) { BackTrace(x + 1, 0); } else { BackTrace(x, y + 1); } isPlace[x][y] = false; } if (y == M - 1) { BackTrace(x + 1, 0); } else { BackTrace(x, y + 1); } } } public static boolean isOk(int x, int y) { if (x - 1 >= 0 && isPlace[x - 1][y]) { return false; } if (x + 1 < N && isPlace[x + 1][y]) { return false; } if (y - 1 >= 0 && isPlace[x][y - 1]) { return false; } if (y + 1 < M && isPlace[x][y + 1]) { return false; } return true; } }
第二题:求黄金时间段:
package org.example; import java.util.Arrays; import java.util.Comparator; import java.util.Scanner; class Node { int beginTime; int value; public Node(int beginTime, int value) { this.beginTime = beginTime; this.value = value; } } public class Main2 { public static void main(String[] args) { Scanner in = new Scanner(System.in); int orders = in.nextInt(); int[][] input = new int[orders][3]; Node[] inNode = new Node[orders]; int maxResult = -1, maxTime = 0; for (int i = 0; i < orders; i++) { for (int j = 0; j < 3; j++) { input[i][j] = in.nextInt(); } inNode[i] = new Node(input[i][1], input[i][2]); } Arrays.sort(inNode, new Comparator<Node>() { @Override public int compare(Node o1, Node o2) { return o1.beginTime - o2.beginTime; } }); int end = 0,start=0; int curTotal = 0; while(start!=orders&&end!=orders) { while (end < orders && inNode[end].beginTime - inNode[start].beginTime <= 7200) { curTotal += inNode[end].value; end++; } if (curTotal > maxResult) { maxResult = curTotal; maxTime = inNode[start].beginTime; } if (end == orders) { break; } while (start < orders && (inNode[end].beginTime - inNode[start].beginTime) > 7200) { curTotal -= inNode[start].value; start++; } // curTotal -= inNode[start].value; if (start == orders) { break; } } System.out.printf("%d %d\n", maxTime, maxResult);//7200 } }
全部评论
(9) 回帖