/*逆时针读数,但每读过一个就标志已读*/
#include<iostream>
#include<vector>
using namespace std;
int main(){
int m, n, cnt=1, x=0, y=0, flag=0;
cin>>m>>n;
vector<vector<int> > M(m, vector<int>(n, 0)), visited(m, vector<int>(n,0));
for(int i=0; i<m; i++)
for(int j=0; j<n; j++)
cin>>M[i][j];
cout<<M[0][0]; visited[0][0]=1;
while(cnt<m*n){
while(x+1<m&&!visited[x+1][y]){
visited[++x][y]=1; cnt++;
cout<<" "<<M[x][y];
}
while(y+1<n&&!visited[x][y+1]){
visited[x][++y]=1; cnt++;
cout<<" "<<M[x][y];
}
while(x-1>=0&&!visited[x-1][y]){
visited[--x][y]=1; cnt++;
cout<<" "<<M[x][y];
}
while(y-1>=0&&!visited[x][y-1]){
visited[x][--y]=1; cnt++;
cout<<" "<<M[x][y];
}
}
cout<<endl;
return 0;
}
原文地址:https://www.cnblogs.com/A-Little-Nut/p/10348806.html
时间: 2024-10-29 05:03:34