`
yangphere
  • 浏览: 78446 次
  • 性别: Icon_minigender_1
  • 来自: 重庆
社区版块
存档分类
最新评论

蚂蚁爬杆问题的java实现

阅读更多
/** 
 * Filename:    com.yangphere.demo.AntWalker.java 
 * Description:  题目描述: 有一根27厘米的细木杆, 在第3厘米、7厘米、11厘米、17厘米、23厘米这五个位置上各有一只蚂蚁。 木杆很细,不能同时通过一只蚂蚁。
 * 开始时,蚂蚁的头朝左还是朝右是任意的,它们只会朝前走或调头,但不会后退。 当任意两只蚂蚁碰头时,两只蚂蚁会同时调头朝反方向走。
 * 假设蚂蚁们每秒钟可以走一厘米的距离。 编写程序,求所有蚂蚁都离开木杆的最小时间和最大时间。
 * Copyright:   Copyright (c)2009 
 * Company:     yangphere 
 * @author:     yangphere 
 * @version:    1.0 
 * Create at:   2009-6-28
 * 
 * Modification History: 
 * Date			Author		Version		Description 
 * ------------------------------------------------------------------ 
 * 2009-6-28	yangphere	1.0			1.0 Version 
 */
package com.yangphere.demo;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * 配合逻辑分析给出一种简单的实现方式
 * 其中有个很重要的原则是:当任意两只蚂蚁碰头时,两只蚂蚁会同时调头朝反方向走,其实和假设两只蚂蚁之间可以无障碍穿过时是一样的(忽略蚂蚁自身的长度)。
 * 
 * @author yangphere
 * 
 */
public class AntWalker {

	/**
	 * 细木杆长度
	 */
	private static final int TOTAL_LENGTH = 27;

	/**
	 * 蚂蚁的位置
	 */
	private static final List<Integer> ANT_PER_POSITION = new ArrayList<Integer>();

	/**
	 * 初始化蚂蚁位置
	 */
	static {
		ANT_PER_POSITION.add(3);
		ANT_PER_POSITION.add(7);
		ANT_PER_POSITION.add(11);
		ANT_PER_POSITION.add(17);
		ANT_PER_POSITION.add(23);
	}

	/**
	 * 
	 * @author yangphere 2009-6-28
	 * @param args
	 */
	public static void main(String[] args) {
		if (!check()) {
			System.out.println("参数错误!");
		} else {
			System.out.println("最短时间是:" + getMinTime() + "秒");
			System.out.println("最长时间是:" + getMaxTime() + "秒");
		}
	}

	/**
	 * 获得最长时间 根据那个原则,无需关心蚂蚁碰撞的问题,最长时间就是所有蚂蚁中距细杆一端最长的那只蚂蚁的爬完时间
	 * 
	 * @author yangphere 2009-6-29
	 * @return
	 */
	private static int getMaxTime() {
		// 对蚂蚁位置排序
		Collections.sort(ANT_PER_POSITION);
		// 获得最靠两端的蚂蚁
		int oneSide = ANT_PER_POSITION.get(0);
		int anotherSide = ANT_PER_POSITION.get(ANT_PER_POSITION.size() - 1);

		// 获得一只蚂蚁距两端最长的距离
		int oneMax = (oneSide > (TOTAL_LENGTH - oneSide)) ? oneSide
				: TOTAL_LENGTH - oneSide;
		int antherMax = (anotherSide > (TOTAL_LENGTH - anotherSide)) ? anotherSide
				: TOTAL_LENGTH - anotherSide;

		return oneMax > antherMax ? oneMax : antherMax;
	}

	/**
	 * 获得最短时间 根据那个原则,最短时间就是距细杆中间最近的那只蚂蚁爬完细杆最短的那端所花的时间
	 * 
	 * @author yangphere 2009-6-29
	 * @return
	 */
	private static int getMinTime() {

		// 蚂蚁距一端的距离
		int oneSide = getNearCenterAnt();

		// 蚂蚁距另一段的距离
		int anotherSide = TOTAL_LENGTH - oneSide;

		return oneSide < anotherSide ? oneSide : anotherSide;
	}

	/**
	 * 获得最接近细杆中间的蚂蚁
	 * 
	 * @author yangphere 2009-6-29
	 * @return
	 */
	private static int getNearCenterAnt() {
		// 中间位置
		double center = TOTAL_LENGTH / 2.0;
		// 离中间最近的蚂蚁的位置
		int antNearCenter = 0;

		Map<Integer, Double> map = new HashMap<Integer, Double>();

		double antNearPosition = center - ANT_PER_POSITION.get(0);
		for (Integer ant : ANT_PER_POSITION) {
			antNearPosition = antNearPosition < Math.abs(center - ant) ? antNearPosition
					: Math.abs(center - ant);

			map.put(ant, Math.abs(center - ant));
		}

		Set<Integer> set = map.keySet();
		for (Iterator iterator = set.iterator(); iterator.hasNext();) {
			Integer key = (Integer) iterator.next();
			if (antNearPosition == map.get(key)) {
				antNearCenter = key;
				break;
			}
		}

		return antNearCenter;
	}

	/**
	 * 检查参数的正确性
	 * 
	 * @author yangphere 2009-6-29
	 * @return
	 */
	private static boolean check() {
		if (TOTAL_LENGTH <= 0) {
			return false;
		} else if (ANT_PER_POSITION.size() <= 0) {
			return false;
		}

		// 站在杆外
		for (Integer ant : ANT_PER_POSITION) {

			if (ant <= 0) {
				return false;
			} else if (ant > TOTAL_LENGTH) {
				return false;
			}
		}

		return true;
	}

}

分享到:
评论

相关推荐

    蚂蚁爬杆问题

    蚂蚁爬杆问题 A.B.C三只蚂蚁不同速度在一个杆上爬行,求蚂蚁爬出杆的时间问题

    1蚂蚁爬杆之动态演示

    1. 源代码文件(.java):这些是用Java编写的程序,实现了蚂蚁爬杆的逻辑和动画展示。 2. 类库文件(.jar):可能包含了用于图形渲染和其他功能的外部Java类库。 3. 资源文件(如图片、音频):可能包括蚂蚁和杆子的...

    蚂蚁爬行问题源码

    《蚂蚁爬行问题的Java实现及其算法解析》 在计算机科学领域,模拟自然界的现象并从中学习解决问题的方法是一种常见的策略。蚂蚁爬行问题就是一个这样的例子,它源于对真实世界蚂蚁行为的观察,然后通过编程来模拟...

    27厘米的细木杆上蚂蚁爬杆

    6. **异常处理**:在程序中,对于蚂蚁爬出木杆的情况,通过设置`hasRemove`标志来终止蚂蚁的移动,并清空当前位置的蚂蚁。 7. **时间复杂度**:由于每秒蚂蚁只能移动一厘米,且每次移动都会检查是否与其他蚂蚁碰撞...

    蓝桥杯部分题目(含答案)-word版-21页

    题目要求计算所有蚂蚁都爬离杆子后,感染感冒的蚂蚁总数。 **输入格式**: - 第一行输入一个整数 `n` (1 ),表示蚂蚁的总数。 - 接下来的一行包含 `n` 个整数 `Xi` (-100 ),表示每只蚂蚁距离杆子左边端点的距离。...

Global site tag (gtag.js) - Google Analytics