'Coding > CPP 삽질기' 카테고리의 다른 글
함수 포인터 (0) | 2009.01.08 |
---|---|
[CPP] STL에서 List의 sorting 방법 (0) | 2008.10.30 |
Copy & Paste 신공 (0) | 2008.03.08 |
함수 포인터 (0) | 2009.01.08 |
---|---|
[CPP] STL에서 List의 sorting 방법 (0) | 2008.10.30 |
Copy & Paste 신공 (0) | 2008.03.08 |
#include <iostream>
#include <list>
using namespace std;
class CData {
public:
CData (int x = 0) { data = x; }
void set (int x) { data = x; }
int get (void) { return data ; }
// 방법 1: 비교를 위한 함수를 만든다.
int operator()( CData &lhv, CData &rhv) const { return (lhv.get() > rhv.get()); }
// 방법 2: < 연산자를 오버로딩 한다.
int operator<( CData &rhv) const { return (this->data < rhv.get()); }
private:
int data;
};
void process(void);
void prinfList( list<CData> myList );
int main (int argc, char **argv)
{
process();
getchar();
return 0;
}
void process (void)
{
list<CData> myData;
myData.push_front(10);
myData.push_front(17);
myData.push_front(11);
myData.push_front(1);
myData.push_front(20);
prinfList( myData );
// STL의 list는 제너릭한 sort를 사용할 수 없다.
// 멤버함수를 호출하면 < 연산자를 비교 함수로 쓴다.
myData.sort(); // method 2
prinfList( myData );
// 직접 비교 함수를 넣을 수도 있다.
myData.sort(CData()); // metod 1
prinfList( myData );
}
void prinfList( list<CData> myList )
{
list<CData> :: iterator iter;
for (iter = myList.begin(); iter != myList.end(); iter++)
cout << iter->get() << " ";
cout << endl;
}
[CPP] Linux Thread Example Code (0) | 2008.12.14 |
---|---|
Copy & Paste 신공 (0) | 2008.03.08 |
연산자 우선 순위 (0) | 2008.01.13 |
중경 대한민국 임시 정부에서 본 글귀 (0) | 2009.10.13 |
---|---|
중경 대한민국 임시정부에서 본 태극기 (0) | 2008.04.08 |
중경의 대한민국 임시정부 (0) | 2008.04.08 |
2009년 1월 1일 태종대에서 본 일출 (1) | 2009.01.01 |
---|---|
태종대의 여름 (0) | 2008.09.09 |
태종대에 남겨진 흔적들 (0) | 2008.09.09 |
#!/usr/local/bin/python
#-*- coding: cp949 -*-
# date : 2006. 10. 15
#
# 스도쿠를 풀어주는 스크립트
import random
class Sodoku:
def __init__(self):
self.timer = 0
self.board = []
self.tmpBoard=[]
for y in range(0, 82):
self.board.append(0)
self.tmpBoard.append(511)
def display(self):
for y in range(0, 9):
for x in range(0, 9):
idx = y* 9 + x + 1;
if self.board[idx] != 0:
print "%d" %(self.board[idx]),
else:
print "_",
print '|',
print '\n'
def checkSquare(self, sx, sy):
sum = 0;
for y in range(0, 3):
for x in range(0, 3):
if ( int(self.board[ (y+sy)*9+(x+sx)+1]) > 0):
sum |= (1 << (int(self.board[ (y+sy)*9+(x+sx)+1]-1)))
return sum
def checkBoard(self):
ret = 0;
for y in range(0, 9):
sum = 0;
for x in range(0, 9):
if ( int(self.board[y*9+x+1]) > 0):
sum |= (1 << (int(self.board[y*9+x+1]-1)))
if (sum != 511):
ret = 1
for x in range(0, 9):
sum = 0;
for y in range(0, 9):
if ( int(self.board[y*9+x+1]) > 0):
sum |= (1 << (int(self.board[y*9+x+1]-1)))
if (sum != 511):
ret = 2
for y in range(0, 3):
for x in range(0, 3):
sum = self.checkSquare(x*3, y*3)
if (sum != 511):
ret = 3
return ret
def readData(self):
fp = open("sudoku.dat", 'r')
self.board = map(int, fp.read().split(','))
fp.close()
for y in range(0, 82):
self.tmpBoard[y] =511
def guessCount(self, num):
count = 0
for i in range(0, 9):
if ( (1 << i) & num ) != 0:
count += 1
return count
def findStartPos(self):
sx = 0
sy = 0
mvalue = 511
mcount = 10
for y in range(0,9):
for x in range(0,9):
if (self.board[y * 9 + x + 1] == 0):
temp = self.guessCount(self.tmpBoard[y*9 + x + 1] )
if (mcount > temp):
mvalue = self.tmpBoard[y*9 + x + 1]
mcount = temp
sx = x
sy = y
return (sx, sy, mvalue)
def isFull(self):
count = 0
for y in range(0,9):
for x in range(0,9):
if (self.board[y * 9 + x + 1] != 0):
count += 1
return count == 81
def solve(self):
if (self.checkBoard() != 0):
if (self.isFull()):
return 0
#get position
(x, y, num) = self.findStartPos()
# set number
for i in range(0, 9):
if ( (1 << i) & num ) != 0:
self.board[y*9 + x + 1] = i+1
# checkTempBoard
self.checkWidthTempBoard(x, y)
self.checkHeightTempBoard(x, y)
self.checkSquareTempBoard(x/3, y/3, x, y)
if (self.solve() == 1):
return 1
else:
self.unCheckWidthTempBoard(x, y)
self.unCheckHeightTempBoard(x, y)
self.unCheckSquareTempBoard(x/3, y/3, x, y)
self.board[y*9 + x + 1] = 0
return 0
else:
return 1
def checkTempBoard(self):
for y in range(0, 9):
for x in range(0, 9):
if (self.board[y * 9 + x + 1] != 0):
self.checkWidthTempBoard(x, y)
self.checkHeightTempBoard(x, y)
self.checkSquareTempBoard(x/3, y/3, x, y)
def checkWidthTempBoard(self, sx, sy):
for x in range(0,9):
if (self.board[sy * 9 + x + 1] == 0):
self.tmpBoard[sy * 9 + x + 1] &= ~(1 << (self.board[sy * 9 + sx + 1] - 1))
def checkHeightTempBoard(self, sx, sy):
for y in range(0,9):
if (self.board[y * 9 + sx + 1] == 0):
self.tmpBoard[y * 9 + sx + 1] &= ~(1 << (self.board[sy * 9 + sx + 1] - 1))
def checkSquareTempBoard(self, sx, sy, tx, ty):
for y in range(0,3):
for x in range(0,3):
if (self.board[(y + sy) * 9 + sx + x + 1] == 0):
self.tmpBoard[(y + sy * 3) * 9 + sx * 3 + x + 1] &= ~(1 << (self.board[ty * 9 + tx + 1] - 1))
def unCheckWidthTempBoard(self, sx, sy):
for x in range(0,9):
if (self.board[sy * 9 + x + 1] == 0):
self.tmpBoard[sy * 9 + x + 1] |= (1 << (self.board[sy * 9 + sx + 1] - 1))
def unCheckHeightTempBoard(self, sx, sy):
for y in range(0,9):
if (self.board[y * 9 + sx + 1] == 0):
self.tmpBoard[y * 9 + sx + 1] |= (1 << (self.board[sy * 9 + sx + 1] - 1))
def unCheckSquareTempBoard(self, sx, sy, tx, ty):
for y in range(0,3):
for x in range(0,3):
if (self.board[(y + sy) * 9 + sx + x + 1] == 0):
self.tmpBoard[(y + sy * 3) * 9 + sx * 3 + x + 1] |= (1 << (self.board[ty * 9 + tx + 1] - 1))
def generate(self):
self.readData()
self.checkTempBoard()
def main():
sodoku = Sodoku()
sodoku.generate()
sodoku.display()
sodoku.solve()
print "-"*60
sodoku.display()
#----------------------------------------------------------
# main
if __name__ == "__main__":
main()
[실행결과]
6 | _ | 9 | _ | _ | _ | _ | 4 | 3 |
_ | _ | _ | 2 | 9 | _ | 7 | 1 | _ |
_ | _ | 4 | _ | 1 | _ | _ | 5 | 8 |
5 | _ | _ | _ | _ | 8 | _ | 6 | _ |
8 | _ | 7 | 1 | _ | 9 | 4 | _ | 5 |
_ | 3 | _ | 7 | _ | _ | _ | _ | 2 |
1 | 5 | _ | _ | 8 | _ | 3 | _ | _ |
_ | 4 | 8 | _ | 6 | 2 | _ | _ | _ |
7 | 2 | _ | _ | _ | _ | 6 | _ | 9 |
-----------------------------
6 | 1 | 9 | 8 | 7 | 5 | 2 | 4 | 3 |
3 | 8 | 5 | 2 | 9 | 4 | 7 | 1 | 6 |
2 | 7 | 4 | 6 | 1 | 3 | 9 | 5 | 8 |
5 | 9 | 2 | 4 | 3 | 8 | 1 | 6 | 7 |
8 | 6 | 7 | 1 | 2 | 9 | 4 | 3 | 5 |
4 | 3 | 1 | 7 | 5 | 6 | 8 | 9 | 2 |
1 | 5 | 6 | 9 | 8 | 7 | 3 | 2 | 4 |
9 | 4 | 8 | 3 | 6 | 2 | 5 | 7 | 1 |
7 | 2 | 3 | 5 | 4 | 1 | 6 | 8 | 9 |
WxPython - HelloWorld (0) | 2008.12.22 |
---|---|
[조각코드] 숫자 배열 읽어서 리스트에 저장하는 코드 (0) | 2008.10.06 |
간단한 메모장2 (0) | 2008.09.17 |
Sudoku를 풀어주는 스크립트 (0) | 2008.10.09 |
---|---|
간단한 메모장2 (0) | 2008.09.17 |
간단한 메모장 (0) | 2008.09.14 |
def LoadData():
if os.path.exists(filename):
fp = open(filename, 'r')
tempFileData = fp.read()
fp.close()
text.insert(1.0, tempFileData.decode('cp949'))
def SaveData():
textData = text.get(1.0, END).encode('cp949')
fp = open(filename, 'w')
fp.write(textData)
fp.close()
def ShowInfo():
tkMessageBox.showinfo("Information","http://chobocho.com\nVersion 0.2")
#----------------------------------------------------------
# main
if __name__ == "__main__":
root = Tk()
text = Text(root, width=50, height = 20)
text.pack()
append_button = Button(root, text="Append", command = AppendData)
save_button = Button(root, text="Save", command = SaveData)
clear_button = Button(root, text="Clear", command = ClearText)
load_button = Button(root, text="Load", command = LoadData)
info_button = Button(root, text="Info", command = ShowInfo)
info_button.pack(side=RIGHT)
load_button.pack(side=RIGHT)
clear_button.pack(side=RIGHT)
save_button.pack(side=RIGHT)
append_button.pack(side=RIGHT)
root.mainloop()
[조각코드] 숫자 배열 읽어서 리스트에 저장하는 코드 (0) | 2008.10.06 |
---|---|
간단한 메모장 (0) | 2008.09.14 |
Fractal Tree (0) | 2008.06.21 |
간단한 메모장2 (0) | 2008.09.17 |
---|---|
Fractal Tree (0) | 2008.06.21 |
파일을 HEX 값으로 보여 주는 소스 (0) | 2008.05.19 |
Ahnlab 바이러스 신고센터 사용기 (0) | 2009.04.08 |
---|---|
신기전기화차 (0) | 2008.09.11 |
수다 (0) | 2008.08.31 |
블로그 랭킹 (0) | 2008.09.12 |
---|---|
수다 (0) | 2008.08.31 |
2008 베이징 올림픽 기간 구글 로고 모음 (0) | 2008.08.24 |