喺N9上面有一隻遊戲叫做fling...
就係拉啲波波撞來撞去.. 撞走剩低1個波就赢. 相鄰波波唔可以互相撞走
就係拉啲波波撞來撞去.. 撞走剩低1個波就赢. 相鄰波波唔可以互相撞走
果斷 dfs 夾硬解題.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# 8 rows 7 cols
from copy import deepcopy as dcopy
base_board = [ [ False for i in range(7) ] for j in range(8)]
directs = [ (-1,0), (1,0), (0,-1), (0,1) ]
pr_dirt = [ u"上", u"下", u"左", u"右" ]
###########
def can_fling( ball, board, direct ):
x, y = ball
last_blank = True
x += direct[0]
y += direct[1]
while x in range(8) and y in range(7):
if last_blank == False and board[x][y]:
return True
last_blank = board[x][y]
x += direct[0]
y += direct[1]
return False
def mkb( board, ball, direct ):
x, y = ball
x += direct[0]
y += direct[1]
while x in range(8) and y in range(7):
if board[ x - direct[0] ][ y - direct[1] ] and not board[x][y]:
board[ x - direct[0] ][ y - direct[1] ] = False
board[ x ][ y ] = True
x += direct[0]
y += direct[1]
board[ x - direct[0] ][ y - direct[1] ] = False
return board
def dfs( steps, board ):
balls = []
for i in range(8):
for j in range(7):
if board[i][j]:
balls.append( (i,j) )
# print balls
# for i in board:
# for j in i:
# print j == True and 1 or 0,
# print ""
if len( balls ) == 1 :
return True, steps
candosteps = []
for i in balls:
for direct in directs:
if can_fling( i, board, direct ):
# print "can_fling: ",i, direct
candosteps.append( (i, direct) )
for i in candosteps:
done, done_steps = dfs( steps + [ i ], mkb( dcopy(board), i[0], i[1]) )
if done:
return True, done_steps
return False, []
# end of dfs
def main():
while 1:
x,y = raw_input().split()
x,y = int(x)-1, int(y)-1
if x == -1 or y == -1: break
base_board[x][y] = True
done, ans = dfs( [], dcopy(base_board) )
print done
for i in ans:
print "(",i[0][0]+1,i[0][1]+1,") : ",pr_dirt[ directs.index( i[1] ) ]
if __name__ == "__main__":
main()