Advertisement
Guest User

Untitled

a guest
Dec 16th, 2024
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.40 KB | Source Code | 0 0
  1. from itertools import product
  2. import networkx as nx        
  3.        
  4.  
  5. def main():
  6.     with open('d1.txt') as f:
  7.         grid=[list(i) for i in f.read().split('\n')]
  8.    
  9.     height=len(grid)
  10.     width=len(grid[0])
  11.    
  12.     for i,j in product(range(height),range(width)):
  13.         if grid[i][j]=='S':
  14.             start=(i,j)
  15.         if grid[i][j]=='E':
  16.             end=(i,j)
  17.    
  18.     G=nx.Graph()
  19.     G.add_edge((start,(0,1)),(start,(-1,0)),weight=1000)
  20.     for i,j in product(range(height),range(width)):
  21.         if grid[i][j] in 'S#':
  22.             continue
  23.         if (i,j)==end:
  24.             G.add_edge(((i+1,j),(-1,0)),end,weight=1)
  25.             G.add_edge(((i,j-1),(0,1)),end,weight=1)
  26.         for d in headings:
  27.             x,y=move((i,j),d)
  28.            
  29.             if grid[x][y] not in 'E#':            
  30.                 for d1 in headings:
  31.                     dr=(-d[0],-d[1])
  32.                     if d==d1:
  33.                         continue
  34.                     if dr==d1:
  35.                         G.add_edge(((i,j),dr),((x,y),d1),weight=1)
  36.                     else:
  37.                         G.add_edge(((i,j),dr),((x,y),d1),weight=1001)
  38.  
  39.     print(nx.dijkstra_path_length(G,(start,(0,1)),end))
  40.    
  41.     seats=set()  
  42.     for l in nx.all_shortest_paths(G,(start,(0,1)),end,weight='weight',method='dijkstra'):
  43.         s=map(lambda x:x[0],l)
  44.         seats.update(s)
  45.     print(len(seats))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement