P5731 【深基5.习6】蛇形方阵

本题由于数据不大,最多只要输出9中类型的蛇形方阵,可以用打表来解决:

#include <bits/stdc++.h>
using namespace std;
int n;
int main () {
	cin >> n;
	if(n == 1)
		cout << "  1\n";
	if(n == 2)
		cout <<"  1  2\n  4  3\n";
	if(n == 3) {
		cout << "  1  2  3\n";
		cout << "  8  9  4\n";
		cout << "  7  6  5\n";
	}
	if(n == 4) {
		cout << "  1  2  3  4\n";
		cout << " 12 13 14  5\n";
		cout << " 11 15 16  6\n";
		cout << " 10  9  8  7\n";
	}
	if(n == 5) {
		cout << "  1  2  3  4  5\n";
		cout << " 16 17 18 19  6\n";
		cout << " 15 24 25 20  7\n";
		cout << " 14 23 22 21  8\n";
		cout << " 13 12 11 10  9\n";
	}
	if(n == 6) {
		cout << "  1  2  3  4  5  6\n";
		cout << " 20 21 22 23 24  7\n";
		cout << " 19 32 33 34 25  8\n" ;
		cout << " 18 31 36 35 26  9\n";
		cout << " 17 30 29 28 27 10\n";
		cout << " 16 15 14 13 12 11\n";
	}
	if(n == 7) {
		cout << "  1  2  3  4  5  6  7\n";
		cout << " 24 25 26 27 28 29  8\n";
		cout << " 23 40 41 42 43 30  9\n";
		cout << " 22 39 48 49 44 31 10\n";
		cout << " 21 38 47 46 45 32 11\n";
		cout << " 20 37 36 35 34 33 12\n";
		cout << " 19 18 17 16 15 14 13\n";
	}
	if(n == 8) {
		cout << "  1  2  3  4  5  6  7  8\n";
		cout << " 28 29 30 31 32 33 34  9\n";
		cout << " 27 48 49 50 51 52 35 10\n";
		cout << " 26 47 60 61 62 53 36 11\n";
		cout << " 25 46 59 64 63 54 37 12\n";
		cout << " 24 45 58 57 56 55 38 13\n";
		cout << " 23 44 43 42 41 40 39 14\n";
		cout << " 22 21 20 19 18 17 16 15\n";
	}
	if(n == 9) {
		cout << "  1  2  3  4  5  6  7  8  9\n";
		cout << " 32 33 34 35 36 37 38 39 10\n";
		cout << " 31 56 57 58 59 60 61 40 11\n";
		cout << " 30 55 72 73 74 75 62 41 12\n";
		cout << " 29 54 71 80 81 76 63 42 13\n";
		cout << " 28 53 70 79 78 77 64 43 14\n";
		cout << " 27 52 69 68 67 66 65 44 15\n";
		cout << " 26 51 50 49 48 47 46 45 16\n";
		cout << " 25 24 23 22 21 20 19 18 17\n";
	}
	return 0;
}

此方法过于简单,我们下面要按照题目思路正常解决此题,以下是来自dingcx大佬的题解:

主要问题是怎么转。其实每次转都相当于是往右转,所以只需要定义一个改变位置的数组,还有一个变量记录现在头朝哪边,每次转改变一下就行了。注意不能直接改,需要 %4

改变位置的数组:

int pos[4][2]={0,1,1,0,0,-1,-1,0};//0表示右,1表示下,2表示左,3表示上

核心代码(个人认为上述的pos二维数组是本题的关键所在,可以控制转向):

//a记录输出,d记录头朝向(含义同pos)
int tx=x+pos[d][0],ty=y+pos[d][1];//先变着
if(tx<1||tx>n||ty<1||ty>n||a[tx][ty])//如果越界或以前填过
	d=(d+1)%4;//头往右转,注意%4
x+=pos[d][0],y+=pos[d][1];//改变x和y

下面我给出图解完善大佬的题解:

不断循环上述操作,赋值之前先判断要不要转向,如此便能模拟出蛇形矩阵的操作,以下是完整代码:

#include<cstdio>
using namespace std;
int read(){//快速读取
	int x=0,f=1;
	char c=getchar();
	while(c<'0'||c>'9'){
		if(c=='-') f=-1;
		c=getchar();
	}
	while(c>='0'&&c<='9'){
		x=x*10+c-'0';
		c=getchar();
	}
	return x*f;
}
int a[15][15];//记录输出的数组
int pos[4][2]={0,1,1,0,0,-1,-1,0};//改变位置的数组
int main(){//主函数
	int n=read(),x=1,y=1,d=0;//初始化
	for(int i=1;i<=n*n;i++){//遍历
		a[x][y]=i;//赋值
		int tx=x+pos[d][0],ty=y+pos[d][1];//核心代码
		if(tx<1||tx>n||ty<1||ty>n||a[tx][ty]) d=(d+1)%4;
		x+=pos[d][0],y+=pos[d][1];
	}
	for(int i=1;i<=n;i++){//输出
		for(int j=1;j<=n;j++) printf("%3d",a[i][j]);//注意%3d
		if(i<n) printf("\n");
	}
	return 0;
}
  • 19
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值