fork download
  1. #include<bits/stdc++.h>
  2. #define ll long long
  3. #define int long long
  4. #define endl '\n'
  5. #define all(s) s.begin(),s.end()
  6. #define rep(i,n) for(int i=0;i<(n);i++)//0-indexed
  7. #define ull unsigned long long
  8. // #define NDEBUG
  9. #define loop(a,b) for(int i=a;i<=b;i++)
  10. #define format(n) fixed<<setprecision(n)
  11. // Matrix vector<vector<data type>>a(r,vector<data type>(c));
  12. #define AYAA ios_base::sync_with_stdio(false); cout.tie(0);
  13. using namespace std;
  14. int fx[]={0,-1,0,1};
  15. int fy[]={1,0,-1,0};
  16. int pos;
  17. int n,m,cnt=0;
  18. int vis[1000][1000];
  19. bool notvalid(int i,int j,vector<vector<int>>&a){
  20. if(i>n||j>m||i<1||j<1||vis[i][j])
  21. return true;
  22. else
  23. return false;
  24. }
  25. void reco(int i,int j,vector<vector<int>>&a){
  26. if(notvalid(i,j,a)) {
  27. pos=(pos+1)%4;
  28. return;
  29. }
  30. cnt++;
  31. cout<<a[i][j]<<" ";
  32. if(cnt%n==0) cout<<endl;
  33. vis[i][j]=1;
  34. reco(i+fx[pos],j+fy[pos],a);
  35. reco(i+fx[pos],j+fy[pos],a);
  36. reco(i+fx[pos],j+fy[pos],a);
  37. reco(i+fx[pos],j+fy[pos],a);
  38.  
  39.  
  40.  
  41.  
  42.  
  43.  
  44.  
  45. }
  46.  
  47.  
  48.  
  49.  
  50.  
  51. int32_t main() {
  52. #ifndef ONLINE_JUDGE
  53. freopen("Input.txt","r",stdin);
  54. freopen("Output.txt","w",stdout);
  55. #endif
  56. AYAA
  57.  
  58.  
  59.  
  60.  
  61. int t=1;
  62. //cin>>t;
  63. while(t--) {
  64. cin>>n;
  65. m=n;
  66. vector<vector<int>>a(n+1,vector<int>(m+1));
  67. for(int i=1;i<=n;i++){
  68. for(int j=1;j<=m;j++){
  69. cin>>a[i][j];
  70. }
  71. }
  72. reco(1,1,a);
  73.  
  74. }
  75.  
  76.  
  77.  
  78.  
  79.  
  80.  
  81.  
  82.  
  83. return 0;
  84. }
Success #stdin #stdout 0.01s 5304KB
stdin
Standard input is empty
stdout
Standard output is empty