[BOJ]1697 ์จ๋ฐ๊ผญ์ง(Catch that cow)๐ฎ (kotlin,java)
โ๋ฌธ์ โPermalink
๐Solution๐Permalink
์ฒ์ ๋ฌธ์ ๋ฅผ ๋ดค์๋๋ ๋ฐฑํธ๋ํน ๋ฌธ์ ์ธ์ค ์๊ณ ๋ฐฑํธ๋ํน์ผ๋ก ์ด์ฌํ ํ์๋๋ฐ ์๊ฐ์ด๊ณผ๊ฐ ๋ฌ๋ค.
๊ณ์ ๋ฐฑํธ๋ํน์ผ๋ก ๋์ ํ๋ค๊ฐ ๊ฒฐ๊ตญ ๋ฌธ์ ๋ถ๋ฅ๋ฅผ ๋ดค๋๋ฐ BFS๋ฌธ์ ์๋ค..ใ
Try1) BFS๋ก ํ์ด + ๋ฐฑํธ๋ํนPermalink
์ฒ์์๋ ์๋น์ ์์น์ธ n์ ๊ธฐ์ค์ผ๋ก bfs๋ฅผ ํ๋๋ฐ ์๊ฐ ์ด๊ณผ์ ๊ฑธ๋ ธ๋ค..
์๊ฐ์ ์ค์ผ ์ ์๋ ๋ฐฉ๋ฒ์ ๋ํด ์๊ฐํ๋ค๊ฐ ์ฒ์์ ์๊ฐํ๋ ๋ฐฑํธ๋ํน์ ์ ์ฉํ๋ฉด ์ฐ์ฐ์ด ์ข ๋นจ๋ผ์ง์ง ์์๊น ํด์ ์ ์ฉํด๋ดค๋๋ฐ ์๊ฐ๋ณด๋ค ๋น ๋ฅด์ง ์์๋ ๊ฒ ๊ฐ๋ค..ใ
๊ฒฐ๊ตญ ์ด๊ฒ๋ ์๊ฐ์ด๊ณผ๋ฅผ ๋ฐ์๋คใ ใ
fun bfs(n:Int,k:Int):Int{
val queue=mutableListOf<Pair<Int,Int>>()//depth, n
var time=0
queue.add(0 to k)
while(!queue.isEmpty()){
// val mark=Array<Boolean>(k*2){false}
val current=queue.first()
queue.removeFirst()
if(current.second==n){
time=current.first
break
}
else{
if(current.second>n){
if(current.second%2==0) {
queue.add(current.first + 1 to current.second / 2)
}
queue.add(current.first+1 to current.second+1)
queue.add(current.first+1 to current.second-1)
}else{
queue.add(current.first+1 to current.second+1)
}
}
}
return time
}
Try2) BFS + ์ค๋ณต๊ฒ์ฌPermalink
๊ฒฐ๊ตญ ์์์ ์๋ฉ ๋ป์ง์ ํ๋ค๊ฐ ๋ชป ํ์ด์ ๊ตฌ๊ธ๋ง์ ํด์ ํ์ด๋ฅผ ํ๊ฒ ๋์๋ค.
BFS์ ์ค๋ณต ๊ฒ์ฌ๋ฅผ ๊ฐ์ด ํด์ ํ์ ์๋ ์ค๋ณต ์ฐ์ฐ์ ์ค์ด๋ ๋ฐฉ์์ผ๋ก ์ฐ์ฐ ์๊ฐ์ ์ค์๋ค.
์ฒซ ๋ฒ์งธ ์๋์์ ์ค๋ณต ๊ฒ์ฌ๋ฅผ ์๋ํด๋ณด๊ธฐ๋ ํ์๋๋ฐ ๋ฐฑํธ๋ํน์ ํ๋ฉด์ ํ๋ ค๊ณ ํ๋๊น ์๊พธ ํท๊ฐ๋ ค์ ๊ทธ๋ฅ n ๋ถํฐ ๊ฒ์ฌํ๋ ๊ฑธ๋ก ์์ ํ๋ค.
์ฃผ์ํด์ผ ํ๋ ๋ถ๋ถ์ n>k์ผ ๋์ด๋ค! ๋๋ ์ฒ์์ ์ด ๋ถ๋ถ์ ์ฒ๋ฆฌ ์ ํด๋๊ณ ArrayIndexOutOfBounds๊ฐ ๋์ค๋๊น ๋ก์ง์ ๋ฌธ์ ๊ฐ ์๋ ์ค ์๊ณ ๊ดํ ์ด๊ฒ์ ๊ฒ ๊ณ ์น๋ฉด์ 30๋ถ์ ๋ ๋ ธ๋ค.ใ
fun bfs2(n:Int,k:Int):Int{
if(n<k){
val mark=Array(k*2+1){false}
val dist=Array(k*2+1){0}
val queue= mutableListOf<Int>()
queue.add(n)
mark[n]=true
while(queue.isNotEmpty()){
val current=queue.first()
queue.removeFirst()
if(current==k) break;
if(current*2<dist.size&&!mark[current*2]) {
queue.add(current * 2)
dist[current*2]=dist[current]+1
mark[current*2]=true
}
if(current>0&&!mark[current-1]) {
queue.add(current - 1)
dist[current-1]=dist[current]+1
mark[current-1]=true
}
if(current+1<dist.size&&!mark[current+1]) {
queue.add(current + 1)
dist[current+1]=dist[current]+1
mark[current+1]=true
}
}
return dist[k]
}else return n-k
}
๐ Language Change(java)๐ Permalink
์๋ฐ๋ก ๋ค์ ์ฝ๋ ์ง๋ ๊ฒ ๊ท์ฐฎ๊ธด ํ๋ฐ ํ๋ก์ฐ๊ฐ ํ๋ฒ ์ ๋ฆฌ๋๋ ๋๋์ด๋ผ์ ์ข๋ค!
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.Queue;
public class S1_1697_catch_that_cow {
public static void main(String[] args) throws IOException {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
StringBuilder builder=new StringBuilder();
String[] input=br.readLine().split(" ");
int n=Integer.parseInt(input[0]);
int k=Integer.parseInt(input[1]);
builder.append(bfs(n,k));
System.out.println(builder);
}
static int bfs(int n,int k){
if(n<k){
Queue<Integer> queue=new LinkedList<>();
int[] dist=new int[k*2+1];
boolean[] mark=new boolean[k*2+1];
dist[n]=0;
mark[n]=true;
queue.add(n);
while(!queue.isEmpty()){
int current=queue.peek();
queue.remove();
if(current==k)break;
if(current*2<dist.length&&!mark[current*2]){
queue.add(current*2);
mark[current*2]=true;
dist[current*2]=dist[current]+1;
}
if(current+1<dist.length&&!mark[current+1]){
queue.add(current+1);
mark[current+1]=true;
dist[current+1]=dist[current]+1;
}
if(current>0&&!mark[current-1]){
queue.add(current-1);
mark[current-1]=true;
dist[current-1]=dist[current]+1;
}
}
return dist[k];
}else return n-k;
}
}
๐Advanced๐Permalink
[kotlin]
-
IntArray๋ฅผ ์ฌ์ฉํ๋ฉด ์ด๊ธฐํ ํ์ง ์๊ณ ํฌ๊ธฐ๋ง ์๋ ๋ฐฐ์ด์ ์ ์ธํ ์ ์๋ค.
IntArray(10)
- ํ .. ์ด๊ธฐํ ์ํ๊ณ ์ฌ์ฉํ๋ฉด ์คํ์๊ฐ์ด ์งง์์ง ์ค ์์๋๋ฐ ์ ์ง ์ข ๋์ด๋ฌ๋ค.. (780msโ784ms)
- BooleanArray๋ ์๊ธธ๋ ์ด๊ฒ๋ ์ ์ฉํ๋ฉด ๋ ๋์ด๋๋ค(780msโ796ms)
- GPT๋ง๋ก๋ IntArray๊ฐ ๊ฐ์ฒด๋ฅผ ๋ฐ์ฑํ์ง ์์์ ๋ฉ๋ชจ๋ฆฌ ๊ณต๊ฐ๊ณผ ์๊ฐ์ ์ธก๋ฉด์์ ํจ์จ์ ์ด๋ผ๊ณ ํ๋๋ฐ ํน์ ์ํฉ(์ปดํ์ผ๋ฌ,์บ์ํจ๊ณผ,ํ ์คํธํ๊ฒฝ)์ ๋ฐ๋ผ ๊ฒฐ๊ณผ๊ฐ ๋ค๋ฅผ ์ ์๋ค๊ณ ํ๋ค.
- ํ์คํ ๋ฉ๋ชจ๋ฆฌ๋ ๋ฐ์ฑ์ ์ํ๋ IntArray๊ฐ ๋ ์ฐ๋ ๊ฒ ๊ฐ๋ค(27632kbโ23960kb)
- ์๊ฐ ์ธก๋ฉด์ ๋์ค์ ๋ ๋น๊ตํด ๋ณผ ์ผ์ด ์์ผ๋ฉด ํ๋ฒ ๋ ํ์ธํด ๋ด์ผ๊ฒ ๋ค.
- ํ ๋๋ฆ ์ด๋ ์ ๋๋ ์ฝํ๋ฆฐ์ ์ ์ฐ๊ณ ์๋ ์ค ์์๋๋ฐ ์๊ฐ๋ณด๋ค ํ์ค์ ์์ฃผํด ์์๋ ๊ฒ ๊ฐ๋ค.. ๋ ์ฌ๋ฌ๊ฐ์ง ๋ฐฉ๋ฒ์ผ๋ก ์ฝ๋๋ฅผ ์งค ์ ์๊ฒ ๋ฌธ๋ฒ๊ณต๋ถ๋ฅผ ํํํ ํด์ผ๊ฒ ๋ค!
- ํ๋ฅผ list๋ก ๋ง๋ค์ง ์๊ณ front, real๋ก ์ธ๋ฑ์ฑํ๋ฉด์ ์๊ณ ๋ฆฌ์ฆ์ ์งค ์ ๋ ์๋ค.
๋๊ธ๋จ๊ธฐ๊ธฐ