#include #include #include #define REPEAT 100 int iSudoku[9][9]; void ChangeMove(int iFstNumber, int iSndNumber, bool bLineCheck); // bLineCheck : TRUE : 행, FALSE : 열 void NewSudoku(void); // 스도쿠 초기화 void SudokuPrint(void); // 스도쿠 출력 함수 int main() { srand(time(NULL)); while(1) { NewSudoku(); int iRnd, iTemp, iTempFst, iTempSnd, iRepeat; for(iRepeat = 0; iRepeat <= REPEAT - 1; iRepeat++) { iRnd = (iTempFst = rand() % 3) + (iTempSnd = (rand() % 3) * 3); iTemp = ((iTemp = rand() % 3) != iTempFst) ? iTemp + iTempSnd : (iTemp + 1) % 3 + iTempSnd; ChangeMove(iRnd, iTemp, true); iRnd = (iTempFst = rand() % 3) + (iTempSnd = (rand() % 3) * 3); iTemp = ((iTemp = rand() % 3) != iTempFst) ? iTemp + iTempSnd : (iTemp + 1) % 3 + iTempSnd; ChangeMove(iRnd, iTemp, false); } SudokuPrint(); printf("Enter press is new Sudoku.\nE(x)it : "); if(getchar() == 'x') break; system("cls"); } return 0; } void ChangeMove(int iFstNumber, int iSndNumber, bool bLineCheck) { int iCounter, iSwapTemp; for(iCounter = 0; iCounter < 9; iCounter++) { if(bLineCheck) { iSwapTemp = iSudoku[iFstNumber][iCounter]; iSudoku[iFstNumber][iCounter] = iSudoku[iSndNumber][iCounter]; iSudoku[iSndNumber][iCounter] = iSwapTemp; } else { iSwapTemp = iSudoku[iCounter][iFstNumber]; iSudoku[iCounter][iFstNumber] = iSudoku[iCounter][iSndNumber]; iSudoku[iCounter][iSndNumber] = iSwapTemp; } } } void NewSudoku(void) { int iTemp, iLine = 0, iTier = 0; while(iLine <= 8) { iSudoku[0][iLine] = rand() % 9 + 1; for(iTemp = 0; iTemp < iLine; iTemp++) { if(iSudoku[0][iTemp] == iSudoku[0][iLine]) --iLine; } ++iLine; } for(iTemp = 1; iTemp <= 8; iTemp++) { iTier = ((iTier + 3) % 8 != 0) ? (iTier + 3) % 8 : 8; for(iLine = 0; iLine < 9; iLine++) { iSudoku[iTier][(((iLine - iTemp) < 0) ? iLine + 8 - (iTemp - 1) : iLine - iTemp)] = iSudoku[0][iLine]; } } } void SudokuPrint(void) { int iTier, iLine; for(iTier = 0; iTier < 9; iTier++) { for(iLine = 0; iLine < 9; iLine++) { printf("%d ", iSudoku[iTier][iLine]); } printf("\n"); } printf("\n"); }
소스 주소 : http://blueb24.cafe24.com/c/sudoku.zip