Показать сообщение отдельно

  #10  
Старый 30.05.2007, 02:03
nerezus
Pagan Heart
Регистрация: 12.08.2004
Сообщений: 3,791
Провел на форуме:
6490435

Репутация: 2290


Отправить сообщение для nerezus с помощью ICQ
По умолчанию

Код:
# -*- coding: cp1251 -*-
from UserString import MutableString

lines = [
    "1100111110",
    "0011100010",
    "0010000111",
    "0010110010",
    "0010000010",
    "0011111110"
    ]

lines.insert(0, MutableString("0"*len(lines[0])))
lines.append(MutableString("0"*len(lines[0])))

for i in range(len(lines)):
    lines[i] = MutableString("0" + lines[i] + "0")
    
def convert1To2():
    done = False
    while not done:
        done = True
        for i in range(len(lines)):
            for j in range(len(lines[i])):
                if lines[i][j] == '1' and \
                       (lines[i+1][j] == '2' or \
                        lines[i-1][j] == '2' or \
                        lines[i][j+1] == '2' or \
                        lines[i][j-1] == '2'):
                    lines[i][j] = '2'
                    done = False

amount = 0

for i in range(len(lines)):
    for j in range(len(lines[i])):
        if lines[i][j] == '1':
            lines[i][j] = '2'
            amount += 1
            convert1To2()

print "Итого: ", amount
На питоне

Последний раз редактировалось nerezus; 30.05.2007 в 02:06..