TIL (Today I Learned)/Algorithm
[๋ฐฑ์ค] 1074๋ฒ Z
loki d
2021. 12. 11. 19:57
728x90
1074๋ฒ Z
๋ฌธ์

์ ๋ ฅ
์ฒซ์งธ ์ค์ ์ ์ N, r, c๊ฐ ์ฃผ์ด์ง๋ค.
3 7 7
์ถ๋ ฅ
rํ c์ด์ ๋ช ๋ฒ์งธ๋ก ๋ฐฉ๋ฌธํ๋์ง ์ถ๋ ฅํ๋ค.
63
์ค๋ช
- ํ๋์ฉ divide & conquer๋ฅผ ํ๋ฉด ์๊ฐ์ด๊ณผ...
- 4๊ฐ๋ก ๋๋๊ณ ๋จ์๋ฉด์ ์ ์ด์ฉํด์ ๋ฌธ์ ํ์ด
- r,c์ ์์น์ ๋ฐ๋ผ ๋จ์๋ฉด์ ์ ๋ํด์ฃผ๋ ๋ฐฉ์์ผ๋ก count๋ฅผ ๋ํด์ค๋ค.
์์ค์ฝ๋
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer;
public class Main{
private static int N, r, c;
private static int count = 0;
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());
r = Integer.parseInt(st.nextToken());
c = Integer.parseInt(st.nextToken());
int n = (int)Math.pow(2, N);
int x=0, y=0;
while(n > 1) {
n /= 2;
// 1 ์ผ์ชฝ ์
if(r < x+n && c < y+n) {
}
// 2 ์ค๋ฅธ์ชฝ ์
else if(r < x+n && c >= y+n) {
count += n * n * 1;
y += n; //์ค๋ฅธ์ชฝ ์๋ก ์์น ์ด๋
}
// 3 ์ผ์ชฝ ์๋
else if(r>= x+n && c < y+n) {
count += n * n * 2;
x += n;
}
// 4 ์ค๋ฅธ์ชฝ ์๋
else {
count += n * n * 3;
x += n;
y += n;
}
}
System.out.println(count);
}
}