【小米oj】 小米兔的轨迹

刘汝佳紫书上都有

 1 #define mm(a) memset(a,0,sizeof(a));
 2 #define max(x,y) (x)>(y)?(x):(y)
 3 #define min(x,y) (x)<(y)?(x):(y)
 4 #define Fopen freopen("1.in","r",stdin); freopen("m.out","w",stdout);
 5 #define rep(i,a,b) for(ll i=(a);i<=(b);i++)
 6 #define per(i,b,a) for(ll i=(b);i>=(a);i--)
 7 #include<bits/stdc++.h>
 8 typedef long long ll;
 9 #define PII pair<ll,ll>
10 using namespace std;
11 const int INF=0x3f3f3f3f;
12 const int MAXN=(int)2e5+5;
13 const int dx[]= {0,1,0,-1};
14 const int dy[]= {1,0,-1,0};
15 int n,m,a[1005][1005],vis[1005][1005];
16 vector<int>ans;
17 int main() {
18     scanf("%d%d",&n,&m);
19     for(int i=1; i<=n; i++)for(int j=1; j<=m; j++)scanf("%d",&a[i][j]);
20     pair<int,int>now= {1,1};
21     ans.push_back(a[1][1]);
22     vis[1][1]=1;
23     while(ans.size()!=n*m) {
24         while(now.second<m&&!vis[now.first][now.second+1]) {
25             now.second++;
26             vis[now.first][now.second]=1;
27             ans.push_back(a[now.first][now.second]);
28         }
29         while(now.first<n&&!vis[now.first+1][now.second]) {
30             now.first++;
31             vis[now.first][now.second]=1;
32             ans.push_back(a[now.first][now.second]);
33         }
34         while(now.second>1&&!vis[now.first][now.second-1]) {
35             now.second--;
36             vis[now.first][now.second]=1;
37             ans.push_back(a[now.first][now.second]);
38         }
39         while(now.first>1&&!vis[now.first-1][now.second]) {
40             now.first--;
41             vis[now.first][now.second]=1;
42             ans.push_back(a[now.first][now.second]);
43         }
44     }
45     for(auto i:ans)printf("%d ",i);
46     printf("
");
47     return 0;
48 }
原文地址:https://www.cnblogs.com/dogenya/p/10815647.html