UVa776 - Monkeys in a Regular Forest(种子填充法)

探讨在一个理想化的有限欧几里得格网森林中,不同种类的树木如何被特殊化的猴子家族占据。通过从左到右、从上到下的方式放置猴子家族,并确保每个家族占据同一物种的所有相邻树木。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 ({$A, B, C, \dots$} 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);
	}
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

kgduu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值