Ssyrovatkin/task1 — различия между версиями

Материал из DISCOPAL
Перейти к: навигация, поиск
(Новая страница: «*Codechef/TOURBUS <source lang="python"> places_coords = [] roads = [] def orientation(p, q, r): val = (q[1] - p[1]) * (r[0] - q[0]) - (…»)
 
 
(не показаны 2 промежуточные версии этого же участника)
Строка 2: Строка 2:
  
 
<source lang="python">
 
<source lang="python">
    places_coords = []
 
    roads = []
 
  
    def orientation(p, q, r):
+
places_coords = []
        val = (q[1] - p[1]) * (r[0] - q[0]) - (q[0] - p[0]) * (r[1] - q[1])
+
roads = []
  
        return val > 0
+
def orientation(p, q, r):
 +
    val = (q[1] - p[1]) * (r[0] - q[0]) - (q[0] - p[0]) * (r[1] - q[1])
  
     def cross(a, b):
+
     return val > 0
        """check if crosses p1q1 and p2q2"""
+
        p1, q1, p2, q2 = places_coords[a[0]], places_coords[a[1]], places_coords[b[0]], places_coords[b[1]]
+
        o1 = orientation(p1, q1, p2)
+
        o2 = orientation(p1, q1, q2)
+
        o3 = orientation(p2, q2, p1)
+
        o4 = orientation(p2, q2, q1)
+
  
        # common case
+
def cross(a, b):
        if o1 != o2 and o3 != o4:
+
    """check if crosses p1q1 and p2q2"""
            return True
+
    p1, q1, p2, q2 = places_coords[a[0]], places_coords[a[1]], places_coords[b[0]], places_coords[b[1]]
 +
    o1 = orientation(p1, q1, p2)
 +
    o2 = orientation(p1, q1, q2)
 +
    o3 = orientation(p2, q2, p1)
 +
    o4 = orientation(p2, q2, q1)
  
        # don't cross
+
    # common case
         return False
+
    if o1 != o2 and o3 != o4:
 +
         return True
  
     def dfs(root):
+
     # don't cross
        current_tour = []
+
    return False
        u = root
+
        while True:
+
            current_tour.append(u)
+
            v = None
+
            for r in roads:
+
                if u not in r:
+
                    continue
+
                curr = list(set(r) - set([u]))[0]
+
  
                 if curr in current_tour:
+
def dfs(root):
                    continue
+
    current_tour = []
                crossing = False
+
    u = root
                for i in range(len(current_tour)-1):
+
    while True:
                    if cross((current_tour[i], current_tour[i+1]), r):
+
        current_tour.append(u)
 +
        v = None
 +
        for r in roads:
 +
            if u not in r:
 +
                 continue
 +
            curr = list(set(r) - set([u]))[0]
 +
 
 +
            if curr in current_tour:
 +
                continue
 +
            crossing = False
 +
            for i in range(len(current_tour)-1):
 +
                if cross((current_tour[i], current_tour[i+1]), r):
 
                     crossing = True
 
                     crossing = True
 
                     break
 
                     break
                if crossing:  
+
            if crossing:  
                    continue
+
                continue
                else:
+
            else:
                    v = curr
+
                v = curr
                    break
+
 
+
            if v is None:
+
 
                 break
 
                 break
            u = v
 
            roads.remove(r)
 
  
         return current_tour
+
         if v is None:
 +
            break
 +
        u = v
 +
        roads.remove(r)
  
     # read N and coords of places
+
     return current_tour
    N = int(input())
+
  
    for n in range(N):
+
# read N and coords of places
        x, y = [int(i) for i in input().split(' ')]
+
N = int(input())
        places_coords.append([n, x, y])
+
  
    # adding roads in format: [(i1, j1), ...]
+
for n in range(N):
    input_roads = []
+
    x, y = [int(i) for i in input().split(' ')]
    for r in range(N):
+
    places_coords.append([n, x, y])
        input_roads.append(input())
+
    for i in range(N):
+
        for j in range(i+1, N):
+
            if input_roads[i][j] == 'Y':
+
                roads.append((i, j))
+
  
    M = len(roads) # number of roads
+
# adding roads in format: [(i1, j1), ...]
 +
input_roads = []
 +
for r in range(N):
 +
    input_roads.append(input())
 +
for i in range(N):
 +
    for j in range(i+1, N):
 +
        if input_roads[i][j] == 'Y':
 +
            roads.append((i, j))
  
    tours = []
+
tours = []
    while roads:
+
while roads:
        # find the first place in roads
+
    # find the first place in roads
        tours.append(dfs(roads[0][0]))
+
    tours.append(dfs(roads[0][0]))
 +
 
 +
num_tours = len(tours)
 +
print(num_tours)
 +
for t in tours:
 +
    print (len(t)-1, ' '.join(map(str,t)))
  
    num_tours = len(tours)
 
    print(num_tours)
 
    for t in tours:
 
        print (len(t)-1, ' '.join(map(str,t)))
 
  
 
</source>
 
</source>
 +
 +
{{checkme|[[Участник:Ssyrovatkin|Ssyrovatkin]] 11:05, 26 сентября 2024 (UTC)}}

Текущая версия на 11:05, 26 сентября 2024

places_coords = []
roads = []
 
def orientation(p, q, r):
    val = (q[1] - p[1]) * (r[0] - q[0]) - (q[0] - p[0]) * (r[1] - q[1])
 
    return val > 0
 
def cross(a, b):
    """check if crosses p1q1 and p2q2"""
    p1, q1, p2, q2 = places_coords[a[0]], places_coords[a[1]], places_coords[b[0]], places_coords[b[1]]
    o1 = orientation(p1, q1, p2)
    o2 = orientation(p1, q1, q2)
    o3 = orientation(p2, q2, p1)
    o4 = orientation(p2, q2, q1)
 
    # common case
    if o1 != o2 and o3 != o4:
        return True
 
    # don't cross
    return False
 
def dfs(root):
    current_tour = []
    u = root
    while True:
        current_tour.append(u)
        v = None
        for r in roads:
            if u not in r:
                continue
            curr = list(set(r) - set([u]))[0]
 
            if curr in current_tour:
                continue
            crossing = False
            for i in range(len(current_tour)-1):
                if cross((current_tour[i], current_tour[i+1]), r):
                    crossing = True
                    break
            if crossing: 
                continue
            else:
                v = curr
                break
 
        if v is None:
            break
        u = v
        roads.remove(r)
 
    return current_tour
 
# read N and coords of places
N = int(input())
 
for n in range(N):
    x, y = [int(i) for i in input().split(' ')]
    places_coords.append([n, x, y])
 
# adding roads in format: [(i1, j1), ...]
input_roads = []
for r in range(N):
    input_roads.append(input())
for i in range(N):
    for j in range(i+1, N):
        if input_roads[i][j] == 'Y':
            roads.append((i, j))
 
tours = []
while roads:
    # find the first place in roads
    tours.append(dfs(roads[0][0]))
 
num_tours = len(tours)
print(num_tours)
for t in tours:
    print (len(t)-1, ' '.join(map(str,t)))

Check-me-animated.gif Решено: Ssyrovatkin 11:05, 26 сентября 2024 (UTC)