fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. int n, r[102][102],i=2,j=1;
  4.  
  5. void up(int i, int j){
  6. if(r[i-1][j+1]==0){
  7. i--;
  8. j++;
  9. }
  10. else {
  11. if(r[i-1][j+1]==-1 && r[i][j+1]==0){
  12. j++;
  13. }
  14. else {
  15. if(r[i-1][j+1]==-1 && r[i][j+1]==-1){
  16. i++;
  17. }
  18. }
  19. }
  20. }
  21.  
  22.  
  23. void down(int i, int j){
  24. if(r[i+1][j-1]==0){
  25. i++;
  26. j--;
  27. }
  28. else {
  29. if(r[i+1][j-1]==-1 && r[i+1][j]==0){
  30. i++;
  31. }
  32. else {
  33. if(r[i+1][j-1]==-1 && r[i+1][j]==-1){
  34. j++;
  35. }
  36. }
  37. }
  38. }
  39.  
  40.  
  41.  
  42. int main() {
  43. cin>>n;
  44. for(int i=0;i<=n+1;i++){
  45. for(int j=0;j<=n+1;j++){
  46. r[i][j]=-1;
  47. }
  48. }
  49. for(int i=1;i<=n;i++){
  50. for(int j=1;j<=n;j++){
  51. r[i][j]=0;
  52. }
  53. }
  54. int t=2;
  55. r[1][1]=1;
  56. while(t<=n*n){
  57. r[i][j]=t;
  58. t++;
  59. if(r[i-1][j+1]==0){
  60. up;
  61. }
  62. else{
  63. down;
  64. }
  65. }
  66.  
  67.  
  68.  
  69.  
  70. for(int i=1;i<n+1;i++){
  71. for(int j=1;j<n+1;j++){
  72. cout<<r[i][j]<<" ";
  73. }
  74. cout<<endl;
  75. }
  76.  
  77. }
  78.  
Success #stdin #stdout 0s 5320KB
stdin
3
stdout
1 0 0 
9 0 0 
0 0 0