谷歌Codejam奶昔示例:找不到错误

时间:2012-02-24 20:29:35

标签: python

我正在练习这些问题: http://code.google.com/codejam/contest/32016/dashboard#s=p1&a=1

我相信我有正确的解决方案,(即使在手动检查输入和输出之后); 我仍然继续Incorrect Output

有人能指出我错在哪里吗?

def process_file(file):
    fsock = open(file)
    text = fsock.read()
    fsock.close()
    lines = text.split('\n')
    return lines


def process_lines(lines):
    cur = 1
    ans = []
    while cur < len(lines) - 1:
        N = int(lines[cur])
        cur += 1
        M = int(lines[cur])
        cur += 1
        struct = []
        for i in range(0, M):
            cust_pref = [int(n) for n in lines[cur].split(' ')]
            cust_drinks = [a-1 for a in cust_pref[1::2]]
            cust_drinks_malt_pref = cust_pref[2::2]
            cust_choice = [(cust_drinks[i], cust_drinks_malt_pref[i]) for i in range(0, len(cust_drinks))]
            cur += 1
            struct.append(cust_choice)
        ans.append((N, struct))
    return ans


def process_case(case):
    milkshake_menu = [0] * case[0] # our default menu
    i = 0
    impossible = False
    while i < len(case[1]): # i represents the customer number, case[1] represents customers
        acceptable = False
        customer = case[1][i]
        for drink_preferred in customer:
            if milkshake_menu[drink_preferred[0]] == drink_preferred[1]:
                acceptable = True
                i += 1 # ok, next customer
                break
        if not acceptable:
            for drink_preferred in customer:
                # find a malted preference
                if drink_preferred[1] == 1 and milkshake_menu[drink_preferred[0]] != 1:
                    # he needs a malted one
                    milkshake_menu[drink_preferred[0]] = 1
                    # but then we have to test previous customers, reset i
                    i = 0
                    break
                #if you have come here, the customer does not have a malted preference, or has a unmalted preference that conflicts with other customer
                impossible = True
            #impossible is True, break outer loop
            if impossible:
                break
    if impossible:
       return "IMPOSSIBLE"
    else:
       return " ".join([str(n) for n in milkshake_menu])



if __name__ == "__main__":
    import sys
    filename = sys.argv[1]
    lines = process_file(filename)
    inp = process_lines(lines)
    for k, v in enumerate(inp):
        a = process_case(v)
        print "Case #%d: %s" % (k + 1, a) 

Pastebin输出:http://pastebin.com/uXJQKbSr

1 个答案:

答案 0 :(得分:2)

有一件看起来很奇怪的事情就是在循环中

        for drink_preferred in customer:
            # find a malted preference
            if drink_preferred[1] == 1 and milkshake_menu[drink_preferred[0]] != 1:
                # he needs a malted one
                milkshake_menu[drink_preferred[0]] = 1
                # but then we have to test previous customers, reset i
                i = 0
                break
            impossible = True

我希望这是

        for drink_preferred in customer:
            # find a malted preference
            if drink_preferred[1] == 1 and milkshake_menu[drink_preferred[0]] != 1:
                # he needs a malted one
                milkshake_menu[drink_preferred[0]] = 1
                # but then we have to test previous customers, reset i
                i = 0
                break
        else:
            impossible = True

我认为,在检查所有可能性之前,您将判断任务是不可能的。