git reimport

This commit is contained in:
2019-03-15 13:53:39 +04:00
commit 226324cf52
3 changed files with 221 additions and 0 deletions

12
Pipfile Normal file
View File

@@ -0,0 +1,12 @@
[[source]]
url = "https://pypi.org/simple"
verify_ssl = true
name = "pypi"
[packages]
numpy = "*"
[dev-packages]
[requires]
python_version = "3.7"

55
Pipfile.lock generated Normal file
View File

@@ -0,0 +1,55 @@
{
"_meta": {
"hash": {
"sha256": "bc7e66f6a26742e21d302e533ffeb1e4665a7a5e43d6d51b4f3be34fab2cfc5e"
},
"pipfile-spec": 6,
"requires": {
"python_version": "3.7"
},
"sources": [
{
"name": "pypi",
"url": "https://pypi.org/simple",
"verify_ssl": true
}
]
},
"default": {
"numpy": {
"hashes": [
"sha256:0df89ca13c25eaa1621a3f09af4c8ba20da849692dcae184cb55e80952c453fb",
"sha256:154c35f195fd3e1fad2569930ca51907057ae35e03938f89a8aedae91dd1b7c7",
"sha256:18e84323cdb8de3325e741a7a8dd4a82db74fde363dce32b625324c7b32aa6d7",
"sha256:1e8956c37fc138d65ded2d96ab3949bd49038cc6e8a4494b1515b0ba88c91565",
"sha256:23557bdbca3ccbde3abaa12a6e82299bc92d2b9139011f8c16ca1bb8c75d1e95",
"sha256:24fd645a5e5d224aa6e39d93e4a722fafa9160154f296fd5ef9580191c755053",
"sha256:36e36b6868e4440760d4b9b44587ea1dc1f06532858d10abba98e851e154ca70",
"sha256:3d734559db35aa3697dadcea492a423118c5c55d176da2f3be9c98d4803fc2a7",
"sha256:416a2070acf3a2b5d586f9a6507bb97e33574df5bd7508ea970bbf4fc563fa52",
"sha256:4a22dc3f5221a644dfe4a63bf990052cc674ef12a157b1056969079985c92816",
"sha256:4d8d3e5aa6087490912c14a3c10fbdd380b40b421c13920ff468163bc50e016f",
"sha256:4f41fd159fba1245e1958a99d349df49c616b133636e0cf668f169bce2aeac2d",
"sha256:561ef098c50f91fbac2cc9305b68c915e9eb915a74d9038ecf8af274d748f76f",
"sha256:56994e14b386b5c0a9b875a76d22d707b315fa037affc7819cda08b6d0489756",
"sha256:73a1f2a529604c50c262179fcca59c87a05ff4614fe8a15c186934d84d09d9a5",
"sha256:7da99445fd890206bfcc7419f79871ba8e73d9d9e6b82fe09980bc5bb4efc35f",
"sha256:99d59e0bcadac4aa3280616591fb7bcd560e2218f5e31d5223a2e12a1425d495",
"sha256:a4cc09489843c70b22e8373ca3dfa52b3fab778b57cf81462f1203b0852e95e3",
"sha256:a61dc29cfca9831a03442a21d4b5fd77e3067beca4b5f81f1a89a04a71cf93fa",
"sha256:b1853df739b32fa913cc59ad9137caa9cc3d97ff871e2bbd89c2a2a1d4a69451",
"sha256:b1f44c335532c0581b77491b7715a871d0dd72e97487ac0f57337ccf3ab3469b",
"sha256:b261e0cb0d6faa8fd6863af26d30351fd2ffdb15b82e51e81e96b9e9e2e7ba16",
"sha256:c857ae5dba375ea26a6228f98c195fec0898a0fd91bcf0e8a0cae6d9faf3eca7",
"sha256:cf5bb4a7d53a71bb6a0144d31df784a973b36d8687d615ef6a7e9b1809917a9b",
"sha256:db9814ff0457b46f2e1d494c1efa4111ca089e08c8b983635ebffb9c1573361f",
"sha256:df04f4bad8a359daa2ff74f8108ea051670cafbca533bb2636c58b16e962989e",
"sha256:ecf81720934a0e18526177e645cbd6a8a21bb0ddc887ff9738de07a1df5c6b61",
"sha256:edfa6fba9157e0e3be0f40168eb142511012683ac3dc82420bee4a3f3981b30e"
],
"index": "pypi",
"version": "==1.15.4"
}
},
"develop": {}
}

154
src/solver.py Normal file
View File

@@ -0,0 +1,154 @@
import collections
from enum import Enum
import numpy as np
class Car(Enum):
TwoHorizontal = 1
TwoVertical = 2
ThreeHorizontal = 3
ThreeVertical = 4
start_state = np.array([
[1, 0, 1, 0, 2, 0],
[0, 0, 2, 0, 0, 4],
[0, 2, 0, 1, 0, 0],
[4, 0, 0, 1, 0, 0],
[0, 0, 0, 2, 1, 0],
[0, 1, 0, 0, 1, 0]
], dtype=int)
car_lengths = [
2, 2, 3, 3
]
car_directions = np.array([
[
[0, -1],
[0, 1],
],
[
[-1, 0],
[1, 0],
],
[
[0, -1],
[0, 1],
],
[
[-1, 0],
[1, 0],
],
], dtype=int)
def get_neighbours(state):
n, m = state.shape
taken = np.zeros((n, m), dtype=bool)
for i in range(n):
for j in range(m):
if state[i, j] == 0:
continue
c = state[i, j] - 1
for k in range(car_lengths[c]):
taken[
i + car_directions[c, 1, 0] * k,
j + car_directions[c, 1, 1] * k,
] = True
for i in range(n):
for j in range(m):
if state[i, j] == 0:
continue
c = state[i, j] - 1
for k in range(car_lengths[c]):
taken[
i + car_directions[c, 1, 0] * k,
j + car_directions[c, 1, 1] * k,
] = False
for direction in range(2):
for move_distance in range(1, 10):
ok = True
ii = i + car_directions[c, direction, 0] * move_distance
jj = j + car_directions[c, direction, 1] * move_distance
for k in range(car_lengths[c]):
iii = ii + car_directions[c, 1, 0] * k
jjj = jj + car_directions[c, 1, 1] * k
if iii < 0 or iii >= n or jjj < 0 or jjj >= m:
ok = False
break
if taken[
iii,
jjj,
]:
ok = False
break
if not ok:
break
new_state = np.array(state)
new_state[ii, jj] = state[i, j]
new_state[i, j] = 0
yield new_state
for k in range(car_lengths[c]):
taken[
i + car_directions[c, 1, 0] * k,
j + car_directions[c, 1, 1] * k,
] = True
def find_exit():
queue = collections.deque()
visited = set()
queue.append((0, start_state))
visited.add(hash(start_state.tobytes()))
history = dict()
while len(queue) > 0:
distance, state = queue.popleft()
if state[2, 4] == 1:
print(len(visited))
return state, history, distance
for new_state in get_neighbours(state):
new_distance = distance + 1
hsh = hash(new_state.tobytes())
if hsh in visited:
continue
visited.add(hsh)
history[hsh] = state
queue.append((new_distance, new_state))
if __name__ == '__main__':
state, history, distance = find_exit()
states = [state]
while hash(state.tobytes()) in history:
prev_state = history[hash(state.tobytes())]
states.append(prev_state)
state = prev_state
states = states[::-1]
print(states)