顺时针打印数字矩阵
给定一个数字矩阵,请设计一个算法从左上角开始顺时针打印矩阵元素
输入描述
输入第一行是两个数字,分别代表行数M和列数N;接下来是M行,每行N个数字,表示这个矩阵的所有元素;当读到M=-1,N=-1时,输入终止。
输出描述:
请按逗号分割顺时针打印矩阵元素(注意最后一个元素末尾不要有逗号!例如输出“1,2,3”,而不是“1,2,
3,”),每个矩阵输出完成后记得换行
输入
3 3
1 2 3
4 5 6
7 8 9
-1 -1
输出
1,2,3,6,9,8,7,4,5
备注:
M,N为正整数且 M*N<=300000
其实这样的题目倒是变得更好写,循环读入,然后遍历输出就好了,不用考虑算法的时间复杂度不过关的问题了。因为有逗号的问题,所以我们先输出第一个,然后每次输出的时候是(逗号+数字)这样就保证了输出的格式,并且方便我们对要输出的位置是否正确进行判断;
从(0,0)开始,每次判断他的下一个位置是否是需要输出的数字,输出之后给他赋值成0,表示这个数字已经输出过,然后开始转圈,每次碰到边界或者已经输出过了的数就转弯,代码如下:
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int N = 1010;
int s[N][N];
int main()
{
while(true)
{
int n, m, x = 0, y = 0;
scanf("%d%d",&n, &m);
if(n == -1 && m == -1) return 0;
for(int i = 0; i < n; i ++ )
for(int j = 0; j < m; j ++ )
scanf("%d",&s[i][j]);
int tot = n * m, now = 1;
printf("%d",s[0][0]);
s[0][0] = 0;
while(now < tot)
{
while(y + 1 < m && s[x][y + 1])
{
printf(",%d", s[x][++y]);
s[x][y] = 0;
now ++;
}
while(x + 1 < n && s[x + 1][y])
{
printf(",%d", s[++x][y]);
s[x][y] = 0;
now ++;
}
while(y - 1 >= 0 && s[x][y-1])
{
printf(",%d", s[x][--y]);
s[x][y] = 0;
now ++;
}
while(x - 1 >= 0 && s[x-1][y])
{
printf(",%d", s[--x][y]);
s[x][y] = 0;
now ++;
}
}
printf("\n");
}
}
还有一个类似的,在刚开始接触循环的时候碰到过的一个问题
蛇形填数– 来自《算法竞赛入门经典》
#include<stdio.h>
#include<string.h>
#define maxn 20
int a[maxn][maxn];
int main()
{
int n, x, y, tot = 0;
scanf("%d", &n);
memset(a, 0, sizeof(a));
tot = a[x=0][y=n-1] = 1;
while(tot < n*n)
{
while(x+1<n && !a[x+1][y]) a[++x][y] = ++tot;
while(y-1>=0 && !a[x][y-1]) a[x][--y] = ++tot;
while(x-1>=0 && !a[x-1][y]) a[--x][y] = ++tot;
while(y+1<n && !a[x][y+1]) a[x][++y] = ++tot;
}
for(x = 0; x < n; x++)
{
for(y = 0; y < n; y++) printf("%3d", a[x][y]);
printf("\n");
}
printf("a[0][%d] = %d\n",n-1, a[0][n-1]);
return 0;
}