Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Coloring map
Language:
Ada
Assembly
Bash
C#
C++ (gcc)
C++ (clang)
C++ (vc++)
C (gcc)
C (clang)
C (vc)
Client Side
Clojure
Common Lisp
D
Elixir
Erlang
F#
Fortran
Go
Haskell
Java
Javascript
Kotlin
Lua
MySql
Node.js
Ocaml
Octave
Objective-C
Oracle
Pascal
Perl
Php
PostgreSQL
Prolog
Python
Python 3
R
Rust
Ruby
Scala
Scheme
Sql Server
Swift
Tcl
Visual Basic
Layout:
Vertical
Horizontal
//'main' method must be in a class 'Rextester'. //Compiler version 1.8.0_111 import java.util.*; import java.lang.*; class Rextester { public static int[] xPoint = { 0, -1, 1, 0 }; public static int[] yPoint = { 1, 0, 0, -1 }; static int count; public static List<Integer> list; public static void main(String[] args) { // TODO Auto-generated method stub Readfile(); } public static void Excute(int xStart, int yStart, int[][] matrix, int rows, int cols) { for (int i = 0; i < 4; i++) { int tempX = xStart + xPoint[i]; int tempY = yStart + yPoint[i]; if (0 <= tempX && tempX < rows && 0 <= tempY && tempY < cols && matrix[tempX][tempY] == 0) { matrix[tempX][tempY] = 4; count++; Excute(tempX, tempY, matrix, rows, cols); } } // for (int i = 0; i < rows; i++) { // for (int j = 0; j < cols; j++) { // System.out.print(matrix[i][j] + " "); // } // System.out.println(); // } // System.out.println(); } public static void Readfile() { BufferedReader reader; try { list = new ArrayList<Integer>(); reader = new BufferedReader(new FileReader("D:\\Long\\Homework\\Coloring map\\input1.txt")); String line = reader.readLine(); int case_seat = Integer.parseInt(line); line = reader.readLine(); while (line != null) { String[] size_matrix = line.split(" "); if (size_matrix.length == 2) { int rows = Integer.parseInt(size_matrix[1]); int cols = Integer.parseInt(size_matrix[0]); // System.out.println(rows + " " + cols); int[][] matrix = new int[rows][cols]; line = reader.readLine(); for (int row = 0; row < rows; row++) { if (row != rows) { try { String[] row_matrix = line.split(" "); for (int col = 0; col < cols; col++) { try { matrix[row][col] = Integer.parseInt(row_matrix[col]); } catch (Exception e) { } } } catch (Exception e) { } line = reader.readLine(); } } for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { if (matrix[i][j] == 0) { matrix[i][j] = 4; count = 1; Excute(i, j, matrix, rows, cols); list.add(count); } } } Collections.sort(list, new Comparator<Integer>() { @Override public int compare(Integer o1, Integer o2) { // TODO Auto-generated method stub return o2.compareTo(o1); } }); for(int i=0;i<list.size();i++) { System.out.println(list.get(i)); } int k = 0; for (int i = 0; i < list.size(); i++) { k = k + list.get(i); } System.out.println(k); list.clear(); // for (int i = 0; i < rows; i++) { // for (int j = 0; j < cols; j++) { // System.out.print(matrix[i][j] + " "); // } // System.out.println(); // } // System.out.println(); } } } catch (IOException e) { e.printStackTrace(); } } }
[
+
]
Show input
edit mode
|
history
|
discussion