Results 1 to 2 of 2

Thread: [Python] What's wrong with this code

  1. #1
    Join Date
    Jan 2012
    Posts
    2,568
    Mentioned
    35 Post(s)
    Quoted
    356 Post(s)

    Default [Python] What's wrong with this code

    EDIT: fixed, apparently configuration is valid only if it has strictly 8 queens...


    I'm trying to solve this https://open.kattis.com/problems/8queens
    my code passed the 2 public test cases but failed the hidden test cases (no additional info given on what it failed)
    Looked through it several times, cant figure out what the bug is :S
    link: http://pastebin.com/WCv4WZyc
    Code:
    import sys
    d = []
    for l in sys.stdin:  #
        d.append(list(l[i] for i in range(len(l)-1)))
     
    res = False
    for j in range(8):
        if d[j].count('*') > 1: #check row
            res = True
            break
       
        r = False
        for x in d:
            if x[j]=='*': #check column
                if r: #if count > 1, break
                    res = True
                    break
                r = True #if first count, set flag true
       
        r,r2,r3,r4=False, False, False, False
        for i in range(8-j):
            if d[i][i+j]=='*': #check diagonal towards right
                if r:
                    res = True
                    break
                r = True
               
            if d[i+j][i]=='*': #check diagonal towards btm
                if r2:
                    res = True
                    break
                r2 = True
     
            if d[7-i-j][i]=='*':
                if r3:
                    res = True
                    break
                r3 = True
     
            if d[7-i][i+j]=='*':
                if r4:
                    res = True
                    break
                r4 = True
        if res:
            break
    if res:
        print('invalid')
    else:
        print('valid')
    Last edited by riwu; 09-05-2015 at 06:59 AM.

  2. #2
    Join Date
    Sep 2008
    Location
    Not here.
    Posts
    5,422
    Mentioned
    13 Post(s)
    Quoted
    242 Post(s)

    Default

    This is a simple solution and you just need to think about it more.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •