예제 #1
0
파일: A1.java 프로젝트: paohui817/arena
 public static ArrayList<String> serializeTree(TreeNode tree) {
   ArrayList<String> res = new ArrayList<>();
   LinkedList<TreeNode> queue = new LinkedList<>();
   queue.addLast(tree);
   while (!queue.isEmpty()) {
     TreeNode node = queue.removeFirst();
     if (node == null) {
       res.add(NULL);
     } else {
       res.add(String.valueOf(node.val));
       queue.add(node.left);
       queue.add(node.right);
     }
   }
   while (res.size() > 1 && res.get(res.size() - 1).equals(NULL)) {
     res.remove(res.size() - 1);
   }
   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);
 }