简体   繁体   中英

How to do the reversed eight queen problems (check if any pair can eat ea other)?

Give the position of 8 queens on the chessboard. Print YES if at least one pair of queens hit each other. If not print out NO.

so here's my code but when checking if these queens can hit ea other diagonally, python says

unsupported operand type(s) for Sub: "str" and "str"

a, b = input().split()
c, d = input().split()
e, f = input().split()
g, h = input().split()
i, j = input().split()
k, l = input().split()
m, n = input().split()
o, p = input().split()
#check cross or straight
if a==c or c==e or e==g or g==i or i==k or k==m or m==o or o==a or b==d or d==f or f==h or h==j or j==l or l==n or n==p or p==b:
    print("YES")
#check diagonally
elif a==b and c==d or c==d and e==f or e==f and g==h or g==h and i==j or i==j and k==l or k==l and a==b:
    print("YES")
elif abs(int(a-b))==abs(int(c-d)) or abs(int(c-d))==abs(int(e-f)) or abs(int(e-f))==abs(int(g-h)) or abs(int(g-h))==abs(int(i-j)) or abs(int(i-j)) == abs(int(k-l)) or abs(int(k-l))==abs(int(a-b)):
    print("YES")
else:
    print("NO")

these inputs are splited by spaces (coordination of each queen on the chess board)

example coordination

1 3
4 8
6 1
5 5
2 7
8 6
7 4
3 2

Use abs(int(a)-int(b)) instead of abs(int(ab))

You are trying to convert ab into an int, but two strings cannot be substracted.

Always look at the error stack, not just the description of the error type:

TypeError                                 Traceback (most recent call last)
<ipython-input-6-23e2f43d3b2f> in <module>()
     13 elif a==b and c==d or c==d and e==f or e==f and g==h or g==h and i==j or i==j and k==l or k==l and a==b:
     14     print("YES")
---> 15 elif abs(int(a-b))==abs(int(c-d)) or abs(int(c-d))==abs(int(e-f)) or abs(int(e-f))==abs(int(g-h)) or abs(int(g-h))==abs(int(i-j)) or abs(int(i-j)) == abs(int(k-l)) or abs(int(k-l))==abs(int(a-b)):
     16     print("YES")
     17 else:

TypeError: unsupported operand type(s) for -: 'str' and 'str'

there you can see the error is happening in the line

---> 15 elif abs(int(a-b))==abs(int(c-d)) or abs(int(c-d))==abs(int(e-f)) or abs(int(e-f))==abs(int(g-h)) or abs(int(g-h))==abs(int(i-j)) or abs(int(i-j)) == abs(int(k-l)) or abs(int(k-l))==abs(int(a-b)):

where the operations involving to strings (and an operand) are the ones of the kind ab, cd, etc.

Bonus:

Since it make sense to use the coordinates always as numbers, you could do

def get_coords():
  x, y = input().split()
  x = int(x)
  y = int(y)
  return x, y

a, b = get_coords()
c, d = get_coords()
...

And then the rest of the script becomes a bit shorter and easier to read.

So you'll basically convert everything into a list so it will be easier to check

rows = []
cols = []
for _ in range(8):
    r, c = [int(v) for v in input().split()]
    rows.append(r)
    cols.append(c)
count = 0
for i in range(8):
    for j in range(i+1, 8):
        if rows[i] == rows[j] or cols[i]==cols[j] or abs(rows[i]-rows[j])==abs(cols[i]-cols[j]):
            count += 1
if count==0:
    print("NO")
else:
    print("YES")
    

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM