From e96cd29a4f7a2a26119b99d193a6113e7cdb1920 Mon Sep 17 00:00:00 2001 From: unknown Date: Fri, 17 Oct 2025 13:29:44 -0500 Subject: [PATCH] Graph-1 completed --- maze.py | 45 +++++++++++++++++++++++++++++++++++++++++++++ town_judge.py | 12 ++++++++++++ 2 files changed, 57 insertions(+) create mode 100644 maze.py create mode 100644 town_judge.py diff --git a/maze.py b/maze.py new file mode 100644 index 0000000..1182360 --- /dev/null +++ b/maze.py @@ -0,0 +1,45 @@ +import collections +class Solution: + def hasPath(self, maze, start, destination): + self.dirs=[[0,-1],[0,1],[-1,0],[1,0]] + m=len(maze) + n=len(maze[0]) + + q=collections.deque() + q.append([start[0],start[1]]) + maze[start[0]][start[1]]=-1 + + while q: + curr=q.popleft() + for dir in self.dirs: + r=curr[0]+dir[0] + c=curr[1]+dir[1] + + while r>=0 and c>=0 and r<=m-1 and c<=n-1 and maze[r][c]!=1: + r+=dir[0] + c+=dir[1] + r-=dir[0] + c-=dir[1] + + if r==destination[0] and c==destination[1]: + return True + + if maze[r][c]!=-1: + q.append([r,c]) + maze[r][c]=-1 + return False + + +maze = [ + [0,0,1,0,0], + [0,0,0,0,0], + [0,0,0,1,0], + [1,1,0,1,1], + [0,0,0,0,0] +] +start = [0,4] +destination = [3,2] + +s = Solution() +result = s.hasPath(maze, start, destination) +print(result) diff --git a/town_judge.py b/town_judge.py new file mode 100644 index 0000000..ed53ef4 --- /dev/null +++ b/town_judge.py @@ -0,0 +1,12 @@ +class Solution: + def findJudge(self, n: int, trust: List[List[int]]) -> int: + array=[0]*(n+1) + for t in trust: + array[t[0]]-=1 + array[t[1]]+=1 + + for i in range(1,n+1): + if array[i]==n-1: + return i + return -1 +