- - 基本上和上一道一样的道理
const int dx[] = {0, 1, 0, -1};
const int dy[] = {1, 0, -1, 0};
int n;
vector<vector<int> > ans;
void dfs(int r, int c, int p, int number){
ans[r][c] = number;
for(int i = 0; i < 4; ++ i){
int nowp = p + i & 3;
int nr = r + dx[nowp];
int nc = c + dy[nowp];
if(nr >= 0 && nr < n)
if(nc >= 0 && nc < n)
if(ans[nr][nc] == 0){
dfs(nr, nc, nowp, number + 1);
break;
}
}
}
class Solution {
public:
vector<vector<int> > generateMatrix(int n) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
ans.clear();
if(n <= 0)
return ans;
::n = n;
ans.resize(n);
for(int i = 0; i < n; ++ i)
for(int j = 0; j < n; ++ j)
ans[i].push_back(0);
dfs(0, 0, 0, 1);
return ans;
}
};