顺时针输出矩阵


#include <iostream>
#include <string.h>
using namespace std;
typedef struct zuobiao{
    int x,y;
}Spot;
Spot *sequence;
void init(int **a,int **fuzhu,int m,int n){
    sequence[0].x=0;
    sequence[0].y=0;
    int flag=1;
    int count1=0;
    fuzhu[0][0]=1;
    int dx,dy;
    while(flag){
        int x=sequence[count1].x;
        int y=sequence[count1].y;
        int wether=1;
        if(dx||dy){
            if(wether&&y+dy<n&&x+dx<m&&y+dy>=0&&x+dx>=0){
                if(fuzhu[x+dx][y+dy]==0){
                    fuzhu[x+dx][y+dy]=1;
                    count1++;
                    sequence[count1].x=x+dx;
                    sequence[count1].y=y+dy;
                    wether=0;
                }
            }
        }
        if(wether&&y+1<n&&fuzhu[x][y+1]==0){//east
            fuzhu[x][y+1]=1;
            count1++;
            sequence[count1].x=x;
            sequence[count1].y=y+1;
            wether=0;
        }
        if(wether&&x+1<m&&fuzhu[x+1][y]==0){//south
            fuzhu[x+1][y]=1;
            count1++;
            sequence[count1].x=x+1;
            sequence[count1].y=y;
            wether=0;
        }
        if(wether&&y-1>=0&&fuzhu[x][y-1]==0){//west
            fuzhu[x][y-1]=1;
            count1++;
            sequence[count1].x=x;
            sequence[count1].y=y-1;
            wether=0;
        }
        if(wether&&x-1>=0&&fuzhu[x-1][y]==0){//north
            fuzhu[x-1][y]=1;
            count1++;
            sequence[count1].x=x-1;
            sequence[count1].y=y;
            wether=0;
        }
        dx=sequence[count1].x-x;
        dy=sequence[count1].y-y;
        if(wether) flag=0;
    }
}
int main()
{
    int m,n;
    cin>>m>>n;
    int **a = new int*[m];
    int **fuzhu = new int*[m];
    for(int i=0;i<m;i++){
        a[i]=new int[n];
        fuzhu[i] =new int[n];
    }
    int count1=0;
    for(int i=0;i<m;i++)
        for(int j=0;j<n;j++){
            a[i][j]=count1;
            count1++;
            fuzhu[i][j]=0;
        }
    sequence = new Spot[m*n];
    init(a,fuzhu,m,n);
    for(int i=0;i<m;i++){
        for(int j=0;j<n;j++)
            cout<<a[i][j]<<"\t";
        cout<<"\n";
    }
    for(int i=0;i<m*n;i++){
        int x=sequence[i].x;
        int y=sequence[i].y;
        cout<<a[x][y]<<" ";
    }

    return 0;
}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值