I'm trying to find all possible paths made of 0 from bottom left to top right, only by moving either up or to the right. The input file contains a grid of characters, where 1
represents an obstacle and 0
represents a vacant cell.
The problem I have is the code not finishing/ taking too long. The code works correctly with smaller inputs like this, but on a large file with 5000 rows and 5000 characters in each row it never finishes even after 20 minutes.
import sys sys.setrecursionlimit(900000000) file_e = open('file.txt','r') file = file_e.readlines() file_e.close() file.reverse() for index,line in enumerate(file): file[index] = line.split(' ') print(len(file)-1, len(file[0])-1) def end(file,y,x): if y == len(file)-1 and x == len(file[0])-1: return 1 try: if file[y+1][x] == '0': up = True except: pass try: if file[y][x+1] == '0': right = True except: pass try: if up == True and right == True: return end(file,y+1,x) + end(file,y,x+1) except: pass try: if up == True: return end(file,y+1,x) except: pass try: if right == True: return end(file,y,x+1) except: pass if file[y+1][x] == '1' or file[y][x+1] == '1': return 0 print(end(file,0,0))