SDUT 2254 字母螺旋方阵(递归)

与之前做过的螺旋矩阵不同之处在于,n*m的矩阵,并且是在A-Z之间不停循环的问题。注意控制好边界如递归条件一直在l<=r&&up<=down 和tmp==cnt

#include <iostream>
#include <cstring>
#include<cmath>
#include<cstdio>
using namespace std;

int cnt;

int a[1100][1100];
int n,m;
void so(int up,int down,int l,int r,int s,int tmp)
{
    int i;
    if(l<=r&&up<=down)
    {
        for(i=l; i<=r; i++)
        {
            tmp++;
            ++s;
            if(s==27)
                s=1;
            a[up][i]=s;
            if(tmp==cnt)
            {
                for(int x=1; x<=n; x++)
                {
                    for(int y=1; y<=m; y++)
                    {
                        int k=a[x][y];
                        printf(" %c",64+k);

                    }
                    cout<<endl;
                }
                return ;
            }

        }

        for(i=up+1; i<down; i++)
        {
            tmp++;
            ++s;
            if(s==27)
            s=1;
            a[i][r]=s;
            if(tmp==cnt)
            {
              //  s=0;
                for(int x=1; x<=n; x++)
                {
                    for(int y=1; y<=m; y++)
                    {
                        int k=a[x][y];
                        printf(" %c",64+k);
                    }
                    cout<<endl;
                }
                return ;
            }
        }
        for(i=r; i>=l; --i)
        {
            tmp++;
            ++s;
            if(s==27)
                s=1;
            a[down][i]=s;
            if(tmp==cnt)
            {

                for(int x=1; x<=n; x++)
                {
                    for(int y=1; y<=m; y++)
                    {
                        int k=a[x][y];
                        printf(" %c",64+k);
                    }
                    cout<<endl;
                }
                return ;
            }
        }
        for(i=down-1; i>up; --i)
        {
            tmp++;
            ++s;
            if(s==27)
                s=1;
            a[i][l]=s;
            if(tmp==cnt)
            {
                s=0;
                for(int x=1; x<=n; x++)
                {
                    for(int y=1; y<=m; y++)
                    {
                        int k=a[x][y];
                        printf(" %c",64+k);
                    }
                    cout<<endl;
                }
                return ;
            }
        }
        so(up+1,down-1,l+1,r-1,s,tmp);
    }
    else
    {
        for(i=1; i<=n; i++)
        {
            for(int j=1; j<=m; j++)
            {
                int k=a[i][j];
                printf(" %c",65+k);
            }
            cout<<endl;
        }
    }
}

int main()
{
    int i,j,k;
    while(~scanf("%d%d",&n,&m))
    {
        cnt=n*m;
        so(1,n,1,m,0,0);
    }
    return 0;
}


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值