:
( )
3x3
+---+---+---+
| | | |
+---+---+---+
| | | |
+---+---+---+
| | | |
+---+---+---+
,
SR=0, ER=2, SC=0, EC=2
(SR: startRow, ER: endRow, SC: startCol, EC: endCol)
SC->EC
SR++ (=>SR=1)
SR->ER
EC-- (=>EC=1)
EC->SC
ER-- (=>ER=1)
ER->SR
SC++ (=>SC=1)
SC->EC
-> finish with (SR=1,ER=1,SC=1,EC=1)
, .
SR=1, ER=1, SC=1, EC=1
EC->SC
SC--
SR->ER
ER++
SC->EC
EC++
ER->SR
SR--
EC->SC
-> finish with (SR=0,ER=2,SC=0,EC=2)
, SR, ER, SC, EC, , .
, , startCol endCol. .
,
if(leftToRight) {
for(int i = startCol; i <= endCol ; i++){
stringBuffer.append(matrix[startRow][i]);
counter--;
}
leftToRight = false;
moveCol = false;
startRow++;
}
if (leftToRight) {
for (int i = startCol; i <= endCol; i++) {
stringBuffer.append(matrix[endRow][i]);
counter--;
}
leftToRight = false;
moveCol = false;
endCol++;
}
, # # , , (moveCol, leftRight, upDown). , #, # , .
+---+---+---+---+
| | | | |
+---+---+---+---+
| | P | | |
+---+---+---+---+
| | | | |
+---+---+---+---+
| | | | |
+---+---+---+---+
, 4x4, P (1,1) (startRow = (4-1)/2, startCol = (4-1)/2), (upDown = true, leftRight = true, moveCol = false)
if (leftToRight) {
for (int i = startCol; i <= endCol; i++) {
stringBuffer.append(matrix[startRow][i]);
counter--;
}
leftToRight = false;
moveCol = false;
endCol++;
}