Study
2008. 10. 6. 04:22
magic_square result !
#include <stdio.h> #define SWAP(x, y, temp) (temp=x, x=y, y=temp) int dataInput(int* blank, char * argv[]) ; // 기본 데이터 입력 int substitute(int* blank, int i, int n) ; // 재귀적 입력 int check() ; // 마방진의 유효성 검사 void output() ; // 결과 출력 struct point { int x ; int y ; } ; int square[5][5]; // 마방진을 담을 배열 struct point coordinate[25] ; // 마방진에 숫자가 없는 곳의 좌표를 저장하기 위한 배열 int main(int argc, char * argv[]) { int blank[25]; // 마방진에 없는 숫자를 담을 배열 int count = dataInput(blank, argv); if(count == -1) return 0 ; else substitute(blank, 0, count - 1); return 0 ; } int dataInput(int* blank, char * argv[]) { FILE* file; int i, x, y, count ; int check_blank[26]; // 마방진에 없는 숫자를 판별하는 배열 file = fopen(argv[1], "r"); if(file == NULL) { printf("File open failed.\n") ; return -1 ; } for(i = 0 ; i < 26 ; i++) check_blank[i] = 0 ; for(x = 0 ; x < 5 ; x++) { for(y = 0 ; y < 5; y++) fscanf(file, "%d", &square[x][y]); } fclose(file); /* 최초의 반복문으로 배열내에 0이 들어있는 좌표를 새로운 배열에 저장하고 마방진 내에 없는 값을 blank 배열에 저장 */ for(x = 0, count = 0 ; x < 5 ; x++) { for(y = 0 ; y < 5 ; y++) { if(square[x][y] == 0) { coordinate[count].x = x; coordinate[count].y = y; count ++ ; } else check_blank[square[x][y]] = 1 ; } } for(i = 1, count = 0 ; i <= 26 ; i++) { if(check_blank[i] == 0) { blank[count] = i; count ++; } } return count ; } int substitute(int* blank, int i, int blankCount) { int j, temp; if(i == blankCount) { for(j = 0 ; j <= blankCount ; j++) { square[coordinate[j].x][coordinate[j].y] = blank[j]; } if(check() == 12) { output() ; return 0 ; } } else { for(j = i ; j <= blankCount ; j++) { SWAP(blank[i], blank[j], temp); substitute(blank, i + 1, blankCount) ; SWAP(blank[i], blank[j], temp); } } return 0; } int check() { int x , y ; int solution = 0; int checkSum ; //↓ for(x = 0 ; x < 5 ; x++) { for(y = 0, checkSum = 0 ; y < 5 ; y++ ) checkSum += square[x][y] ; if(checkSum == 65) solution += 1 ; } //→ for(y = 0 ; y < 5; y++) { for(x = 0, checkSum = 0 ; x < 5 ; x++) checkSum += square[x][y] ; if(checkSum == 65) solution += 1 ; } //↘ for(x = 0, checkSum = 0 ; x < 5 ; x++) checkSum += square[x][x]; if(checkSum == 65) solution += 1 ; //↙ for(x = 4, checkSum = 0, y = 0 ; x >= 0 ; x--) { checkSum += square[x][y]; y++; } if(checkSum == 65) solution += 1 ; return solution ; } // 완성된 마방진을 출력 void output() { FILE* file; int x, y ; file = fopen(".\\output.txt", "wr"); for(x = 0; x < 5; x ++) { for(y = 0; y < 5; y++) fprintf(file, "%d ", square[x][y]); fprintf(file, "\n\n"); } fclose(file); printf("Output Answer.\n") ; return ; }
라스君이 코딩하고 유미르님이 기름기를 쫙 뺀, 마방진 빈칸 메우기 소스,,,
물론 제출 한건 기름기가 빠지기 전의 소스다,
공유하기
URL 복사
카카오톡 공유
페이스북 공유
엑스 공유
게시글 관리
구독하기
솔직한 목소리
blog
profile
tag cloud
travel log
guest book
by
bslime
전체보기
(52)
목소리
(1)
영화
(0)
음악
(0)
Study
(34)
Security
(12)
Web
(2)
WLAN
(7)
Network
(1)
System
(1)
Wargame
(5)
Web
(0)
Reverse Engineering
(3)
Crypto
(2)
잘했어요
삽질
Algorithm
CTF
티스토리
padocon
초대장
Maze
폐인
미로
«
2025/04
»
일
월
화
수
목
금
토
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
darktoil.
인생여전龍군.
슝이.
MR-J@.
미움.
vvipbeom.
Copyright ⓒ 2010.
bslime
, Skin designed by
Creasmworks.
All rights reserved.
티스토리툴바
닫기
단축키
내 블로그
내 블로그 - 관리자 홈 전환
Q
Q
새 글 쓰기
W
W
블로그 게시글
글 수정 (권한 있는 경우)
E
E
댓글 영역으로 이동
C
C
모든 영역
이 페이지의 URL 복사
S
S
맨 위로 이동
T
T
티스토리 홈 이동
H
H
단축키 안내
Shift
+
/
⇧
+
/
* 단축키는 한글/영문 대소문자로 이용 가능하며, 티스토리 기본 도메인에서만 동작합니다.