Moderator: Board moderators
fillRegion()regionColorch[java]
import java.io.*;
import java.util.*;
public class GraphicalEditor {
public static void main(String[] args) {
try {
String tmp;
boolean start = true;
char[][] matrix = null;
while (!(tmp = readLn().trim()).equals("X")) {
StringTokenizer tok = new StringTokenizer(tmp);
tmp = tok.nextToken();
if (tmp.equals("I")) {
int t = Integer.parseInt(tok.nextToken());
matrix =
new char[Integer.parseInt(tok.nextToken()) + 2][t + 2];
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[0].length; j++) {
matrix[i][j] = 'O';
}
}
} else if (tmp.equals("C")) {
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[0].length; j++) {
matrix[i][j] = 'O';
}
}
} else if (tmp.equals("L")) {
int t = Integer.parseInt(tok.nextToken());
matrix[Integer.parseInt(tok.nextToken())][t] =
tok.nextToken().charAt(0);
} else if (tmp.equals("V")) {
int x = Integer.parseInt(tok.nextToken());
int y1 = Integer.parseInt(tok.nextToken());
int y2 = Integer.parseInt(tok.nextToken());
char color = tok.nextToken().charAt(0);
int max = Math.max(y1, y2);
for (int i = Math.min(y1, y2); i <= max; i++) {
matrix[i][x] = color;
}
} else if (tmp.equals("H")) {
int x1 = Integer.parseInt(tok.nextToken());
int x2 = Integer.parseInt(tok.nextToken());
int y = Integer.parseInt(tok.nextToken());
char color = tok.nextToken().charAt(0);
int max = Math.max(x1, x2);
for (int i = Math.min(x1, x2); i <= max; i++) {
matrix[y][i] = color;
}
} else if (tmp.equals("K")) {
int x1 = Integer.parseInt(tok.nextToken());
int y1 = Integer.parseInt(tok.nextToken());
int x2 = Integer.parseInt(tok.nextToken());
int y2 = Integer.parseInt(tok.nextToken());
char color = tok.nextToken().charAt(0);
int max = Math.max(x1, x2);
int maxy = Math.max(y1, y2);
for (int i = Math.min(x1, x2); i <= max; i++) {
for (int j = Math.min(y1, y2); j <= max; j++) {
matrix[j][i] = color;
}
}
} else if (tmp.equals("F")) {
int x = Integer.parseInt(tok.nextToken());
int y = Integer.parseInt(tok.nextToken());
char color = tok.nextToken().charAt(0);
boolean[][] inRegion =
new boolean[matrix.length][matrix[0].length];
inRegion[y][x] = true;
char prevColor = matrix[y][x];
// System.out.println(prevColor);
matrix[y][x] = color;
inRegion(x, y, matrix, inRegion, color, prevColor);
} else if (tmp.equals("S")) {
if (!start) {
System.out.println();
}
start = false;
System.out.println(tok.nextToken());
for (int i = 1; i < matrix.length - 1; i++) {
for (int j = 1; j < matrix[i].length - 1; j++) {
System.out.print(matrix[i][j]);
}
if (i != matrix.length-2)
System.out.println();
}
} else if (tmp.equals("X")) {
return;
}
}
} catch (Exception e) {
while (true);
}
}
static void inRegion(
int x,
int y,
char[][] matrix,
boolean[][] inRegion,
char color,
char prevcolor) {
x--;
if (x != 0
&& y != 0
&& x != matrix[0].length - 1
&& y != matrix.length - 1
&& matrix[y][x] == prevcolor
&& !inRegion[y][x]) {
inRegion[y][x] = true;
matrix[y][x] = color;
inRegion(x, y, matrix, inRegion, color, prevcolor);
}
x += 2;
if (x != 0
&& y != 0
&& x != matrix[0].length - 1
&& y != matrix.length - 1
&& matrix[y][x] == prevcolor
&& !inRegion[y][x]) {
inRegion[y][x] = true;
matrix[y][x] = color;
inRegion(x, y, matrix, inRegion, color, prevcolor);
}
x--;
y--;
if (x != 0
&& y != 0
&& x != matrix[0].length - 1
&& y != matrix.length - 1
&& matrix[y][x] == prevcolor
&& !inRegion[y][x]) {
inRegion[y][x] = true;
matrix[y][x] = color;
inRegion(x, y, matrix, inRegion, color, prevcolor);
}
y += 2;
if (x != 0
&& y != 0
&& x != matrix[0].length - 1
&& y != matrix.length - 1
&& matrix[y][x] == prevcolor
&& !inRegion[y][x]) {
inRegion[y][x] = true;
matrix[y][x] = color;
inRegion(x, y, matrix, inRegion, color, prevcolor);
}
y--;
}
static String readLn() // utility function to read from stdin
{
int maxLg = 200;
byte lin[] = new byte[maxLg];
int lg = 0, car = -1;
String line = "";
try {
while (lg < maxLg) {
car = System.in.read();
if (car < 0)
break;
else if (car == '\n') {
lin[lg++] += car;
break;
}
lin[lg++] += car;
}
} catch (IOException e) {
return (null);
}
if ((car < 0) && (lg == 0))
return (new String("")); // eof
return new String(lin, 0, lg);
}
}
[/java]Algoritmo wrote:--in K command, X2>X1 and Y2>Y1, but this is not always true for V and H commands.
Alexander Denisjuk wrote:Why do you think K X1 Y1 X2 Y2 C would be better?Algoritmo wrote:I found 2 bad tricks:
--K comand synthax is not K X1 Y1 X2 Y2 C, but K X1 X2 Y1 Y2 C.
Users browsing this forum: No registered users and 0 guests