コード例 #1
0
ファイル: A1.java プロジェクト: paohui817/arena
 int maxPath(TreeNode tree) {
   if (tree == null) {
     return 0;
   }
   if (map.containsKey(tree)) {
     return map.get(tree);
   }
   int res = tree.val;
   int left = maxPath(tree.left);
   int right = maxPath(tree.right);
   int maxPath = max(left, right);
   res = res + maxPath > 0 ? res + maxPath : 0;
   map.put(tree, res);
   return res;
 }
コード例 #2
0
ファイル: A2.java プロジェクト: paohui817/arena
 public void solve() {
   int m = in.nextInt();
   int n = in.nextInt();
   int[][] a = new int[n][m];
   for (int i = 0; i < n; i++) {
     for (int j = 0; j < m; j++) {
       a[i][j] = in.nextInt();
     }
   }
   Set<Point> visited = new HashSet<Point>();
   List<Set<Point>> rooms = new ArrayList<Set<Point>>();
   for (int i = 0; i < n; i++) {
     for (int j = 0; j < m; j++) {
       Point cur = new Point(i, j);
       if (!visited.contains(cur)) {
         Set<Point> connected = new HashSet<Point>();
         LinkedList<Point> list = new LinkedList<Point>();
         list.addLast(cur);
         while (!list.isEmpty()) {
           Point head = list.removeFirst();
           debug(head);
           if (!visited.contains(head)) {
             visited.add(head);
             connected.add(head);
             if ((a[head.x][head.y] & 1) == 0) {
               list.addLast(new Point(head.x, head.y - 1));
             }
             if ((a[head.x][head.y] & 1 << 1) == 0) {
               list.addLast(new Point(head.x - 1, head.y));
             }
             if ((a[head.x][head.y] & 1 << 2) == 0) {
               list.addLast(new Point(head.x, head.y + 1));
             }
             if ((a[head.x][head.y] & 1 << 3) == 0) {
               list.addLast(new Point(head.x + 1, head.y));
             }
             debug(visited);
           }
         }
         rooms.add(connected);
       }
     }
   }
   out.println(rooms.size());
   int[][] count = new int[n][m];
   int maxRooms = 0;
   Map<Point, Integer> map = new HashMap<Point, Integer>();
   int hashCount = 0;
   for (Set<Point> room : rooms) {
     maxRooms = max(maxRooms, room.size());
     for (Point x : room) {
       count[x.x][x.y] = room.size();
       map.put(x, hashCount);
     }
     hashCount++;
   }
   out.println(maxRooms);
   int maxConnected = 0;
   int resX = 0, resY = 0;
   char resD = ' ';
   for (int j = 0; j < m; j++) {
     for (int i = n - 1; i >= 0; i--) {
       if (i > 0
           && ((a[i][j] & 1 << 1) != 0)
           && !map.get(new Point(i, j)).equals(map.get(new Point(i - 1, j)))
           && count[i][j] + count[i - 1][j] > maxConnected) {
         maxConnected = count[i][j] + count[i - 1][j];
         resX = i;
         resY = j;
         resD = 'N';
       }
       if (j < m - 1
           && ((a[i][j] & 1 << 2) != 0)
           && !map.get(new Point(i, j)).equals(map.get(new Point(i, j + 1)))
           && count[i][j] + count[i][j + 1] > maxConnected) {
         maxConnected = count[i][j] + count[i][j + 1];
         resX = i;
         resY = j;
         resD = 'E';
       }
     }
   }
   out.println(maxConnected);
   out.printf("%d %d %c\n", resX + 1, resY + 1, resD);
   debug(count);
 }