---
# System prepended metadata

title: Problem03-2(Mine Field)
tags: [week8, answer]

---

# Problem03-2(Mine Field)
###### tags: `week8` `answer`
```python=1
alist =[]
import sys
for line in sys.stdin:
    alist.append(list(map(int,line.split())))
m =len(alist)
n=len(alist[0])
anslist = [[0 for _ in range(n)] for _ in range(m)]

def check(i,j):
    global m,n
    if 0<= i<m and 0<=j<n:
        return True
    return False

def surround(i,j):
    return [(i-1,j-1),(i,j-1),(i+1,j-1),(i-1,j),(i,j),(i+1,j),(i-1,j+1),(i,j+1),(i+1,j+1)]

def change(i,j,d):
    global alist
    for k in surround(i,j):
        if check(*k):
            alist[k[0]][k[1]] += d

def negative(i,j,alist):
    k = surround(i,j)
    for w in k:
        if check(*w):
            if alist[w[0]][w[1]] <0:
                return True
    return False

def turn(alist,x,y):
    global m,n
    li = [0]
    if x == m-1:
        if y>1:
            li.append(alist[x][y-2])
    if x>=1:
        if y>=2:
            li.append(alist[x-1][y-2])
        if x>=2:
            if y == 0:
                li.extend(alist[x-2][n-2:n])
    return set(li)

def p(alist):
    for k in alist:
        print(k)
    print('')

def go(start,anslist):
    global m,n,alist
    x = start//n
    y = start%m
    if turn(alist,x,y) != {0}:
        return
    if negative(x,y,alist):
        return
    if start ==m*n+1:
        p(anslist)
        exit()
    go(start+1,anslist)
    change(x,y,-1)
    anslist[x][y]=1
    go(start+1,anslist)
    change(x,y,1)
    anslist[x][y]=0
    if start == 0:
        print(-1)
        quit()
    return
    
go(0,anslist)
```
