Submission #18500995


Source Code Expand

Copy
from copy import copy
#from time import time
from collections import deque
from heapq import heapify
from heapq import heappush
from heapq import heappop

#start=time()


H,W,K,sr,sc=map(int,input().split())
sr=sr-1
sc=sc-1
s=[]
for h in range(H):
    s.append(input())
N=int(input())
food=[[[0,0] for _ in range(W)] for _ in range(H)]
for i in range(N):
    fr,fc,F,D=map(int,input().split())
    food[fr-1][fc-1]=[F,D]

step=[[1,0],[-1,0],[0,1],[0,-1]]
dir=["D","U","R","L"]

def BFS(score,r,c,course,cnt,eat):

    seen=[[0]*W for _ in range(H)]

    q=deque([[score,r,c,course,cnt,eat]])
    CD=[]
    heapify(CD)
    seen[r][c]=1
    while q:
        sc,h0,w0,c,count,eat1=q.popleft()
        if count==K or sc<0:
            heappush(CD,[sc,h0,w0,c,count,eat1])
            if len(CD)>=2:
                sc,h0,w0,c,count,eat1=heappop(CD)
                return sc,h0,w0,c,count,eat1

        for i in range(4):
            h1=h0+step[i][0]
            w1=w0+step[i][1]
            if 0<=h1<H and 0<=w1<W and seen[h1][w1]==0 and s[h1][w1]==".":
                tmp_score = copy(sc)
                tmp_course = copy(c)
                tmp_eat=copy(eat1)
                if (food[h1][w1][0]-food[h1][w1][1]*count)>0:
                    tmp_eat.append([h1,w1])
                    tmp_score-=food[h1][w1][0]-food[h1][w1][1]*count
                elif (food[h1][w1][0]-food[h1][w1][1]*count)<0:
                    continue
                tmp_course+=dir[i]
                q.append([tmp_score,h1,w1,tmp_course,count+1,tmp_eat])
                seen[h1][w1]=1
    if len(CD)>0:
        sc,h0,w0,c,count,eat1=heappop(CD)

    return sc,h0,w0,c,count,eat1


anss=[]
#while time()-start<9.5:
for n in range(1):
    ans=[]
    score=0
    h0=sr
    w0=sc
    hp=h0
    wp=w0
    course=""
    cnt=0

    while True:
        score1,h1,w1,course,cnt,eat=BFS(0,h0,w0,"",cnt,[])
        if len(course)==0:
            for i in range(K-len(ans)):
                ans+="-"
        if cnt==K:
            ans.append(course)
            score-=score1
            break
        score+=score1
        ans.append(course)
        for fr,fc in eat:
            food[fr][fc]=[0,0]
        h0=h1
        w0=w1

    anss.append([score,ans])

Ans=anss[anss.index(min(anss))][1]
print("".join(Ans))






Submission Info

Submission Time
Task B - Food Collector
User SleepyPuppy
Language PyPy3 (7.3.0)
Score 17666
Code Size 2388 Byte
Status AC
Exec Time 166 ms
Memory 71620 KB

Judge Result

Set Name test_01 test_02 test_03 test_04 test_05 test_06 test_07 test_08 test_09 test_10
Score / Max Score 1701 / 20000 2268 / 20000 1764 / 20000 1081 / 20000 1941 / 20000 2143 / 20000 2018 / 20000 1581 / 20000 1162 / 20000 2007 / 20000
Status
AC × 1
AC × 1
AC × 1
AC × 1
AC × 1
AC × 1
AC × 1
AC × 1
AC × 1
AC × 1
Set Name Test Cases
test_01 subtask_01_01.txt
test_02 subtask_01_02.txt
test_03 subtask_01_03.txt
test_04 subtask_01_04.txt
test_05 subtask_01_05.txt
test_06 subtask_01_06.txt
test_07 subtask_01_07.txt
test_08 subtask_01_08.txt
test_09 subtask_01_09.txt
test_10 subtask_01_10.txt
Case Name Status Exec Time Memory
subtask_01_01.txt AC 166 ms 70712 KB
subtask_01_02.txt AC 152 ms 71200 KB
subtask_01_03.txt AC 128 ms 70476 KB
subtask_01_04.txt AC 129 ms 71620 KB
subtask_01_05.txt AC 140 ms 70696 KB
subtask_01_06.txt AC 146 ms 70452 KB
subtask_01_07.txt AC 142 ms 70372 KB
subtask_01_08.txt AC 129 ms 70204 KB
subtask_01_09.txt AC 124 ms 69884 KB
subtask_01_10.txt AC 129 ms 70040 KB