Created
October 1, 2013 04:40
-
-
Save cohalz/6773936 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#define maxRow 15 //最大行数 | |
int main(){ | |
int n,y,x; //正方行列の大きさ。 | |
int count = 0; //文字を入れ終わった回数。 | |
char arr[maxRow][maxRow]; //行列。 | |
char printChar = 'Z'; //初期位置の文字。ここをYやCにしても動くように対応。 | |
enum directions { UP,LEFT,DOWN,RIGHT }; | |
enum directions direction = UP; | |
printf("n = "); | |
while(scanf("%d", &n) != 1 || n < 3 || n > maxRow) { | |
printf("#matrix must be at least 3 and at most %d.\n",maxRow); | |
while(getchar() != '\n') {}; | |
printf("n = "); | |
} | |
for(y=0;y<n;y++) { | |
for(x=0;x<n;x++) { | |
arr[y][x] = '$'; | |
} | |
} | |
x = n/2; | |
y = n/2; | |
while(count != n*n) { | |
arr[y][x] = 'Z'-(('Z'-printChar+count)%26); | |
count++; | |
switch(direction) { | |
case UP: | |
y--; | |
if(arr[y][x-1] == '$') direction = LEFT; | |
break; | |
case LEFT: | |
x--; | |
if(arr[y+1][x] == '$') direction = DOWN; | |
break; | |
case DOWN: | |
y++; | |
if(arr[y][x+1] == '$') direction = RIGHT; | |
break; | |
case RIGHT: | |
x++; | |
if(arr[y-1][x] == '$') direction = UP; | |
break; | |
} | |
} | |
for(y=0;y<n;y++) { | |
for(x=0;x<n;x++) { | |
printf("%c ",arr[y][x]); | |
} | |
puts(""); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment