0
在給定的二維正方形(n * n)偶數大小的數組中,我想從起始角到中心遍歷。以下是更多信息的圖片。二維數組遍歷從中心到中心
我的算法是從角落開始和維護兩個全局變量作爲currentX
和currentY
和運行loop
直到currentX
和currentY
到達市中心。以下是我的僞代碼 -
x=0
y=0
currentX=0
currentY=0
while(currentX != centerX and currentY != centerY){
currentX=travel_in_x_plus_direction(x,n);
currenty=travel_in_y_plus_direction(y,n);
currentX=travel_in_x_minux_direction(currentX,x);
currentY=travel_in_y_minux_direction(currentY,y-1);
n--;
x--;
y--;
}
The function travel_in_x_plus_direction(currentX) traverse the array starting from currentX till x and returns the final value of x. The same concept applies for rest of the functions also.
這是正確的方法嗎?有沒有更好的方法來以相同的方式遍歷它?