EOJ 2983 蛇行图案
来源:互联网 发布:linux sip服务器 编辑:程序博客网 时间:2024/04/30 11:38
题目简介
输出蛇形图案,如:
1 2 3
8 9 4
7 6 5
说明
直接模拟,用控制方向的常量数组简化程序。
#include <stdio.h>#include <stdbool.h>#include <memory.h>int n, x, y, num[11][11];bool inside(int x, int y){ if (1 <= x && x <= n && 1 <= y && y <= n && num[x][y] == 0) return 1; return 0;}int main(){ int cas, i, j, t; const int dx[4] = {0, 1, 0, -1}; const int dy[4] = {1, 0, -1, 0}; scanf("%d", &cas); for (t = 0; t < cas; ++t){ scanf("%d", &n); memset(num, 0, sizeof(num)); int now = 0, dir = 0; x = 1;y = 1; for (i = 1; i <= n * n; ++i){ num[x][y] = ++now; if (now == n * n) break; int x1 = x + dx[dir], y1 = y + dy[dir]; while (!inside(x1, y1)){ dir = (dir + 1) % 4; x1 = x + dx[dir]; y1 = y + dy[dir]; } x = x1; y = y1; } printf("case #%d:\n", t); for (i = 1; i <= n; ++i){ printf("%d", num[i][1]); for (j = 2; j <= n; ++j) printf(" %d", num[i][j]); printf("\n"); } } return 0;}
阅读全文
0 0
- EOJ 2983 蛇行图案
- EOJ
- EOJ
- EOJ
- 蛇行矩阵
- 蛇行矩阵
- 蛇行矩阵
- 蛇行矩阵
- 蛇行矩阵
- 蛇行矩阵
- 【hihocoder1257 2015北京赛区I】【构造 从公式入手】Snake Carpet 构造矩阵使得恰好容纳1~n个拐拐蛇行图案
- 图案
- 图案
- 蛇行矩阵算法
- fjnu 1304 蛇行矩阵
- 蛇行方阵的算法
- 蛇行矩阵的解法
- 蛇行矩阵 [ 天羽 转载]
- java中写入文件时换行符是用"/r/n"还是"/n"?
- Qt之图形(绘制文本)
- eggjs&sequelize使用教程一(表单自动验证)
- SDUT-2138 数据结构实验之图论三:判断可达性(DFS)
- USB摄像头驱动框架分析
- EOJ 2983 蛇行图案
- 用IDEA打开现有的项目
- (CNN)卷积神经网络(四)dropout
- centos7安装jdk
- 个人总结55
- 构造器
- hue web报错 400 'json' or 'msgpack' parameter is required
- JAVA | 50
- 第十三周项目1验证算法