蛇形填数

来源:互联网 发布:windows of the mind 编辑:程序博客网 时间:2024/05/16 06:14
#include<iostream>#include "stdlib.h"#define MAX_ 10void showArr(int arr[MAX_][MAX_], int m){for (int i = 0; i < MAX_; i++){for (int j = 0; j < m; j++){std::cout << arr[i][j] << "   ";}std::cout << std::endl;}}void snakeShape(int arr[MAX_][MAX_], int m){int x = 0, y = MAX_ - 1;int count = arr[x][MAX_ - 1] = 1;while (m*m > count){while (x + 1 < MAX_ && !arr[x + 1][y]) arr[++x][y] = ++count;while (y > 0 && !arr[x][y - 1]) arr[x][--y] = ++count;while (x > 0 && !arr[x - 1][y]) arr[--x][y] = ++count;while (y + 1 < MAX_ && !arr[x][y + 1]) arr[x][++y] = ++count;}}void main(){int arr[MAX_][MAX_];memset(arr, 0, sizeof(arr));snakeShape(arr, MAX_);showArr(arr, MAX_);system("pause");return;}

0 0