KY12 玛雅人的密码
def getSwapNum(txt):
myQueue = list()
myQueue.append((txt,0))
while len(myQueue)>0:
item = myQueue.pop(0)
txt, count = item
if '2012' in txt:
return count
else:
for i in range(len(txt)-1):
newTxt = txt[0:i] + txt[i+1] + txt[i] + txt[i+2:]
myQueue.append((newTxt,count+1))
def main():
N = int(input().strip())
txt = input().strip()
if txt.count('2') < 2 or txt.count('0') < 1 or txt.count('1') < 1:
print(-1)
else:
num = getSwapNum(txt)
print(num)
if __name__=="__main__":
try:
while(True):
main()
except:
pass
- 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