poj 1656
2007/09/15
poj
現在、Acceptedが多い問題で悩まずできそうな問題にチャレンジ中。
1656 -- Counting Black
こんな問題だと計算量とか考慮に入れないですむので、自分的には楽なのだけど。
package p1656;
import java.io.IOException;
import java.util.Scanner;
public class Main {
public static void main(String[] args) throws IOException {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int x, y, l;
int[][] g = new int[100][100];
for (int i = 0; i < n; i++) {
String s = scanner.next();
x = scanner.nextInt() - 1;
y = scanner.nextInt() - 1;
l = scanner.nextInt();
if ("BLACK".equals(s)) {
black(g, x, y, l);
continue;
}
if ("WHITE".equals(s)) {
white(g, x, y, l);
continue;
}
if ("TEST".equals(s)) {
System.out.println(countBlack(g, x, y, l));
continue;
}
}
}
static void black(int[][] g, int x, int y, int l) {
for (int i = x; i < (x + l); i++) {
for (int j = y; j < (y + l); j++) {
g[i][j] = 1;
}
}
}
static void white(int[][] g, int x, int y, int l) {
for (int i = x; i < (x + l); i++) {
for (int j = y; j < (y + l); j++) {
g[i][j] = 0;
}
}
}
static int countBlack(int[][] g, int x, int y, int l) {
int count = 0;
for (int i = x; i < (x + l); i++) {
for (int j = y; j < (y + l); j++) {
if (g[i][j] == 1) {
count += 1;
}
}
}
return count;
}
}
import java.io.IOException;
import java.util.Scanner;
public class Main {
public static void main(String[] args) throws IOException {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int x, y, l;
int[][] g = new int[100][100];
for (int i = 0; i < n; i++) {
String s = scanner.next();
x = scanner.nextInt() - 1;
y = scanner.nextInt() - 1;
l = scanner.nextInt();
if ("BLACK".equals(s)) {
black(g, x, y, l);
continue;
}
if ("WHITE".equals(s)) {
white(g, x, y, l);
continue;
}
if ("TEST".equals(s)) {
System.out.println(countBlack(g, x, y, l));
continue;
}
}
}
static void black(int[][] g, int x, int y, int l) {
for (int i = x; i < (x + l); i++) {
for (int j = y; j < (y + l); j++) {
g[i][j] = 1;
}
}
}
static void white(int[][] g, int x, int y, int l) {
for (int i = x; i < (x + l); i++) {
for (int j = y; j < (y + l); j++) {
g[i][j] = 0;
}
}
}
static int countBlack(int[][] g, int x, int y, int l) {
int count = 0;
for (int i = x; i < (x + l); i++) {
for (int j = y; j < (y + l); j++) {
if (g[i][j] == 1) {
count += 1;
}
}
}
return count;
}
}
: