ํฐ์คํ ๋ฆฌ ๋ทฐ
https://www.acmicpc.net/problem/2564
[๋ฌธ์ ํ์ด]
๊ฐ ๋ฐฉํฅ๋ณ๋ก ๊ฑฐ๋ฆฌ๋ฅผ ๊ตฌํ ๋ ๊ณ ๋ คํด์ผ๋ ์์๋ค์ด ๋ง์์ ๋ค์ ๋ณต์กํ๋ ๊ตฌํ๋ฌธ์ ์์ต๋๋ค.
๊ตฌํ์ ์ฝ๊ฒ ํ๊ธฐ ์ํด ์ผ์ชฝ๊ณผ ์์ชฝ ๊ฒฝ๊ณ๋ก๋ถํฐ์ ๊ฑฐ๋ฆฌ๊ฐ์ ๋ถ์ ์ผ์ชฝ, ๋์ ์์ชฝ, ๋จ์ ์ค๋ฅธ์ชฝ, ์๋ ์๋์ชฝ์ผ๋ก ๊ฑฐ๋ฆฌ๋ฅผ ์ ์ฅํ์ต๋๋ค.
๊ทธ๋ฆผ์ผ๋ก ์ค๋ช ํด ๋ณด์๋ฉด ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
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
*/
'Algorithm > Baekjoon' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Java] ๋ฐฑ์ค 1931๋ฒ - ํ์์ค ๋ฐฐ์ (0) | 2022.06.13 |
---|---|
[Java] ๋ฐฑ์ค 2461๋ฒ - ๋ํ ์ ์ (0) | 2022.06.05 |
[Java] ๋ฐฑ์ค 2056๋ฒ -์์ (0) | 2022.06.05 |
[Java] ๋ฐฑ์ค 19237๋ฒ - ์ด๋ฅธ ์์ด (0) | 2022.05.29 |
[Java] ๋ฐฑ์ค 1516๋ฒ - ๊ฒ์ ๊ฐ๋ฐ (1) | 2022.05.29 |
- Total
- Today
- Yesterday
- ํ๋กํ ์ฝ
- Baekjoon
- ํ๋ก๊ทธ๋๋จธ์ค
- ์ด๋ถํ์
- ์นด์นด์ค ์ธํด
- ๋ค์ด๋๋ฏน ํ๋ก๊ทธ๋๋ฐ
- ๋ชจ๋ ์๋ฐ์คํฌ๋ฆฝํธ deep dive
- http
- ๋ฐฑ์ค
- ํจ์ํ ํ๋ก๊ทธ๋๋ฐ
- ์ด์์ฒด์
- fp
- ๋์์ธ ํจํด
- ๋ ์์ปฌ ํ๊ฒฝ
- ๋คํธ์ํฌ
- ๋ฐฑ์ค javascript
- ์๋ฐ
- ํฌํฌ์ธํฐ
- JavaScript
- ์ ์ญ ๋ณ์
- map
- TDD
- ๊ฐ์ฒด์งํฅ ํ๋ก๊ทธ๋๋ฐ
- ํ๋กํผํฐ
- 2019 ์นด์นด์ค ๊ฐ๋ฐ์ ๊ฒจ์ธ ์ธํด
- ์ฝ๋ฉํ ์คํธ
- ์๋ฐ์คํฌ๋ฆฝํธ
- ์๊ณ ๋ฆฌ์ฆ
- git
- ๋ฐฑ์ค node.js
์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | 7 |
8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 | 16 | 17 | 18 | 19 | 20 | 21 |
22 | 23 | 24 | 25 | 26 | 27 | 28 |
29 | 30 | 31 |