10分求助

P2615 [NOIP2015 提高组] 神奇的幻方

```cpp #include<bits/stdc++.h> using namespace std; int b[39][39]; int s = 2; void f(int x, int y, int c) { if (s == (c + 1) * (c + 1)+1) { return; } else { if (x == 0 && y != c) { b[c][y + 1] = s; s++; f(c, y + 1, c); } else if (y == c && x != 0) { b[x - 1][0] = s; s++; f(x - 1, 0, c); } else if (y == c && x == 0) { b[x + 1][y] = s; s++; f(x + 1, y, c); } else if (y != c && x != 0) { if(b[x-1][y+1]==0){ b[x-1][y+1]=s; s++; f(x-1,y+1,c); }else{ b[x+1][y]=s; s++; f(x+1,y,c); } } } }main() { double a; cin >> a; b[0][(int)floor(a / 2)] = 1; f(0, floor(a / 2), (int)a - 1); for(int i=0;i<a;i++){ for(int j=0;j<a;j++){ cout<<b[i][j]<<" "; }cout<<endl; } return 0; } ``` 乍一看这道题是道递归 实际上这道题确实是道递归
by wangderui111 @ 2024-02-06 14:24:18


|