SkipStones

/*Problem Statement

When a stone is thrown across water, sometimes it will land on the water and bounce rather than falling in right away.

Suppose that a stone is thrown a distance of n. On each successive bounce it will travel half the distance as the previous bounce (rounded down to the nearest integer).

When it can not travel any further, it falls into the water.

If, at any point, the stone lands on an obstruction rather than water, it will not bounce, but will simply deflect and fall into the water.

Please look at the figure for further clarification (with black, red and green cells representing banks, obstructions and free water respectively).

So, if the stone is thrown a distance 7, it will bounce and travel a distance of 3, then finally a distance of 1,

having travelled a total distance of 11 (the green path in the figure).

If a stone is thrown a distance of 8, it will reach the opposite bank, and if thrown at distances of 2 or 6 it will hit an obstruction during its travel.

These are the three red paths in the figure.

You are given a String water. An ‘X’ represents an obstruction, while a ‘.’represents water free from obstruction.

You are to return an int representing the maximum distance a stone can travel and finally fall in the water, without hitting any obstructions,

and without reaching the opposite bank (going beyond the end of the string). You may choose any initial distance for the throw,

which starts from the left side of the string. A distance of 1 is the first character of the string, etc. If no initial throw will result in the stone

landing in the water without hitting an obstruction, return 0.


Definition:

Class: SkipStones
Method: maxDistance
Parameters: String
Returns: int
Method signature: int maxDistance(String water)*/

package skipStones;

public class SkipStones {

public static int maxDistance(String water) {
int dis = initDistance(water.length());

int[] distances = new int[dis];
for(int i = 0; i < dis; i++) {
distances[i] = i + 1;
}

int result = removeStone(distances, water);

int temp = result;
while((temp = temp/2) != 0) {
result = result + temp;
}

return result;
}

private static int initDistance(int length) {

int[] possibleTree = new int[length];
int path = length - 1;

for(int i=0; i < length; i++) {
if(i == 0) {
possibleTree[i] = path;
} else {
possibleTree[i] = possibleTree[i/2] - i - 1;
if(possibleTree[i] == 0) return i + 1;
else if(possibleTree[i] < 0) return i;

if (i + 1 < length) {
possibleTree[i + 1] = possibleTree[i / 2] - i - 2;
if (possibleTree[i + 1] == 0) return i + 2;
else if (possibleTree[i + 1] < 0) return i + 1;

i++;
}
}
}
return 0;
}

private static int removeStone(int[] distances, String water) {
char[] stones = water.toCharArray();

for (int i = 0; i < stones.length; i++) {
if (stones[i] == 'X') {
if (i < distances.length) {
distances[i] = 0;
}
possibleDistance(distances, i + 1);
}
}

for(int k = distances.length - 1; k >= 0; k--) {
if(distances[k] != 0) return distances[k];
}

return 0;
}

private static void possibleDistance(int[] distances, int length) {

int counter = 0;

for(int i=0; i < distances.length; i++) {
int temp = distances[i];
if(temp == length) {
distances[i] = 0;
} else {
int value = distances[i];
while((temp = temp/2) != 0) {
value = value + temp;
if(value == length) {
distances[i] = 0;
break;
}
}
}
}
}
}

资源下载链接为: https://pan.quark.cn/s/d3128e15f681 眨眼检测是一种生物特征识别技术,广泛应用于人机交互、疲劳驾驶监测等领域。本项目采用 Python 编程语言,结合 dlib 和 sklearn(Scikit-learn)库实现眨眼检测功能。dlib 是一个功能强大的 C++ 库,包含丰富的机器学习算法和工具,可方便地在 Python 中调用;而 sklearn 是 Python 中最受欢迎的机器学习库之一,主要用于数据挖掘和数据分析。 要实现眨眼检测,首先需要获取面部特征。dlib 库中的 shape_predictor 模型能够检测和定位面部关键点,包括眼睛位置。该模型通过预先训练好的 .dat 文件实现,项目中需引入此文件以实时定位人脸和眼睛。接下来,需定义算法判断眼睛状态,通常通过计算眼睛开放程度(眼睑闭合程度)实现,可采用计算眼睛区域像素差异或利用特定特征点(如眼角)的方法。获取这些信息后,可借助机器学习算法构建眨眼检测器。sklearn 库中的分类器(如 SVM 或决策树)可用于训练模型,根据眼睛状态(开放或闭合)预测是否眨眼。训练时需使用标注好的数据集,包含不同人的眨眼和非眨眼图像,这些图像需分为训练集和测试集,用于训练模型和评估性能。训练过程包括特征提取、特征选择和模型调优等,以达到最佳预测效果。在实际应用中,该系统可结合视频流处理,实时分析每一帧图像,检测到眨眼事件后可执行相应操作,如记录疲劳状态、提醒用户休息等。 项目文件夹 blink_detect 的结构如下:1. shape_predictor_68_face_landmarks.dat:dlib 的人脸关键点检测模型文件。2. preprocess.py:用于对图像进行预处理,如尺寸调整、灰度化等操作。3. eyelid_detector.py:包含眼睛状态检
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值