U E D R , A S I H C RSS

Random Walk2/Extreme Pair

첫번째 요구사항 버전

~cpp 
import unittest

class ManTestCase(unittest.TestCase):
    def setUp(self):
        self.man = Man()
        self.man.makeBoard(4, 3)
        
    def testMakeBoard(self):
        self.assertEquals([[0,0,0],[0,0,0],[0,0,0],[0,0,0]], self.man.board)

    def testMakeRoach(self):
        self.man.makeRoach(0,0,[1])
        self.assertEquals([[1,0,0],[0,0,0],[0,0,0],[0,0,0]], self.man.board)

    def testMoveRoach(self):
        self.man.makeRoach(0,0,[2,4])
        self.man.moveRoach()
        self.assertEquals([[1,1,0],[0,0,0],[0,0,0],[0,0,0]], self.man.board)
        self.man.moveRoach()
        self.assertEquals([[1,1,0],[0,1,0],[0,0,0],[0,0,0]], self.man.board)

    def testPierceRoach(self):
        self.man.makeRoach(0,0,[6,0])
        self.man.moveRoachAllJourney()
        self.assertEquals([[1,0,1],[0,0,0],[0,0,0],[0,0,1]], self.man.board)

    def testIsBoardAllPassed(self):
        self.assertEquals(False, self.man.isBoardAllPassed())
        self.man.board = [[1,2,1],[1,3,1],[1,4,1],[2,1,5]]
        self.assertEquals(True, self.man.isBoardAllPassed())
        
class Man:
    def __init__(self):
        self.direction = [(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]
        
    def makeBoard(self, row, col):
        self.row = row
        self.col = col
        self.board = [[0 for c in range(self.col)] for r in range(self.row)]
        
    def makeRoach(self, startRow, startCol, journey):
        self.currentRow = startRow
        self.currentCol = startCol
        self.journey = journey
        self.journeyCount = 0
        self.movingCount = 0
        
        self.board[self.currentRow][self.currentCol] += 1

    def moveRoach(self):
        moveRow, moveCol = self.direction[self.journey[self.journeyCount]]
        self.journeyCount += 1
        self.currentRow += moveRow
        self.currentCol += moveCol
        self.checkPierceRoach()
        self.board[self.currentRow][self.currentCol] += 1
        self.movingCount += 1

    def checkPierceRoach(self):
        if(self.currentRow == -1):
            self.currentRow = self.row - 1
        elif(self.currentRow == self.row):
            self.currentRow = 0
        if(self.currentCol == -1):
            self.currentCol = self.col - 1
        elif(self.currentCol == self.col):
            self.currentCol = 0
            
    def moveRoachAllJourney(self):
        while (len(self.journey) != self.movingCount or self.isBoardAllPassed()):
                self.moveRoach()

    def isBoardAllPassed(self):
        for r in range(self.row):
            for c in range(self.col):
                if(self.board[r][c] == 0):
                    return False
        return True

    def showBoard(self):
        print self.movingCount
        
        for r in range(self.row):
            for c in range(self.col):
                print self.board[r][c],
            print
                
if __name__ == '__main__':
    #unittest.main()
    row = int(raw_input())
    col = int(raw_input())
    startRow = int(raw_input())
    startCol = int(raw_input())
    journeyString = raw_input()
    journeyList = []
    for i in range(len(journeyString)):
        journeyList.append(int(journeyString[i]))
    man = Man()
    man.makeBoard(row, col)
    man.makeRoach(startRow, startCol, journeyList)
    man.moveRoachAllJourney()
    man.showBoard()
Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2009-05-27 07:09:19
Processing time 0.2046 sec