Problem solution · Python

CCC 2018 S3 - RoboThieves

CCC 2018 S3 - RoboThieves: a Python solution using breadth-first search. Learn the idea, check the complexity, and read the full code, with credit to CCCSolutions.

Technique
Breadth-first search
Source
CCCSolutions
Length
132 lines
Start with the idea.

Try the problem first. If you get stuck, read the approach below, then write your own solution. The full code is at the bottom.

Approach

Breadth-first search

For CCC 2018 S3 - RoboThieves, the implementation explores reachable states in layers, which is the standard shape for unweighted shortest paths and minimum-step transitions.

  1. Model each valid configuration as a state and each legal move as an edge.
  2. Seed the queue with the starting state and mark it immediately.
  3. Expand each state once, recording distance or reachability for unseen neighbours.

Code notes

  • 132 lines of Python from the credited upstream file ccc18s3.py.
  • The implementation visibly relies on sequence storage, ordered lookup, work queue.
  • No explicit loop blocks detected.

Complexity

Verify that each state and transition is processed only a bounded number of times; that determines the traversal cost.

Check the problem constraints before deciding whether this complexity will pass.

Source

Code and credit

This code comes from CCCSolutions by CCCSolutions contributors · Milliken Mills High School and is used under the MIT licence.

Full codeCCC 2018 S3 - RoboThieves · PythonPython
Use this to learn the idea, then write your own version.
# By Oscar Zhou, Abbey Park High School# This solution still requires commented explanations. Please feel free to add them! # https://dmoj.ca/problem/ccc18s3 import sysfrom collections import dequeinput = sys.stdin.readline def output(graph):    for row in graph:        print(*row) def good(R, C):    return 0 <= R < r and 0 <= C < c def isConve(R, C):    return graph[R][C] in {"L", "R", "U", "D"} def canGo(R, C):    return isConve(R, C) or graph[R][C] == "." def cv(R, C):    dirn = {"L": [0, -1], "R": [0, 1], "U": [-1, 0], "D": [1, 0]}    return dirn[graph[R][C]] def conveyor(start):    cur = start    curR = start[0]    curC = start[1]    d = cv(curR, curC)    while True:        nxtR, nxtC = curR + d[0], curC + d[1]        if good(nxtR, nxtC) and isConve(nxtR, nxtC) and not vis[nxtR][nxtC]:            vis[nxtR][nxtC] = True            dis[nxtR][nxtC] = dis[curR][curC]            curR, curC = nxtR, nxtC            d = cv(curR, curC)        else:            break    return [nxtR, nxtC] # Taking in inputr, c = map(int, input().split())graph = [list(input().strip()) for i in range(r)]start = [] # Finding start# Start is converted to "."flg = Falsefor i in range(r):    for j in range(c):        if graph[i][j] == "S":            start = [i, j]            graph[i][j] = "."            flg = True            break    if flg:        break # Converting blocked spaces to be "F"for i in range(r):    for j in range(c):        if graph[i][j] == "C":            #left            for s in range(j-1, -1, -1):                if graph[i][s] == "W":                    break                if graph[i][s] == ".":                    graph[i][s] = "F"            #right            for s in range(j+1, c):                if graph[i][s] == "W":                    break                if graph[i][s] == ".":                    graph[i][s] = "F"            # up            for s in range(i - 1, -1, -1):                if graph[s][j] == "W":                    break                if graph[s][j] == ".":                    graph[s][j] = "F"            # right            for s in range(i + 1, r):                if graph[s][j] == "W":                    break                if graph[s][j] == ".":                    graph[s][j] = "F" # If start is blocked then epic-failif graph[start[0]][start[1]] == "F":    cnt = -1    for i in range(r):        for j in range(c):            if graph[i][j] == "." or graph[i][j] == "F":                cnt += 1    for i in range(cnt):        print(-1)    sys.exit(0) # BFSdis = [[float("inf")] * c for i in range(r)]vis = [[False] * c for i in range(r)]q = deque([start])vis[start[0]][start[1]] = Truedis[start[0]][start[1]] = 0while q:    cur = q.popleft()    curR = cur[0]    curC = cur[1]    if graph[curR][curC] == "." or graph[curR][curC] == "S":        for d in [[0, 1], [0, -1], [1, 0], [-1, 0]]:            nxtR = curR + d[0]            nxtC = curC + d[1]            if good(nxtR, nxtC) and  not vis[nxtR][nxtC]:                if graph[nxtR][nxtC] == ".":                    vis[nxtR][nxtC] = True                    dis[nxtR][nxtC] = dis[curR][curC] + 1                    q.append([nxtR, nxtC])                elif isConve(nxtR, nxtC):                    vis[nxtR][nxtC] = True                    dis[nxtR][nxtC] = dis[curR][curC] + 1                    offR, offC = conveyor([nxtR, nxtC])                    if good(offR, offC) and not vis[offR][offC] and canGo(offR, offC):                        vis[offR][offC] = True                        dis[offR][offC] = dis[nxtR][nxtC]                        q.append([offR, offC]) for i in range(r):    for j in range(c):        if (graph[i][j] == "." or graph[i][j] == "F") and start != [i, j]:            print(dis[i][j] if vis[i][j] else -1)

Did this explanation save you time? I'm a Grade 11 student building this free library to make difficult algorithms easier to understand.

Buy me a coffee ↗