我正在训练一个网站,要求我制作一个程序,要求两个矩形的坐标,并检查矩形是否相交。然后,当我发送程序时,网站会测试几次。它要求A矩形的x min,x max,y min和y max,然后是B矩形。
这就是我的所作所为:
xmin_a = int(input())
xmax_a = int(input())
ymin_a = int(input())
ymax_a = int(input())
xmin_b = int(input())
xmax_b = int(input())
ymin_b = int(input())
ymax_b = int(input())
if xmin_a < xmax_b <= xmax_a and (ymin_a < ymax_b <= ymax_a or ymin_a <= ymin_b < ymax_a):
print('YES')
elif xmin_a <= xmin_b < xmax_a and (ymin_a < ymax_b <= ymax_a or ymin_a <= ymin_b < ymax_a):
print('YES')
elif xmin_b < xmax_a <= xmax_b and (ymin_b < ymax_a <= ymax_b or ymin_b <= ymin_a < ymax_b):
print('YES')
elif xmin_b <= xmin_a < xmax_b and (ymin_b < ymax_a <= ymax_b or ymin_b <= ymin_a < ymax_b):
print('YES')
else:
print('NO')
不幸的是,它不起作用,我不明白为什么。有什么想法吗?
PS:http://data.france-ioi.org/Task/7b0ee4fb57949c3db1f694afcef9d1a1//exemple.png
答案 0 :(得分:1)
我写了一个生成随机方块的短程序,并用你的函数测试它们。按控件生成新的方块,直到看到失败的东西。我做了一些快速测试,看到一些案例失败了。如果它们具有完全相同的边界,则需要考虑它们是否被计为交叉。
像这样举例:
import numpy as np
import matplotlib.pyplot as plt
from random import randint
def caseTest(xmin_a, xmax_a, ymin_a, ymax_a, xmin_b, xmax_b, ymin_b, ymax_b):
if xmin_a < xmax_b <= xmax_a and (ymin_a < ymax_b <= ymax_a or ymin_a <= ymin_b < ymax_a):
print('YES'),
elif xmin_a <= xmin_b < xmax_a and (ymin_a < ymax_b <= ymax_a or ymin_a <= ymin_b < ymax_a):
print('YES'),
elif xmin_b < xmax_a <= xmax_b and (ymin_b < ymax_a <= ymax_b or ymin_b <= ymin_a < ymax_b):
print('YES'),
elif xmin_b <= xmin_a < xmax_b and (ymin_b < ymax_a <= ymax_b or ymin_b <= ymin_a < ymax_b):
print('YES'),
else:
print('NO'),
print(xmin_a, xmax_a, ymin_a, ymax_a, xmin_b, xmax_b, ymin_b, ymax_b)
def createRandomSquares():
fig = plt.gcf()
fig.clf()
xmin_a = randint(0, 9)
xmax_a = randint(xmin_a + 1, 10)
ymin_a = randint(0, 9)
ymax_a = randint(ymin_a + 1, 10)
xmin_b = randint(0, 9)
xmax_b = randint(xmin_b + 1, 10)
ymin_b = randint(0, 9)
ymax_b = randint(ymin_b + 1, 10)
caseTest(xmin_a, xmax_a, ymin_a, ymax_a, xmin_b, xmax_b, ymin_b, ymax_b)
sqr1_x = [xmin_a, xmax_a, xmax_a, xmin_a, xmin_a]
sqr1_y = [ymin_a, ymin_a, ymax_a, ymax_a, ymin_a]
sqr2_x = [xmin_b, xmax_b, xmax_b, xmin_b, xmin_b]
sqr2_y = [ymin_b, ymin_b, ymax_b, ymax_b, ymin_b]
plt.plot(sqr1_x, sqr1_y)
plt.plot(sqr2_x, sqr2_y)
ax = plt.gca()
ax.set_ylim(min([ymin_b, ymin_a]) - 1, max([ymax_a, ymax_b]) + 1)
ax.set_xlim(min([xmin_b, xmin_a]) - 1, max([xmax_a, xmax_b]) + 1)
fig.canvas.draw()
def key_event(event):
if (event.key == 'control'):
createRandomSquares()
fig = plt.figure()
fig.canvas.mpl_connect('key_press_event', key_event)
plt.show()
这个没有 - 显然应该是。