sanya5791

The Maze

Jul 4th, 2021 (edited)
321
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 3.31 KB | None | 0 0
  1. /**
  2. maze = [[0,0], [0,0]], start = [0,0], destination = [1,1]
  3. **/
  4.  
  5. class Solution {
  6.     fun hasPath(maze: Array<IntArray>, start: IntArray, destination: IntArray): Boolean {
  7.         val startNode = createGraph(maze, Node.Id(row = start[0], col = start[1]))
  8.        
  9.         val destination: Node? = findDestination(startNode, Node.Id(row = destination[0], col = destination[1]))
  10.         return destination != null
  11.     }
  12.    
  13.     private fun findDestination(root: Node, destinationId: Node.Id): Node? {
  14.         val visited = mutableSetOf<Node.Id>()
  15.         val queue: Queue<Node> = LinkedList()
  16.        
  17.         queue.offer(root)
  18.         while(queue.isNotEmpty()) {
  19.             val node = queue.poll()
  20.             if(visited.contains(node.id)) continue
  21.            
  22.             if(node.id == destinationId) return node
  23.            
  24.             node.nodes.forEach { queue.offer(it) }
  25.             visited.add(node.id)            
  26.         }
  27.        
  28.         return null
  29.     }
  30.    
  31.     private fun createGraph(maze: Array<IntArray>, rootId: Node.Id): Node {
  32.         val visited = mutableSetOf<Node.Id>()
  33.         val queue: Queue<Node> = LinkedList()
  34.        
  35.         val root = Node(Node.Id(row = rootId.row, col = rootId.col))
  36.         queue.offer(root)
  37.        
  38.         while(queue.isNotEmpty()) {
  39.             val node = queue.poll()
  40.             if(visited.contains(node.id)) continue
  41.            
  42.             node.addNeighboursIfNeed(maze)
  43.            
  44.             node.nodes.forEach { queue.offer(it) }
  45.             visited.add(node.id)
  46.         }
  47.        
  48.         return root
  49.     }
  50.    
  51.     private fun Node.addNeighboursIfNeed(maze: Array<IntArray>) {
  52.         addTopIfNeed(maze)
  53.         addRightIfNeed(maze)
  54.         addBottomIfNeed(maze)
  55.         addLeftIfNeed(maze)
  56.     }
  57.      
  58.     private fun Node.addTopIfNeed(maze: Array<IntArray>) {    
  59.         var row = id.row - 1
  60.         var col = id.col
  61.         while(row >= 0 && maze[row][col] == 0) {
  62.             row--
  63.         }
  64.         row++
  65.         if(row >= 0 && row != id.row) {
  66.             nodes.add(Node(Node.Id(row, col)))
  67.         }
  68.     }
  69.  
  70.     private fun Node.addRightIfNeed(maze: Array<IntArray>) {    
  71.         var row = id.row
  72.         var col = id.col + 1
  73.         while(col < maze[0].size && maze[row][col] == 0) {
  74.             col++
  75.         }
  76.         col--
  77.         if(col < maze[0].size && col != id.col) {
  78.             nodes.add(Node(Node.Id(row, col)))
  79.         }
  80.     }
  81.    
  82.     private fun Node.addBottomIfNeed(maze: Array<IntArray>) {    
  83.         var row = id.row + 1
  84.         var col = id.col
  85.         while(row < maze.size && maze[row][col] == 0) {
  86.             row++
  87.         }
  88.         row--
  89.         if(row < maze.size && row != id.row) {
  90.             nodes.add(Node(Node.Id(row, col)))
  91.         }
  92.     }
  93.    
  94.    private fun Node.addLeftIfNeed(maze: Array<IntArray>) {    
  95.         var row = id.row
  96.         var col = id.col - 1
  97.         while(col >= 0 && maze[row][col] == 0) {
  98.             col--
  99.         }
  100.         col++
  101.         if(col >= 0 && col != id.col) {
  102.             nodes.add(Node(Node.Id(row, col)))
  103.         }
  104.     }
  105.    
  106.     data class Node(
  107.         val id: Id,
  108.         val nodes: MutableList<Node> = mutableListOf<Node>()
  109.     ) {
  110.         data class Id(
  111.             val row: Int,
  112.             val col: Int
  113.         )
  114.     }
  115. }
Add Comment
Please, Sign In to add comment