fling

Python3.10

Python3.10

Conda
Python

Python 是一种高级、解释型、通用的编程语言,以其简洁易读的语法而闻名,适用于广泛的应用,包括Web开发、数据分析、人工智能和自动化脚本

喺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()


您可能感兴趣的与本文相关的镜像

Python3.10

Python3.10

Conda
Python

Python 是一种高级、解释型、通用的编程语言,以其简洁易读的语法而闻名,适用于广泛的应用,包括Web开发、数据分析、人工智能和自动化脚本

在 Android 开发中,`Scroller` 类是一个用于处理滚动行为的实用工具类,常用于实现平滑滚动效果。`Scroller.fling()` 方法用于模拟一个快速滑动(fling)动作,并计算出在该动作下视图的滚动轨迹。 ### `Scroller.fling()` 方法的基本用法 `fling()` 方法的完整签名如下: ```java public void fling(int startX, int startY, int velocityX, int velocityY, int minX, int maxX, int minY, int maxY) ``` - `startX`, `startY`:滚动的起始坐标。 - `velocityX`, `velocityY`:水平和垂直方向的速度,单位为像素/秒。 - `minX`, `maxX`:水平方向滚动的最小值和最大值。 - `minY`, `maxY`:垂直方向滚动的最小值和最大值。 调用该方法后,`Scroller` 会根据速度和边界条件计算出一系列滚动偏移量,开发者需要在 `computeScroll()` 方法中不断获取当前滚动位置并更新视图。 ### 示例代码 以下是一个典型的使用 `Scroller.fling()` 实现滚动的示例,适用于自定义 `View` 或 `ViewGroup`: ```java public class FlingScrollView extends View { private Scroller scroller; public FlingScrollView(Context context) { super(context); scroller = new Scroller(context); } public void startFling(int startX, int startY, int velocityX, int velocityY) { // 设置 fling 的边界范围 scroller.fling(startX, startY, velocityX, velocityY, 0, 1000, 0, 1000); postInvalidate(); } @Override public void computeScroll() { if (scroller.computeScrollOffset()) { int currX = scroller.getCurrX(); int currY = scroller.getCurrY(); scrollTo(currX, currY); postInvalidate(); } } } ``` ### 实际应用场景 `Scroller.fling()` 常用于实现以下功能: - 自定义 `ScrollView` 或 `HorizontalScrollView`。 - 实现类似 `RecyclerView` 或 `ViewPager` 的滚动和滑动行为。 - 在 `GestureDetector` 检测到 `onFling` 事件后触发滚动动画。 ### 注意事项 - `fling()` 不会自动更新 UI,需要结合 `computeScrollOffset()` 和 `computeScroll()` 来持续更新滚动位置。 - 滚动的边界值应根据实际内容的尺寸进行设置。 - 如果需要更复杂的滚动动画,可以结合 `OverScroller` 或 `ViewDragHelper`。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值