๋ฌธ์ : https://www.acmicpc.net/problem/15685
ํ์ด๊ณผ์
1. ๋๋๊ณค ์ปค๋ธ๋ฅผ ๊ทธ๋ฆฌ๋ ํจ์๋ฅผ ๊ตฌํํด์ผ ํฉ๋๋ค.
1-1. ๋ฆฌ์คํธ๋ฅผ ํ์ฉ
๋๋๊ณค ์ปค๋ธ๋ ํ์ฌ๊น์ง ๋ง๋ค์ด์ง ๋ชจ์์ ๋ฐํ์ผ๋ก ์๋ก์ด ๋ชจ์์ ๋ง๋ค๊ฒ ๋ฉ๋๋ค. ๋ฆฌ์คํธ๋ฅผ ํ์ฉํด์ ๋๋๊ณค ์ปค๋ธ์ ๋ฐฉํฅ์ ๊ตฌํํ์ต๋๋ค.
1-2. 90๋ ํ์
90๋ ํ์ ์ ๊ฒฝ์ฐ
0 ๋ฐฉํฅ์ 1
1 ๋ฐฉํฅ์ 2
2 ๋ฐฉํฅ์ 3
3 ๋ฐฉํฅ์ 0
์ด ๋ฉ๋๋ค.
(ํ์ฌ๋ฐฉํฅ+1)%4 ์ฐ์ฐ์ ํตํด ์ด๋ฅผ ๊ตฌํํ ์ ์์ต๋๋ค.
static void update_history(){ // g๋ฒ ์ด๋์ํค๊ธฐ
for(int i=0;i<g;i++){
int size = history.size();
for(int j=size-1;j>=0;j--){
history.add((history.get(j)+1)%4);
}
}
}
1-3. ๋ ๋ฒ ์ด๋ ์ํค๊ธฐ
์ ์ฌ๊ฐํ์ ๊ฐฏ์๋ฅผ ๊ตฌํ๊ธฐ ์ํด ๋๋๊ณค ์ปค๋ธ๋ฅผ ๊ทธ๋ฆด ๋ ํ ๋ฒ์ ์ด๋์ ๋ ๋ฒ์ ์ด๋์ผ๋ก ๊ตฌํํ์ต๋๋ค.
2์นธ ์ฉ ์ด๋ํ๊ฒ ๋๋ฉด ๋๋๊ณค ์ด๋์ ๋ฐ๋ฅธ ๋น ์ ์ฌ๊ฐํ ์นธ์ ๊ตฌํํ ์ ์๊ธฐ ๋๋ฌธ์ ๋๋ค.
์ด๋ฅผ ์ํด์ ๋ฐฐ์ด์ ํฌ๊ธฐ๋ฅผ ๋ฌธ์ ์ ์กฐ๊ฑด์ธ (0 ≤ x ≤ 100, 0 ≤ y ≤ 100) *2๋ฐฐ ํฌ๊ธฐ๋ก ์ด๊ธฐํ ํ์ต๋๋ค.
์์์ ์ ์ขํ ๋ํ 2๋ฐฐ ์์ผ์คฌ์ต๋๋ค.
static void draw(){
x*=2;
y*=2;
// ์ด๋ ๋ฐฐ์ด์์ rํ์ x์ขํ, c์ด์ y์ขํ
arr[y][x] = 1;
for(int i=0;i<history.size();i++){
int d = history.get(i);
for(int c=0;c<2;c++){ // 2์นธ์ฉ ์ด๋ ์ํค๊ธฐ (์ ์ฌ๊ฐํ์ ๊ฐฏ์๋ฅผ ๊ตฌํ๊ธฐ ์ํด์)
x += dr[d];
y += dc[d];
arr[y][x] = 1;
}
}
}
2. ์ ์ฌ๊ฐํ์ ๊ฐฏ์๋ฅผ ๊ตฌํด์ผ ํฉ๋๋ค.
๋๋๊ณค ์ปค๋ธ๊ฐ ๊ทธ๋ ค์ง ๋ฐฐ์ด๋ค์ ์์ ํ์ ํ๋ฉฐ ๋ค ๋ฐฉํฅ์ ๋๊ฐ์ ์ 1์ด ์๋์ง ํ์ธํ๋ฉฐ ๊ฐฏ์๋ฅผ ์นด์ดํ ํฉ๋๋ค.
์ด๋ ์ ์ฌ๊ฐํ์ด ์์ ์ ์๋ ์ขํ๋ ํ์ ์ขํ์ ๋๋ค.
์์์ ์ x,y์ขํ (1-3 ๊ณผ์ ์์ 2๋ฐฐ ์์ผ์ค) ๋ ์ง์์ด๊ณ , ์ด๋ํ๋ ํ์๋ ์ง์์ด๊ธฐ ๋๋ฌธ์ ๋๋ค.
static int count(){
int result = 0;
for(int r=1;r<201;r+=2){ // ํ๊ณผ ์ด์ด ํ์์ธ ์นธ์ ๋ํด์ ํ์ (์์์ ์ ๋ฌด์กฐ๊ฑด ์ง์์ด๊ณ ์ง์ ๊ฐฏ์๋ก ์ด๋ํจ)
for(int c=1;c<201;c+=2){
int cnt = 0;
if(arr[r][c]!=0) continue; // ํ์ํ๋ ์นธ์ด 0์ด์ด์ผ ํจ
for(int d=0;d<4;d++){
int nr = r + ddr[d];
int nc = c + ddc[d];
if(nr<0 || nr>=201) continue;
if(nc<0 || nc>=201) continue;
if(arr[nr][nc]==0) break; // ํ๋๋ผ๋ 0์ด๋ฉด ๋ ์ ์์
if(arr[nr][nc]==1) cnt+=1;
}
if(cnt==4){
result+=1;
}
}
}
return result;
}
์ถ๋ ฅ๊ณผ ๊ฐ์ด ๋๋๊ณค ์ปค๋ธ๋ฅผ ๊ทธ๋ ธ์ต๋๋ค.
์๊ฒ๋์
- ๋ค๋ฅธ์ฌ๋๋ค์ ํ์ด๋ฅผ ๋ณด๋ ์ง์ ๋๋๊ณค ์ปค๋ธ๋ฅผ ๊ทธ๋ฆฌ์ง ์๊ณ , ๊ผญ์ง์ ์ ๋ํด์๋ง ์ฒดํฌ๋ฅผ ํ์ฌ ๊ตฌํํ ์ ์์์ ์๊ฒ๋์์ต๋๋ค.
- ํ ๋ฒ์ ์ด๋์ ๋ ๋ฒ์ ์ด๋์ผ๋ก ๋ณด์ ํ๋ ๋ฐฉ์์ผ๋ก ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ ์ ์๋ ๊ฒฝ์ฐ๊ฐ ์ข ์ข ์์์ ๋๊ผ์ต๋๋ค.
์ ์ฒด์ฝ๋
package com.mincheolsong;
import java.io.*;
import java.util.*;
// 1. ๋๋๊ณค ์ปค๋ธ ๊ทธ๋ฆฌ๊ธฐ
// - ์ด์ ๋ชจ์์ ๋ฐ๋ผ์ ๊ณ์ ์๋ก์ด ๋ชจ์์ด ๋ง๋ค์ด ์ง
// - List๋ฅผ ์คํ์ผ๋ก ํ์ฉํด์ ํ์ฌ ๋ชจ์์ ๊ธฐ๋ฐ์ผ๋ก ๊ณ์ ์
๋ฐ์ดํธ ํ๊ธฐ
// - ์ด๋ 90๋ ํ์ ํ์ ๋ ๋ฐฉํฅ
// - 0 : 1
// - 1 : 2
// - 2 : 3
// - 3 : 0
// 2. ์ ์ฌ๊ฐํ์ ๊ฐ์ ๊ตฌํ๊ธฐ
public class Main {
static int N;
static int[] dr = {1,0,-1,0}; // 0, 1, 2, 3
static int[] dc = {0,-1,0,1};
static int[] ddr = {-1,-1,1,1};
static int[] ddc = {-1,1,1,-1};
static List<Integer> history;
static int x,y,d,g;
static int[][] arr;
static void update_history(){ // g๋ฒ ์ด๋์ํค๊ธฐ
for(int i=0;i<g;i++){
int size = history.size();
for(int j=size-1;j>=0;j--){
history.add((history.get(j)+1)%4);
}
}
}
static void draw(){
x*=2;
y*=2;
// ์ด๋ ๋ฐฐ์ด์์ rํ์ x์ขํ, c์ด์ y์ขํ
arr[y][x] = 1;
for(int i=0;i<history.size();i++){
int d = history.get(i);
for(int c=0;c<2;c++){ // 2์นธ์ฉ ์ด๋ ์ํค๊ธฐ (์ ์ฌ๊ฐํ์ ๊ฐฏ์๋ฅผ ๊ตฌํ๊ธฐ ์ํด์)
x += dr[d];
y += dc[d];
arr[y][x] = 1;
}
}
}
static int count(){
int result = 0;
for(int r=1;r<201;r+=2){ // ํ๊ณผ ์ด์ด ํ์์ธ ์นธ์ ๋ํด์ ํ์ (์์์ ์ ๋ฌด์กฐ๊ฑด ์ง์์ด๊ณ ์ง์ ๊ฐฏ์๋ก ์ด๋ํจ)
for(int c=1;c<201;c+=2){
int cnt = 0;
if(arr[r][c]!=0) continue; // ํ์ํ๋ ์นธ์ด 0์ด์ด์ผ ํจ
for(int d=0;d<4;d++){
int nr = r + ddr[d];
int nc = c + ddc[d];
if(nr<0 || nr>=201) continue;
if(nc<0 || nc>=201) continue;
if(arr[nr][nc]==0) break; // ํ๋๋ผ๋ 0์ด๋ฉด ๋ ์ ์์
if(arr[nr][nc]==1) cnt+=1;
}
if(cnt==4){
result+=1;
}
}
}
return result;
}
public static void main(String[] args) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = null;
N = Integer.parseInt(br.readLine());
arr = new int[201][201];
for(int i=0;i<N;i++){
st = new StringTokenizer(br.readLine());
history = new ArrayList<>(); // ์ด๋๋ฐฉํฅ ๊ธฐ๋ก
x = Integer.parseInt(st.nextToken());
y = Integer.parseInt(st.nextToken());
d = Integer.parseInt(st.nextToken());
g = Integer.parseInt(st.nextToken());
history.add(d); // ์์๋ฐฉํฅ ๋ฃ์ด์ค
update_history(); // g์ธ๋ ๋งํผ ์ด๋ํ์ ๋ history๋ฅผ ์
๋ฐ์ดํธ
draw(); // history๋ก ๊ทธ๋ฆผ ๊ทธ๋ ค์ฃผ๊ธฐ, 2์นธ ์ฉ ์ด๋์ํค๊ธฐ (์ ์ฌ๊ฐํ ๊ผญ์ง์ ๋ง๋ฟ์ ๋ถ๋ถ ๊ณ์ฐํ๊ธฐ ์ํด์)\
}
// ์ ์ฌ๊ฐํ ๊ฐฏ์ ์นด์ดํธ
System.out.println(count());
}
}
'๐์ฝ๋ฉํ ์คํธ:CodingTest' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
๋ฐฐ์ด ์๊ณ๋ฐฉํฅ ํ์ ๊ณต์ (1) | 2024.04.06 |
---|---|
ํฌํฌ์ธํฐ ์๊ณ ๋ฆฌ์ฆ (0) | 2024.04.05 |
[ํ๋ก๊ทธ๋๋จธ์ค] ํ๋ก์ธ์ค (์ฐ์ ์์ํ) (0) | 2024.04.02 |
[ํ๋ก๊ทธ๋๋จธ์ค] ์์ (0) | 2024.04.01 |
[ํ๋ก๊ทธ๋๋จธ์ค] ๋ฐฉ์ ๊ฐ์ (0) | 2024.03.29 |