亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

雙向鏈接節點矩陣

雙向鏈接節點矩陣

蠱毒傳說 2021-11-17 10:29:53
我正在嘗試在 Java 中創建一個 11 x 11 的節點矩陣雙向鏈接節點,但我有一個問題,我將節點鏈接到右、左和下節點,但是當我嘗試鏈接到上節點時,我就是不能例如,當我嘗試獲取 node.up 時,我得到了一個 null 而不是得到一個 int(我應該得到)。無論如何,這是我的代碼,希望有人可以幫助我。我猜錯誤可能在void linkUp().public class CazadorPresa {// node of linked list static class Node {     int no;     Node right;     Node down;      Node left;    Node up;    int xpos,ypos;    public boolean hunter,prey,crossed,blocked;}; // returns head pointer of linked list // constructed from 2D matrix static Node construct(int arr[][], int i, int j, int m, int n) {     // return if i or j is out of bounds     if (i > n - 1 || j > m - 1)         return null;     // create a new node for current i and j     // and recursively allocate its down and     // right pointers     Node temp = new Node();    temp.no = arr[i][j];     temp.xpos = j;    temp.ypos = i;    temp.blocked = false;    temp.crossed = false;    temp.hunter = false;    temp.prey = false;    temp.right = construct(arr, i, j + 1, m, n);     temp.down = construct(arr, i + 1, j, m, n);     return temp;} // utility function for displaying // linked list data static void display(Node head) {     // pointer to move right     Node Rp;     // pointer to move down     Node Dp = head;     // loop till node->down is not NULL     while (Dp != null) {         Rp = Dp;         // loop till node->right is not NULL         while (Rp != null) {             System.out.print(Rp.no + " ");             Rp = Rp.right;         }         System.out.println();         Dp = Dp.down;     } } // link leftstatic void linkLeft(Node head) {     Node Rp;     Node Dp = head;     Node auxL= head;     // loop till node->down is not NULL     while (Dp != null) {         Rp = Dp;         // loop till node->right is not NULL         while (Rp != null) {             if(Rp==Dp){            }else{                Rp.left = auxL;                auxL = Rp;            }            Rp = Rp.right;            }         Dp = Dp.down;     } }
查看完整描述

1 回答

?
收到一只叮咚

TA貢獻1821條經驗 獲得超5個贊

問題在于使用遞歸來構造Nodes. 當你這樣做時:


temp.right = construct(arr, i, j + 1, m, n); 

temp.down = construct(arr, i + 1, j, m, n); 

您實際上是在創建網格的多個版本,每個版本都會覆蓋right和down鏈接Nodes已經創建的網格。例如,在構造之后,對于給定的 ,應該是這樣的node:


node.right.down == node.down.right

但考慮到網格的構建方式,情況并非如此,當您嘗試將它們連接起來時,這會導致問題。考慮到對于 11x11 網格,您應該創建 121 Nodes,您可以看到問題有多嚴重,但我檢查過,您實際上正在創建 705,431!


幸運的是,修復相當簡單。創建一個 2d 數組Nodes并將它們直接連接起來:


public static void main(String args[]) { 

   // 2D matrix 

   Node arr[][]= new Node[11][11];

   int m = 11, n = 11; 


   int no=1;

   for(int i=0;i<m;i++){

       for(int j=0;j<n;j++){


         arr[i][j] = new Node();

         arr[i][j].no = no;

         arr[i][j].xpos = j;

         arr[i][j].ypos = i;

         no=no+1;

       }

   }


   for(int i=0; i<m; i++)

   {

     for(int j=0; j<n; j++)

     {

       arr[i][j].up    = (i>0)   ? arr[i-1][j] : null;

       arr[i][j].left  = (j>0)   ? arr[i][j-1] : null;

       arr[i][j].down  = (i+1<m) ? arr[i+1][j] : null;

       arr[i][j].right = (j+1<n) ? arr[i][j+1] : null;

     }

   }


   Node head = arr[0][0];

   display(head); 


   hunter(head,5,5);

   }

}

產生:


38

 48 60 50

我相信這是您期望的輸出。


查看完整回答
反對 回復 2021-11-17
  • 1 回答
  • 0 關注
  • 147 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號