[英]Unique paths from top-left corner to bottom-right corner of grid by getting input
[英]Algorithm for a top left/bottom right grid?
我有一個來自JPPanel的GridLayout(4,4,0,0)的正方形網格,只是說一個4x4的小例子
*-*-*-*
*-*-*-*
*-*-*-*
*-*-*-*
假設我只想在左上角循環。 在這種情況下
*-*
*
對於另一種情況,右下角。
*
*-*
我在想動態循環嗎? 對於較大尺寸的板。
案例:1(左上角從(0,0)開始)
int csize = (number of cols)/2+1;
for (int row = 0; row < (number of cols)/2; row++){
csize--;
for (int col = 0; col < csize; col++){
...stuff.....
}
}
案例:2(右下角,從(4,4)開始)
int csize = (number of cols)/2;
for (int row = (number of cols) -1; row > (number of cols)/2; row--){
csize++;
for (int col = (number of cols) -1; col > csize; col--){
...stuff...
}
}
我只是做了cols的初始大小,並減少了行內列的嵌套循環
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.