1

私はpythonが初めてです。私のコードは無限ループに陥り、追加と印刷を続けますnumberCycles。私の C++ のロジックは問題なく動作します。理由を特定するのを手伝ってくれませんか?

  • 入力の最初の行は、シミュレーションの数です。
  • 次の行は、1 回の生殖周期の分数です。
  • 次の行は行数 ( x) で、その後に 1 つのスペースが続き、その後に列数 ( ) が続きますy
  • y 行の次のグループには、xいくつかの文字があり、単一のピリオド ( .) は空白をB表し、単一のキャピトルは、最初のバニーがup, right, down,left方向に複製しようとすることを表します。既存のバニーがある場合は、スリープ状態になります。

入力.txt

2     # 2 simulations
5     # 5 minutes/cycle
3 3   # 3*3 map
...
.B.
...
1
4 4
B.BB
..B.
...
B.B

Spot.py

#!/usr/bin/env python

class Spot(object):
    isBunny = bool()
    nextCycle = 0
    UP = 0
    RIGHT = 1
    DOWN = 2
    LEFT = 3
    SLEEP = 4

    def __init__(self, newIsBunny):
        self.isBunny = newIsBunny
        self.nextCycle = self.UP

    def setNextCycle(self):
        if (self.nextCycle != self.SLEEP):
            self.nextCycle += 1

    def getNextCycle(self):
        return self.nextCycle

    def getIsBunny(self):
        return self.isBunny

    def makeBunny(self):
        if not self.isBunny:
            self.nextCycle = self.UP
        self.isBunny = True

バニー.py

#!/usr/bin/env python
from Spot import Spot
import os

class Bunny(object):    
    @classmethod
    def main(cls, args):
        with open(os.path.expanduser('~/Desktop/input.txt')) as f: 
            numSims = int(f.readline()) 
            myMap = []  
            print numSims
            minPerCycle= int(f.readline()) 
            print minPerCycle
            for k in xrange(numSims): 
                xyLine= f.readline()
                row = int(xyLine.split()[0]) 
                col = int(xyLine.split()[1])  
                print row,col
                for i in range(0,row): 
                    myLine = f.readline() 
                    myMap.append([]) 
                    for j in range(0,col): 
                        myMap[i].append(Spot(myLine[j] == 'B')) 

                numCycles = 1

                if cls.isFilled(row,col,myMap):
                    numCycles = 0

                while not cls.isFilled(row,col,myMap):
                    numCycles += 1
                    print numCycles
                    for m in range(0,row): 
                        for n in range(0,col): 
                            if myMap[m][n].getIsBunny():
                                if myMap[m][n].getNextCycle() == Spot.UP:
                                    if m>0:
                                        myMap[m-1][n].makeBunny()
                                    break
                                elif myMap[m][n].getNextCycle() == Spot.RIGHT:
                                    if n<col-1:
                                        myMap[m][n+1].makeBunny()
                                    break
                                elif myMap[m][n].getNextCycle() == Spot.DOWN:
                                    if m<row-1:
                                        myMap[m+ 1][n].makeBunny()
                                    break
                                elif myMap[m][n].getNextCycle() == Spot.SLEEP:
                                    if n>0:
                                        myMap[m][n- 1].makeBunny()
                                    break
                            myMap[m][n].setNextCycle() 
                time = numCycles * minPerCycle
                print "It took " , time , " minutes for the bunnies to take over the world!\n"
                del myMap[:]
            f.close()

    @classmethod
    def isFilled(cls,row,col,myMap):
        for a in range(0,row): 
            for b in range(0,col): 
                if not myMap[a][b].getIsBunny():
                    return False
        return True


if __name__ == '__main__':
    import sys
    Bunny.main(sys.argv)
4

1 に答える 1