Skip to content

Instantly share code, notes, and snippets.

@Tsutomu-KKE
Created March 8, 2015 04:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Tsutomu-KKE/33d43ab18ddc57d364bb to your computer and use it in GitHub Desktop.
Save Tsutomu-KKE/33d43ab18ddc57d364bb to your computer and use it in GitHub Desktop.
Puzzele(Oh No)
{
"metadata": {
"name": "",
"signature": "sha256:8203856d10fa3cf6ef9ce79f3b3dcf65cbedfa97a36a3ae33123f2c1ba590db9"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"##0h n0\n",
"http://0hn0.com/"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"from pulp import *\n",
"def chk(ary):\n",
" nh, nw = len(ary), len(ary[0])\n",
" mx, rw, rh, r4 = max(nw, nh), range(nw), range(nh), range(4)\n",
" m = LpProblem()\n",
" vb = [[LpVariable('b%d_%d' % (i, j), cat=LpBinary) for j in rw] for i in rh] # 0: red, 1: blue\n",
" # 0: left, 1: up, 2: right, 3:down\n",
" vd = [[[LpVariable('d%d_%d_%d' % (i, j, k), lowBound=0) for k in r4] for j in rw] for i in rh]\n",
" m += lpSum(vb[i][j] for i in rh for j in rw)\n",
" for i in rh:\n",
" for j in rw:\n",
" for k in r4:\n",
" m += vd[i][j][k] <= mx * vb[i][j]\n",
" ik, jk = i + [-1, 0, 1, 0][k], j + [0, -1, 0, 1][k]\n",
" if 0 <= ik < nh and 0 <= jk < nw:\n",
" m += vd[i][j][k] >= vd[ik][jk][k] + 1 - mx * (1 - vb[i][j])\n",
" m += vd[i][j][k] <= vd[ik][jk][k] + 1 + mx * (1 - vb[i][j])\n",
" else:\n",
" m += vd[i][j][k] == vb[i][j]\n",
" if ary[i][j] < 0:\n",
" m += vb[i][j] == 0\n",
" elif ary[i][j]:\n",
" m += vb[i][j] == 1\n",
" m += lpSum(vd[i][j]) == ary[i][j] + 4\n",
" m.solve()\n",
" if m.status != 1:\n",
" return None\n",
" res = [[int(value(vb[i][j])) for j in rw] for i in rh]\n",
" ll = [vb[i][j] for i in rh for j in rw if value(vb[i][j]) > 0.5]\n",
" m += lpSum(ll) <= len(ll) - 1\n",
" m.solve()\n",
" return res if m.status != 1 else None"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 1
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"chk([\n",
"[0,0,-1,3,0],\n",
"[0,0,0,0,0],\n",
"[0,0,0,0,5],\n",
"[3,4,0,0,-1],\n",
"[0,0,4,-1,0],\n",
"])"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 3,
"text": [
"[[0, 0, 0, 1, 1],\n",
" [0, 0, 0, 1, 1],\n",
" [0, 1, 1, 1, 1],\n",
" [1, 1, 1, 0, 0],\n",
" [1, 1, 1, 0, 0]]"
]
}
],
"prompt_number": 3
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"chk([\n",
"[-1,0,0,3,0],\n",
"[0,2,0,0,-1],\n",
"[4,4,0,0,0],\n",
"[5,0,5,0,0],\n",
"[0,0,0,3,0],\n",
"])"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 5,
"text": [
"[[0, 0, 1, 1, 1],\n",
" [0, 1, 0, 1, 0],\n",
" [1, 1, 1, 0, 0],\n",
" [1, 1, 1, 1, 0],\n",
" [1, 0, 1, 1, 1]]"
]
}
],
"prompt_number": 5
},
{
"cell_type": "code",
"collapsed": false,
"input": [],
"language": "python",
"metadata": {},
"outputs": []
}
],
"metadata": {}
}
]
}
@Tsutomu-KKE
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment