#include<iostream>
using namespace std;
const int m=4;
const int n=4;
int mat[m][n];
bool vis[m][n];
int dir=1;
void init(){
memset(vis,0,sizeof(vis));
int num=1;
for(int i=0;i<m;i++)
for(int j=0;j<n;j++)
mat[i][j]=num++;
}
int main(){
init();
int i=0,j=-1,count=0;
while(count<m*n){
if(dir==1){
while(!vis[i][j+1]&&j+1<n){
cout<<mat[i][j+1]<<" ";
vis[i][j+1]=1;
count++;
j++;
}
dir=2;
}
if(dir==2){
while(!vis[i+1][j]&&i+1<m){
cout<<mat[i+1][j]<<" ";
vis[i+1][j]=1;
count++;
i++;
}
dir=3;
}
if(dir==3){
while(!vis[i][j-1]&&j-1>=0){
cout<<mat[i][j-1]<<" ";
vis[i][j-1]=1;
count++;
j--;
}
dir=4;
}
if(dir==4){
while(!vis[i-1][j]&&i-1>=0){
cout<<mat[i-1][j]<<" ";
vis[i-1][j]=1;
count++;
i--;
}
dir=1;
}
}
system("pause");
return 0;
}
11-17
04-12
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交