fling

23 篇文章 0 订阅
2 篇文章 0 订阅
喺N9上面有一隻遊戲叫做fling...
就係拉啲波波撞來撞去.. 撞走剩低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()


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值