Monkeys in a Regular Forest
Monkeys in a Regular Forest |
Consider the situation of an ideal forest, where trees grow on a regular finite euclidean lattice.At every site only one tree grows, and it can be of one amongn species. Each species isdenoted by a single character ({}
are valid species, for instance). Two trees ofthe same species are considered neighbors if the maximum absolute difference between theircoordinates is one.
Families of (rather specialized) monkeys are released, one at a time, in this euclideanforest. Each family will occupy all neighboring tress of a single species which have not beentaken yet by another family. The monkeys are released from left to right and from top to bottom.
Given the map of the forest, build the map of the monkeys families, starting with ``1''and numbering them consecutively.
Input
Input file has the lines of a matrix of single characters, separated by single blank spaces.Next matrices (each matrix is a different instance to the problem) will be preceded by aline with a single ``%'' character and then the same structure as before.
Output
Output file has to show lines of integers separated by as many blank spaces as requiredto align columns to the right.The solution to each instance must be finished by a line with a single ``%'' character.
Sample Input
A B D E C C D F F W D D D D P W E W W W W % a A b B c d E t a a a a a c c t e f g h c a a t
Sample Output
1 2 3 4 5 5 3 6 6 7 3 3 3 3 8 7 9 7 7 7 7 % 1 2 3 4 5 6 7 8 1 1 1 1 1 5 5 8 9 10 11 12 5 1 1 8 %
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.StreamTokenizer;
import java.util.ArrayList;
public class Main
{
public static final boolean DEBUG = false;
public static int[][] dir = {{-1, -1}, {0, -1}, {1, -1}, {-1, 0}, {1, 0}, {-1, 1}, {0, 1}, {1, 1}};
public StreamTokenizer tokenizer;
public BufferedReader cin;
public PrintWriter cout;
public int[][] grid;
public int row, col;
public boolean[][] vis;
public void init()
{
try {
if (DEBUG) {
cin = new BufferedReader(new InputStreamReader(
new FileInputStream("d:\\OJ\\uva_in.txt")));
} else {
cin = new BufferedReader(new InputStreamReader(System.in));
}
cout = new PrintWriter(new OutputStreamWriter(System.out));
} catch (Exception e) {
e.printStackTrace();
}
}
public boolean input()
{
try {
String s = cin.readLine();
if (s == null) return false;
ArrayList<String> as = new ArrayList<String>();
as.add(s);
col = 0;
row = 1;
for (int i = 0, len = s.length(); i < len; i++) {
char ch = s.charAt(i);
if (Character.isLetter(ch)) col++;
}
boolean flag = true;
while (true) {
s = cin.readLine();
if (s == null) {
flag = false;
break;
}
if (s.compareTo("%") == 0) break;
as.add(s);
row++;
}
grid = new int[row][col];
for (int i = 0, size = as.size(); i < size; i++) {
String tmp = as.get(i);
for (int j = 0, k = 0, len = tmp.length(); j < len; j++) {
char ch = tmp.charAt(j);
if (Character.isLetter(ch)) {
grid[i][k++] = ch;
}
}
}
return flag;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
public void dfs(int x, int y, int ch, int fill)
{
if (x < 0 || x >= row || y < 0 || y >= col || vis[x][y]) return;
if (grid[x][y] != ch) return;
vis[x][y] = true;
grid[x][y] = fill;
for (int i = 0; i < 8; i++) {
int xn = x + dir[i][0];
int yn = y + dir[i][1];
dfs(xn, yn, ch, fill);
}
}
public void solve()
{
vis = new boolean[row][col];
int cnt = 1;
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
if (!vis[i][j]) {
int ch = grid[i][j];
dfs(i, j, ch, cnt);
cnt++;
}
}
}
int[] anslen = new int[col];
for (int i = 0; i < col; i++) {
int max = Integer.MIN_VALUE;
for (int j = 0; j < row; j++) {
String tmp = String.valueOf(grid[j][i]);
if (tmp.length() > max) {
max = tmp.length();
}
}
anslen[i] = max;
}
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
String tmp = String.valueOf(grid[i][j]);
int len = tmp.length();
if (len < anslen[j]) {
for (int k = 0; k < anslen[j] - len; k++) cout.print(" ");
}
if (j != 0) cout.print(" ");
cout.print(tmp);
}
cout.println();
}
cout.println("%");
cout.flush();
}
public static void main(String[] args)
{
Main solver = new Main();
solver.init();
boolean flag = true;
do {
flag = solver.input();
solver.solve();
} while (flag);
}
}