论坛首页 招聘求职论坛

从前有个迷宫__面试题

浏览 13908 次
精华帖 (6) :: 良好帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2009-12-16  
[
[0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0],
[0,0,0,1,0,0,0,0],
[0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0]
]
1是小女孩儿
0 请登录后投票
   发表时间:2009-12-16   最后修改:2009-12-16
写这个程序时最大的遗憾是:
enum
类型中的元素没有next方法.
只能自己手动的加上.....
好失败啊.

我写这东西主要目的
是为了让人看的明白

记的住
0 请登录后投票
   发表时间:2009-12-16  
又看到这个朴素的头像了,呵呵……
0 请登录后投票
   发表时间:2009-12-17  
我不DJ的补充下

抛抛把girl都拼成了gril  
0 请登录后投票
   发表时间:2009-12-17   最后修改:2009-12-17
jenlp520 写道
我不DJ的补充下

抛抛把girl都拼成了gril  

好吧我重构一下再看看
代码不是很多了.

public class LostGirl {
	public Point  point = new Point();
	public Compass compass = Compass.东;    //初方向
	public int noWay = 0 ;


	public int walk(Maze maze){
		if(noWay==0){
			maze.breadRoad(point);//如果不是退回来的话就要作记号
		}
		
		compass.run(point);
		
		if(maze.fallDown(point)||maze.crushedRock(point)){
			compass.back(point);//原路退回
			noWay++;//前方是死路一条
			compass=compass.next();//换个方向	
		}else{
			noWay=0;
		}
		
		return noWay;
	}	
	public static void main(String[] args) {
		Maze maze = new Maze(3);
		LostGirl girl = new LostGirl();

		while (girl.walk(maze) < 4) {
			//System.out.println(maze); //偷窥路线
		}
		System.out.println(maze);
		
		
	}

}

手写了next函数
我认为这个应该在api中有....
enum Compass{
	东(1,0){
		public Compass next(){return 南;	}		
	},南(0,1){
		public Compass next(){return 西;	}		
	},西(-1,0){
		public Compass next(){return 北;	}		
	},北(0,-1){
		public Compass next(){return 东;	}		
	};
	
	int x,y;
	private Compass(int x, int y) {
		this.x = x ;
		this.y = y;
	}
	public void back(Point point) {
		point.translate(-x, -y);		
	}
	public void run(Point point){
		point.translate(x, y);
	}
	abstract Compass next();

}


public class Maze {
	Map<Point, Integer> map = new HashMap<Point, Integer>();
	private int bread = 1;
	private int length = 0 ;
	/**
	 * 初始化迷宫
	 * @param length
	 */
	public Maze(int length){
		this.length = length;
	}
	/**
	 * 标记已走过的位置
	 * @param point
	 */
	public void breadRoad(Point point) {
		map.put(new Point(point),bread++);
	}
	/**
	 * 这个点不在数组之中
	 * @param point
	 * @return
	 */
	public boolean fallDown(Point point) {
		return (point.x>=length||point.x<0||point.y>=length||point.y<0);
	}
	/**
	 * 这个点已经被标记过
	 * @param point
	 * @return
	 */
	public boolean crushedRock(Point point) {
		 return map.get(point)!= null ;
	}
	/**
	 * 打印迷宫状态
	 */
	@Override
	public String toString() {
		StringBuilder builder = new StringBuilder();
		for(int i =0 ; i <length;i++){
			for(int j = 0 ; j < length ; j ++){
				Integer show = map.get(new Point(j,i));
				builder.append(show);
				builder.append("\t");
			}
			builder.append("\n");
		}
		return builder.toString();
	}
}
0 请登录后投票
   发表时间:2009-12-17   最后修改:2009-12-17
我对他们的回复很不满:不仅代码表意不清,而且没有好好的对齐打印。
依据抛哥的指导思想,写了 Ruby 版:

# coding: utf-8
require 'matrix'

def 诱 数
  妹, 鼠 = Vector[0,0], Vector[0,1]
  抓 = Matrix[[0,1],[-1,0]] # 正经点说: 这个是旋转矩阵
  摸 = []
  (0...数).each{|位| 摸[位] = Array.new 数, '纯洁'}
  1.upto(数 * 数){|吃|
    女, 未 = 妹.to_a
    摸[女][未] = 吃
    排, 雷 = (妹 + 鼠).to_a
    里 = (排 >= 0 and 排 < 数 and 雷 >= 0 and 雷 < 数)
    鼠 = 抓 * 鼠 unless (里 and 摸[排][雷] == '纯洁') # 鼠死重抓
    妹 += 鼠
  }
  齐 = (数 * 数).to_s.size
  puts "英特 爱=#{数};".encode(Encoding.default_external) rescue nil
  puts 摸.map{|行| 行.map{|粒| 粒.to_s.ljust 齐}.join ' '}.join("\n")
end

诱 5
诱 6

0 请登录后投票
   发表时间:2009-12-17  
刑天战士 写道
lz是否面试的无限讯奇?


无限讯奇面试题不是这样的,话说哪天去那面试,还真看见不少MM
0 请登录后投票
   发表时间:2009-12-24  
讲得狠详细,,看下可能会用到!
0 请登录后投票
   发表时间:2009-12-25  
supersun 写道
刑天战士 写道
lz是否面试的无限讯奇?


无限讯奇面试题不是这样的,话说哪天去那面试,还真看见不少MM

部门和部门之前不一样的,我是基础技术部,你面的哪个?基础技术部和搜索部对技术要求很高。我面试的时候被5个人LJ……
0 请登录后投票
   发表时间:2009-12-31  

又重写Compass
next循环解决了
enum Compass{
	东(1,0),南(0,1),西(-1,0),北(0,-1);	
	int x,y;
	private Compass(int x, int y) {
		this.x = x ;
		this.y = y;
	}
	public void back(Point point) {
		point.translate(-x, -y);		
	}
	public void run(Point point){
		point.translate(x, y);
	}
	public static Compass next(Compass compass){
		Iterator<Compass> it = new LoopingIterator(EnumSet.allOf(Compass.class));
		while(!compass.equals(it.next())){}		
		return it.next();
	}

}


0 请登录后投票
论坛首页 招聘求职版

跳转论坛:
Global site tag (gtag.js) - Google Analytics