[Java] ๋ฐฑ์ค 2564๋ฒ - ๊ฒฝ๋น์
https://www.acmicpc.net/problem/2564
2564๋ฒ: ๊ฒฝ๋น์
์ฒซ์งธ ์ค์ ๋ธ๋ก์ ๊ฐ๋ก์ ๊ธธ์ด์ ์ธ๋ก์ ๊ธธ์ด๊ฐ ์ฐจ๋ก๋ก ์ฃผ์ด์ง๋ค. ๋์งธ ์ค์ ์์ ์ ๊ฐ์๊ฐ ์ฃผ์ด์ง๋ค. ๋ธ๋ก์ ๊ฐ๋ก์ ๊ธธ์ด์ ์ธ๋ก์ ๊ธธ์ด, ์์ ์ ๊ฐ์๋ ๋ชจ๋ 100์ดํ์ ์์ฐ์์ด๋ค. ์ด์ด ํ ์ค
www.acmicpc.net
[๋ฌธ์ ํ์ด]
๊ฐ ๋ฐฉํฅ๋ณ๋ก ๊ฑฐ๋ฆฌ๋ฅผ ๊ตฌํ ๋ ๊ณ ๋ คํด์ผ๋ ์์๋ค์ด ๋ง์์ ๋ค์ ๋ณต์กํ๋ ๊ตฌํ๋ฌธ์ ์์ต๋๋ค.
๊ตฌํ์ ์ฝ๊ฒ ํ๊ธฐ ์ํด ์ผ์ชฝ๊ณผ ์์ชฝ ๊ฒฝ๊ณ๋ก๋ถํฐ์ ๊ฑฐ๋ฆฌ๊ฐ์ ๋ถ์ ์ผ์ชฝ, ๋์ ์์ชฝ, ๋จ์ ์ค๋ฅธ์ชฝ, ์๋ ์๋์ชฝ์ผ๋ก ๊ฑฐ๋ฆฌ๋ฅผ ์ ์ฅํ์ต๋๋ค.
๊ทธ๋ฆผ์ผ๋ก ์ค๋ช ํด ๋ณด์๋ฉด ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
shop๋ฐฐ์ด์ ์์ ๊ฐ์ ๋งํผ ๋ฐฉํฅ๊ฐ๊ณผ, ์์ ๊ทธ๋ฆผ ๋ฐฉํฅ ๊ธฐ์ค์ผ๋ก ๊ฑฐ๋ฆฌ๊ฐ์ ์ ์ฅํ ํ
shop์ ๋ง์ง๋ง ๋ฐฐ์ด์ ๋๊ทผ์ด์ ๋ฐฉํฅ๊ฐ๊ณผ ๊ฑฐ๋ฆฌ๋ฅผ ์ ๋ ฅ๋ฐ์ต๋๋ค.
๊ทธ๋ฐ ๋ค์, ๋๊ทผ์ด๊ฐ ์๋ ๋ฐฉํฅ์ด ๋ฐ๋ฉด์ด๋ผ๊ณ ๊ฐ์ ํ์๋, ๊ฐ ์์ ๊ณผ์ ๊ฑฐ๋ฆฌ ์ต์๊ฐ์ ๊ตฌํฉ๋๋ค.
- ์ผ์ชฝ ์์ : ์์ ๊ฑฐ๋ฆฌ + ๊ฐ๋ก๋ณ - ๋๊ทผ์ด์ ๊ฑฐ๋ฆฌ
- ์ค๋ฅธ์ชฝ ์์ : ์ธ๋ก๋ณ - ์์ ๊ฑฐ๋ฆฌ + ๋๊ทผ์ด์ ๊ฑฐ๋ฆฌ
- ์์ชฝ ์์ : ์์ ๊ฑฐ๋ฆฌ + ๊ฐ๋ก๋ณ - ๋๊ทผ์ด์ ๊ฑฐ๋ฆฌ + ์ธ๋ก๋ณ
- ์๋ซ์ชฝ ์์ : ์์ ๊ฑฐ๋ฆฌ - ๋๊ทผ์ด์ ๊ฑฐ๋ฆฌ์ ์ ๋๊ฐ
[์ ๋ต ์ฝ๋]
import javax.swing.text.Style;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.StringTokenizer;
public class Main {
public static int N;
public static int M;
public static int[][] shop;
public static int count = 0;
public static int direction = 0;
public static int dist = 0;
// ๊ธฐ์ค์ ์ ๊ฐ๋ก
public static int width = 0;
// ๊ธฐ์ค์ ์ ๋์ด
public static int height = 0;
public static HashMap<Integer, String> map = new HashMap<>();
// ๋ฑกํฅ ์ ๋ณด ์ ์ฅ
// 1 ๋ถ , 2 ๋จ, 3 ์, 4 ๋
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
N = Integer.parseInt(st.nextToken());
M = Integer.parseInt(st.nextToken());
count = Integer.parseInt(br.readLine());
// ๋ง์ง๋ง์๋ ๊ธฐ์ค์ ์ ์ฅํจ
shop = new int[count + 1][2];
for(int i=0;i<count + 1;i++) {
st = new StringTokenizer(br.readLine());
shop[i][0] = Integer.parseInt(st.nextToken());
shop[i][1] = Integer.parseInt(st.nextToken());
if(shop[i][0] == 2) {
shop[i][1] = N - shop[i][1];
}
if(shop[i][0] == 3) {
shop[i][1] = M - shop[i][1];
}
}
direction = shop[count][0];
dist = shop[count][1];
getDirection(direction);
getTotalDist();
}
public static void getDirection(int index) {
if(index == 1 || index == 2) {
width = N;
height = M;
} else {
width = M;
height = N;
}
if(index == 1) {
map.put(4,"left");
map.put(3,"right");
map.put(2,"top");
map.put(1,"bottom");
return;
}
if(index == 2) {
map.put(3,"left");
map.put(4,"right");
map.put(1,"top");
map.put(2,"bottom");
return;
}
if(index == 3) {
map.put(1,"left");
map.put(2,"right");
map.put(4,"top");
map.put(3,"bottom");
return;
}
if(index == 4) {
map.put(2,"left");
map.put(1,"right");
map.put(3,"top");
map.put(4,"bottom");
return;
}
}
public static void getTotalDist() {
int result = 0;
int total = 2*N + 2*M;
// ๊ฐ๊ฐ์ shop์ ๋ฃ๊ณ ์ต๋จ ๊ฑฐ๋ฆฌ ๊ตฌํ๊ธฐ
for(int i=0;i<count;i++) {
String dir = map.get(shop[i][0]);
int d = 0;
if(dir.equals("left")) {
d = shop[i][1] + width - dist;
}
else if(dir.equals("right")) {
d = height - shop[i][1] + dist;
}
else if(dir.equals("top")) {
d = shop[i][1] + width - dist + height;
} else if(dir.equals("bottom")) {
d = Math.abs(shop[i][1] - dist);
}
result += Math.min(d, total - d);
}
System.out.println(result);
}
}
/*
1
3 4
2
*/