문제
https://www.acmicpc.net/problem/1986
1986번: 체스
첫째 줄에는 체스 판의 크기 n과 m이 주어진다. (1 ≤ n, m ≤ 1000) 그리고 둘째 줄에는 Queen의 개수와 그 개수만큼의 Queen의 위치가 입력된다. 그리고 마찬가지로 셋째 줄에는 Knight의 개수와 위치,
www.acmicpc.net
풀이
단순한 구현문제
처음에는 그냥 단순하게 구현해서 풀었다.
너무 코드가 더러워서 다시 제대로 풀었다.
코드
정리해서 다시 푼 코드
import java.io.*;
import java.util.*;
class Main {
public static class Point {
int r;
int c;
public Point(int r, int c) {
this.r = r;
this.c = c;
}
public boolean equals(Object object) {
Point point = (Point) object;
if (point.r == this.r && point.c == this.c) {
return true;
}
return false;
}
}
static List<Point> q = new ArrayList<>();
static List<Point> k = new ArrayList<>();
static List<Point> p = new ArrayList<>();
static int n, m;
static boolean board[][];
public static void main(String args[]) throws Exception{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
n = Integer.parseInt(st.nextToken());
m = Integer.parseInt(st.nextToken());
board = new boolean [n+1][m+1];
for(int i=0;i<3;i++) {
st = new StringTokenizer(br.readLine());
int cnt = Integer.parseInt(st.nextToken());
for(int j=0;j<cnt;j++) {
int r = Integer.parseInt(st.nextToken());
int c = Integer.parseInt(st.nextToken());
board[r][c] = true;
if(i == 0) {
q.add(new Point(r, c));
}
else if (i==1) {
k.add(new Point(r, c));
}
else p.add(new Point(r, c));
}
}
//나이트
int kx [] = {-2, -2, -1, -1, 1, 1, 2, 2};
int ky [] = {1, -1, 2, -2, 2, -2, 1, -1};
for(int i=0;i<k.size();i++) {
for(int j=0;j<8;j++) {
int nx = k.get(i).r + kx[j];
int ny = k.get(i).c + ky[j];
if(nx < 1 || ny < 1 || nx > n || ny > m) continue;
board[nx][ny] = true;
}
}
//퀸
for(int i=0;i<q.size();i++) {
queen(q.get(i).r, q.get(i).c);
}
//출력
int answer = 0;
for(int i=1;i<=n;i++) {
for(int j=1;j<=m;j++) {
if(!board[i][j]) answer += 1;
}
}
System.out.print(answer);
}
public static void queen(int r, int c) {
int dx [] = {-1, 1, 0, 0, 1, -1, 1, -1};
int dy [] = {0, 0, -1, 1, 1, -1, -1, 1};
for(int i=0;i<8;i++) {
int nx = r;
int ny = c;
while(true) {
nx = nx - dx[i];
ny = ny - dy[i];
if(nx < 1 || ny < 1 || nx > n || ny > m) break;
Point point = new Point(nx, ny);
if(k.contains(point) || p.contains(point)) break;
board[nx][ny] = true;
}
}
}
}
그냥 푼 코드
import java.io.*;
import java.util.*;
class Main {
public static class Point {
int r;
int c;
public Point(int r, int c) {
this.r = r;
this.c = c;
}
public boolean equals(Object object) {
Point point = (Point) object;
if (point.r == this.r && point.c == this.c) {
return true;
}
return false;
}
}
static List<Point> q = new ArrayList<>();
static List<Point> k = new ArrayList<>();
static List<Point> p = new ArrayList<>();
static int n, m;
static boolean board[][];
public static void main(String args[]) throws Exception{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
n = Integer.parseInt(st.nextToken());
m = Integer.parseInt(st.nextToken());
board = new boolean [n+1][m+1];
for(int i=0;i<3;i++) {
st = new StringTokenizer(br.readLine());
int cnt = Integer.parseInt(st.nextToken());
for(int j=0;j<cnt;j++) {
int r = Integer.parseInt(st.nextToken());
int c = Integer.parseInt(st.nextToken());
board[r][c] = true;
if(i == 0) {
q.add(new Point(r, c));
}
else if (i==1) {
k.add(new Point(r, c));
}
else p.add(new Point(r, c));
}
}
//나이트
int kx [] = {-2, -2, -1, -1, 1, 1, 2, 2};
int ky [] = {1, -1, 2, -2, 2, -2, 1, -1};
for(int i=0;i<k.size();i++) {
for(int j=0;j<8;j++) {
int nx = k.get(i).r + kx[j];
int ny = k.get(i).c + ky[j];
if(nx < 1 || ny < 1 || nx > n || ny > m) continue;
board[nx][ny] = true;
}
}
//퀸
for(int i=0;i<q.size();i++) {
queen(q.get(i).r, q.get(i).c);
}
//출력
int answer = 0;
for(int i=1;i<=n;i++) {
for(int j=1;j<=m;j++) {
if(!board[i][j]) answer += 1;
}
}
System.out.print(answer);
}
public static void queen(int r, int c) {
//상하
int nx = r;
while(true) {
nx -= 1;
if(nx < 1) break;
Point point = new Point(nx, c);
if(k.contains(point) || p.contains(point)) break;
board[nx][c] = true;
}
nx = r;
while(true) {
nx += 1;
if(nx > n) break;
Point point = new Point(nx, c);
if(k.contains(point) || p.contains(point)) break;
board[nx][c] = true;
}
//좌우
int ny = c;
while(true) {
ny -= 1;
if(ny < 1) break;
Point point = new Point(r, ny);
if(k.contains(point) || p.contains(point)) break;
board[r][ny] = true;
}
ny = c;
while(true) {
ny += 1;
if(ny > m) break;
Point point = new Point(r, ny);
if(k.contains(point) || p.contains(point)) break;
board[r][ny] = true;
}
//우하향 대각선
nx = r;
ny = c;
while(true) {
nx += 1;
ny += 1;
if(nx > n || ny > m) break;
Point point = new Point(nx, ny);
if(k.contains(point) || p.contains(point)) break;
board[nx][ny] = true;
}
nx = r;
ny = c;
while(true) {
nx -= 1;
ny -= 1;
if(nx < 1 || ny < 1) break;
Point point = new Point(nx, ny);
if(k.contains(point) || p.contains(point)) break;
board[nx][ny] = true;
}
//좌하향 대각선
nx = r;
ny = c;
while(true) {
nx += 1;
ny -= 1;
if(nx > n || ny < 1) break;
Point point = new Point(nx, ny);
if(k.contains(point) || p.contains(point)) break;
board[nx][ny] = true;
}
nx = r;
ny = c;
while(true) {
nx -= 1;
ny += 1;
if(nx < 1 || ny > m) break;
Point point = new Point(nx, ny);
if(k.contains(point) || p.contains(point)) break;
board[nx][ny] = true;
}
}
}