Readable C++ solution


#1
int t = 1;
int up = 0, down = n-1, left = 0, right = n-1;
int r,c;
while (true) {
    r = up;
    for (int i=left; i<=right; i++) {
        A[r][i] = t++;
    }
    if (t==n*n+1) break;
    up++;
    
    c = right;
    for (int i=up; i<=down; i++) {
        A[i][c] = t++;
    }
    if (t==n*n+1) break;
    right--;
    
    r = down;
    for (int i=right; i>=left; i--) {
        A[r][i] = t++;
    }
    if (t==n*n+1) break;
    down--;
    
    c = left;
    for (int i=down; i>=up; i--) {
        A[i][c] = t++;
    }
    if (t==n*n+1) break;
    left++;
    
}