#Keeper, Python 2
import sys
Out = ["S", "P", "S"]
StX = int(sys.stdin.readline()) - 1
StY = int(sys.stdin.readline()) - 1
NowX = int(sys.stdin.readline()) - 1
NowY = int(sys.stdin.readline()) - 1
Map = []
for i in range(32):
Map.append(sys.stdin.readline())
Pos = [NowX, NowY]
Area = [[StX, StY]]
for x in range(StX-4, StX+4):
for y in range(StY-4, StY+4):
Area.append([x, y])
Fires = []
for y in range(32):
for x in range(32):
if Map[y][x] in "123456":
Fires.append([x, y])
Danger = []
for Tile in Area:
if Map[Tile[1]][Tile[0]] in "123456":
Danger.append(Tile)
Distance = {}
i = -1
for Fire in Danger:
i += 1
Distance[(Pos[0] - Fire[0], Pos[1] - Fire[1])] = i
i = min(Distance)
Closest = Danger[Distance[i]]
if Closest[0] > Pos[0]:
Out[0] = Out[2] = "R"
Pos[0] += 1
if Closest[0] < Pos[0]:
Out[0] = Out[2] = "L"
Pos[0] -= 1
if Closest[0] == Pos[0]:
if Closest[1] > Pos[1]:
Out[0] = Out[2] = "D"
Pos[1] += 1
if Closest[1] < Pos[1]:
Out[0] = Out[2] = "U"
Pos[1] -= 1
if Closest[0] + 1 == Pos[0] and Closest[1] == Pos[1]:
Out[2] = "L"
if Closest[0] - 1 == Pos[0] and Closest[1] == Pos[1]:
Out[2] = "R"
if Closest[1] + 1 == Pos[1] and Closest[0] == Pos[0]:
Out[2] = "U"
if Closest[1] - 1 == Pos[1] and Closest[0] == Pos[0]:
Out[2] = "D"
if Closest[0] == Pos[0] and Closest[1] == Pos[1]:
Out[2] = "S"
print "".join(Out)
Could be simplified, but I'm tired.
The Keeper tries to keep its field from harm. If a fire appears, it hurries over to it and puts it out as fast as it can.
I may add accommodation for incoming fires as well.