-
1.
-
/***************************************************************************
-
2.
-
* Snail_Algorithms.c
-
3.
-
*
-
4.
-
* Wed Jan 2 16:45:57 2008
-
5.
-
* Copyright 2008 pchero21
-
6.
-
* pchero21@gmail.com
-
7.
-
****************************************************************************/
-
8.
-
-
9.
-
/*
-
10.
-
* This program is free software; you can redistribute it and/or modify
-
11.
-
* it under the terms of the GNU General Public License as published by
-
12.
-
* the Free Software Foundation; either version 2 of the License, or
-
13.
-
* (at your option) any later version.
-
14.
-
*
-
15.
-
* This program is distributed in the hope that it will be useful,
-
16.
-
* but WITHOUT ANY WARRANTY; without even the implied warranty of
-
17.
-
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-
18.
-
* GNU General Public License for more details.
-
19.
-
*
-
20.
-
* You should have received a copy of the GNU General Public License
-
21.
-
* along with this program; if not, write to the Free Software
-
22.
-
* Foundation, Inc., 59 Temple Place – Suite 330, Boston, MA 02111-1307, USA.
-
23.
-
*/
-
24.
-
-
25.
-
#include <stdio.h>
-
26.
-
#include <math.h>
-
27.
-
-
28.
-
void array_rotation(int size, int (*array)[size]);
-
29.
-
void snail_algorithms(int size, int (*array)[size]);
-
30.
-
-
31.
-
int main()
-
32.
-
{
-
33.
-
int size;
-
34.
-
int tmp, i, j, k;
-
35.
-
int first, n, size_tmp;
-
36.
-
-
37.
-
-
38.
-
scanf(“%d”, &size);
-
39.
-
-
40.
-
int array[size][size];
-
41.
-
-
42.
-
snail_algorithms(size, array) ;
-
43.
-
-
44.
-
for(i = 0; i < size; i++) {
-
45.
-
for(j = 0; j < size; j++)
-
46.
-
-
47.
-
-
48.
-
putchar(‘n‘);
-
49.
-
}
-
50.
-
}
-
51.
-
-
52.
-
/*********************************************************************************************
-
53.
-
* FUNCTION : array_rotation
-
54.
-
* DESCRIPTION : 행렬의 방향을 90도 바꾸어 주는 함수
-
55.
-
*********************************************************************************************/
-
56.
-
void array_rotation(int size, int (*array)[size])
-
57.
-
{
-
58.
-
int i, j;
-
59.
-
int array_tmp[size][size];
-
60.
-
-
61.
-
// array copy
-
62.
-
for(i = 0; i < size; i++)
-
63.
-
for(j = 0; j < size; j++)
-
64.
-
array_tmp[i][j] = array[i][j];
-
65.
-
-
66.
-
// rotate
-
67.
-
for(i = 0; i < size; i++)
-
68.
-
for(j = 0; j < size; j++)
-
69.
-
array[i][j] = array_tmp[j][size – i – 1];
-
70.
-
-
71.
-
}
-
72.
-
-
73.
-
/*********************************************************************************************
-
74.
-
* FUNCTION : snail_algorithms
-
75.
-
* DESCRIPTION : 달팽이 알고리즘. 행렬의 숫자 배열을 달팽이 껍질처럼 입력한다.
-
76.
-
*********************************************************************************************/
-
77.
-
void snail_algorithms(int size, int (*array)[size])
-
78.
-
{
-
79.
-
int first;
-
80.
-
int number;
-
81.
-
int size_tmp;
-
82.
-
int i, j, k;
-
83.
-
-
84.
-
first = 0;
-
85.
-
number = 1;
-
86.
-
k = 0;
-
87.
-
-
88.
-
for(i = 0; i <= ceil(size / 2); i++) {
-
89.
-
for(j = 0; j < 4; j++) {
-
90.
-
if(first != 0) // 행의 첫부분인지 검사
-
91.
-
k = i + 1;
-
92.
-
else
-
93.
-
k = i;
-
94.
-
-
95.
-
if(j % 3 == 0 && j != 0) // 3번의 로테이션 마다 사이즈가 1씩 줄어든다.
-
96.
-
size_tmp = size – i – 1;
-
97.
-
else
-
98.
-
size_tmp = size – i;
-
99.
-
-
100.
-
for(; k < size_tmp; k++) { // 실제적으로 배열에 값을 입력하는 부분
-
101.
-
array[i][k] = number;
-
102.
-
number++;
-
103.
-
first = 1;
-
104.
-
}
-
105.
-
array_rotation(size, array);
-
106.
-
}
-
107.
-
first = 0;
-
108.
-
}
-
109.
-
}