SDUT 2254 字母螺旋方阵(递归)
2016-01-03 09:51
267 查看
与之前做过的螺旋矩阵不同之处在于,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;
}
#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;
}
相关文章推荐
- 有关数据库SQL递归查询在不同数据库中的实现方法
- C#中的递归APS和CPS模式详解
- WinForm实现按名称递归查找控件的方法
- 使用SqlServer CTE递归查询处理树、图和层次结构
- C#中的尾递归与Continuation详解
- C#递归实现显示文件夹及所有文件并计算其大小的方法
- php递归创建目录的方法
- PHP递归创建多级目录
- Javascript递归打印Document层次关系实例分析
- oracle 使用递归的性能提示测试对比
- 使用curl递归下载软件脚本分享
- Perl脚本实现递归遍历目录下的文件
- JavaScript的递归之递归与循环示例介绍
- C# 递归查找树状目录实现方法
- 全排列算法的非递归实现与递归实现的方法(C++)
- php递归列出所有文件和目录的代码
- java递归菜单树转换成pojo对象
- 一个JavaScript递归实现反转数组字符串的实例
- Java中的递归详解(用递归实现99乘法表来讲解)
- C语言的递归思想实例分析