Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.8k views
in Technique[技术] by (71.8m points)

2020 CCC J5/Q2 Escape Room (Python) BFS Optimization

Is there a way to optimize my code even further? I am trying to solve the 2020 CCC J5/Q2 question and I have passed all the test cases except for a few, which I am exceeding the time limit.

Link to the question (It is the last question) https://cemc.uwaterloo.ca/contests/computing/2020/ccc/juniorEF.pdf

#definitions
row = int(input())
column = int(input())
array = [list(map(int, input().split())) for x in range(row)]
area = row*column

#Setting the queue to only have -1
pathways = [-1]*(area+1)
emptyS = 0
currentPath = 0
inList = [False] * 1000001

#Setting the first number in the queue to equal the number at [0][0]
pathways[emptyS] = array[0][0]
emptyS += 1`enter code here`
#Checking if the number at [0][0] is the answer (the answer is the area of the array)
if pathways[currentPath] == area:
    print('yes')
    raise SystemExit(0)

#Using BFS to find the answer
#Looping until the queue hits -1
while pathways[currentPath] != -1:
    for x in range(1, row+1):
        if pathways[currentPath] % x == 0:
            for y in range(1, column+1):

#I wasn't sure how to add 1 to every index of my 2d array, so I added 1 in the for loop, and arrayMN is the actual index in my loop.
                arrayMN = array[x-1][y-1]
                if pathways[currentPath] == x*y:

                    #Checking if the value is equal to the answer
                    if arrayMN == area:
                        print("yes")
                        raise SystemExit(0)

                    #If it isn't equal, either replace the -1 in pathways with the number (add it to the queue) or don't add it to prevent a loop)
                    if inList[arrayMN] == False:
                        pathways[emptyS] = arrayMN
                        emptyS += 1
                        inList[arrayMN] = True
    currentPath += 1

#If none of the above works, it is not possible
print('no')

This is my first forum post, so if there are any things I can do to improve my posts, please let me know.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神解答

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...