diff --git a/Problems/Graph/Day-12/sol/ishanrajsingh/Solution1.py b/Problems/Graph/Day-12/sol/ishanrajsingh/Solution1.py new file mode 100644 index 0000000..964b82d --- /dev/null +++ b/Problems/Graph/Day-12/sol/ishanrajsingh/Solution1.py @@ -0,0 +1,44 @@ +# Submission Link: https://codeforces.com/contest/769/submission/357188627 +# Approach: use BFS and precompute dist from start and greedy build lexicographically smallest valid cycle by choosing moves in order L,R,U,D +# Runtime: O(n*m + k) +import sys +from collections import deque +input=sys.stdin.readline +n,m,k=map(int,input().split()) +maze=[list(input().strip()) for _ in range(n)] +dirs=[('D',1,0),('L',0,-1),('R',0,1),('U',-1,0)] +sx=sy=0 +for i in range(n): + for j in range(m): + if maze[i][j]=='X': + sx,sy=i,j +dist=[[10**18]*m for _ in range(n)] +dist[sx][sy]=0 +dq=deque([(sx,sy)]) +while dq: + x,y=dq.popleft() + for _,dx,dy in dirs: + nx,ny=x+dx,y+dy + if 0<=nxdist[x][y]+1: + dist[nx][ny]=dist[x][y]+1 + dq.append((nx,ny)) +if k%2==1: + print("IMPOSSIBLE") + sys.exit() +res=[] +cx,cy=sx,sy +for _ in range(k): + moved=False + for ch,dx,dy in [('D',1,0),('L',0,-1),('R',0,1),('U',-1,0)]: + nx,ny=cx+dx,cy+dy + if 0<=nx