Demo entry 6683273

as

   

Submitted by anonymous on Dec 12, 2017 at 16:17
Language: Java. Code size: 1.6 kB.

  void findtheWay()
  {
    if (sPoint != ePoint && sPoint != null && ePoint != null)
    {
      //Use BFS to find the way
      ArrayList<Node> blocks = new ArrayList<Node>();
      int[][] visit = new int[this.rows][this.cols];
      Node[][] Nodes = new Node[this.rows][this.cols];
      
      //Add startpoint to the list
      blocks.add(sPoint);
      visit[sPoint.x][sPoint.y] = 1;
      Nodes[sPoint.x][sPoint.y] = sPoint;
      
      int dir[][] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
      
      while (blocks.size() > 0)
      {
        Node currentNode = blocks.get(0);
        
        if (currentNode.x == ePoint.x && currentNode.y == ePoint.y) break; 
        
        for (int i = 0; i < 4; i++)
        {
          int nx = currentNode.x + dir[i][0];
          int ny = currentNode.y + dir[i][1];
          if (nx == 0 || nx == this.rows-1 || ny == 0 || ny == this.cols-1) continue;
          if (visit[nx][ny] == 0 && this.map[nx][ny] == 1) 
          {
            Node _node = new Node(nx, ny);
            _node.lNode = currentNode;
            blocks.add(_node);
            visit[nx][ny] = 1;
            Nodes[nx][ny] = _node;
          }
        }
        blocks.remove(0);
      }
      
      //init way status
       this.way = new int [rows][cols];  
      
      //trace the way
      if (blocks.size() > 0)
      {
        Node nowNode = blocks.get(0);
        while (nowNode.x != sPoint.x || nowNode.y != sPoint.y)
        {
          this.way[nowNode.x][nowNode.y] = 1;
          nowNode = Nodes[nowNode.x][nowNode.y].lNode;
        }
      }  
    }
  }

This snippet took 0.00 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).