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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | import java.util.* data class dot(var x: Int, var y: Int) { } fun main(){ val(n,m)= readLine()!!.split(" ").map{it.toInt()} val graph = mutableListOf<MutableList<Int>>() for(i in 0 until n){ graph.add(readLine()!!.map{it.toInt()-48}.toMutableList()) } // println(graph) val dx = mutableListOf(-1,1,0,0) val dy = mutableListOf(0,0,-1,1) fun bfs(x:Int,y:Int): Int { var x =x var y = y var q : Queue<dot> = LinkedList<dot>() q.add(dot(x,y)) while(!q.isEmpty()){ val(x,y)= q.poll() // println(d.x) // println(d.y) for(i in 0 until 4){ var nx = x+dx[i] var ny = y+dy[i] if(nx<0 || nx >= n || ny<0 || ny>=m){ continue } if(graph[nx][ny]==0) continue if(graph[nx][ny]==1){ graph[nx][ny] = graph[x][y]+1 //println("x: $x y: $y nx: $nx ny: $ny graph[x][y]: " + // "${graph[x][y]} graph[nx][ny]: ${graph[nx][ny]}") q.add(dot(nx,ny)) // println(q) } } } //println(graph) return graph[n-1][m-1] } print(bfs(0,0)) } | cs |
'알고리즘 공부 > bfs,dfs' 카테고리의 다른 글
백준 11724 with Kotlin (0) | 2021.08.06 |
---|---|
백준2606번 with Kotlin (0) | 2021.08.05 |
백준 1260번 with 코틀린 (0) | 2021.07.23 |
이코테 문제 #dfs # 001을 =[0,0,1]의 list로 만드는 방법 (0) | 2021.07.14 |
dfs bfs (0) | 2021.07.14 |