class Solution { public: vector<vector<int> > Matrix(int n) { vector<vector<int>> f(n,vector<int>(n,0)); vector<vector<int>> ans(n,vector<int>(n)); int i = 1,x = 0,y = 0; while(i<=n*n) { if(!f[x][y]) { ans[x][y] = i; f[x][y] = 1; i++; } while(!f[x][y+1] && y+1<n) { y++; ans[x][y] = i; f[x][y] = 1; i++; } while(!f[x+1][y] && x+1<n) { x++; ans[x][y] = i; f[x][y] = 1; i++; } while(!f[x][y-1] && y-1>=0){ y--; ans[x][y] = i; f[x][y] = 1; i++; } while(!f[x-1][y] && x-1>=0) { x--; ans[x][y] = i; f[x][y] = 1; i++; } } return ans; } };