jerry960216

Untitled

Aug 19th, 2023
1,209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.74 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. int n,d,s=1,t=0; //d為方向 s為步數 t判斷是否要增加步數
  4. int main()
  5. {
  6. cin>>n>>d;
  7. int a[n][n];
  8. for(int i=0;i<n;i++)
  9. {
  10. for(int j=0;j<n;j++)
  11. cin>>a[j][i];
  12. }
  13. int x=n/2 ,y=n/2,m=n*n;
  14. m--;
  15. cout<<a[x][y];
  16. while(m>1)
  17. {
  18. if(d==0)
  19. {
  20. for(int i=1;i<=s;i++)
  21. {
  22. if(x-1<0)
  23. break;
  24. else
  25. {
  26. cout<<a[x-1][y];
  27. x--;
  28. m--;
  29. }
  30. }
  31. t++;
  32. }
  33. if(d==1)
  34. {
  35. for(int i=1;i<=s;i++)
  36. {
  37. if(y-1<0)
  38. break;
  39. else
  40. {
  41. cout<<a[x][y-1];
  42. y--;
  43. m--;
  44. }
  45. }
  46. t++;
  47. }
  48. if(d==2)
  49. {
  50. for(int i=1;i<=s;i++)
  51. {
  52. if(x+1>n-1)
  53. break;
  54. else
  55. {
  56. cout<<a[x+1][y];
  57. x++;
  58. m--;
  59. }
  60. }
  61. t++;
  62. }
  63. if(d==3)
  64. {
  65. for(int i=1;i<=s;i++)
  66. {
  67. if(y+1>n-1)
  68. break;
  69. else
  70. {
  71. cout<<a[x][y+1];
  72. y++;
  73. m--;
  74. }
  75. }
  76. t++;
  77. }
  78. if(t==2)
  79. {
  80. t=0;
  81. s++;
  82. }
  83. d=(d+1)%4;
  84. }
  85. return 0;
  86. }
  87.  
Advertisement
Add Comment
Please, Sign In to add comment