careercup题目20131013

I need a function that takes the name of the movie to look up and 
the width of the letter grid, and computes the key presses that 
will enter that string on the DVR grid. The output should be a 
string, with "u", "d", "l", "r", and "!" corresponding to up, 
down, left, right, and select. 

For example, with a grid of width 5, 
a b c d e 
f g h i j 
k l m n o 
p q r s t 
u v w x y 

the movie "up" would be "dddd!u!".

翻译:我需要一个函数传入电影名称和字母表的宽度,然后计算把该电影名字符串输入到DVR系统中的按键顺序。输出应该是一个字符串,

用 "u", "d", "l", "r", and "!"代表up, down, left, right, and select. 

比如:宽度为5的表格

a b c d e 
f g h i j 
k l m n o 
p q r s t 
u v w x y 
z

电影: "up" 应该输出:"dddd!u!".

分析:根据宽度逐个计算出字符位置,再结合当前位置,获得路径。要注意最后一排字母可能有空位置,获得路径时,不能进入这些位置。

代码:

public class DVR {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		System.out.println(pathToChar("up",5));
	}
	
	public static String pathToChar(String movieName,int width){
		StringBuffer sb = new StringBuffer();
		
		Location currentLocation = new Location(0,0);
		char[] chs = movieName.toCharArray();
		for(int i=0;i<chs.length;i++){
			Location l = getLocation(chs[i],width);
			String s = getPathBetweenTwoLocation(currentLocation,l,width);
			currentLocation = l;
			System.out.println(l.toString());
			sb.append(s);
		}
		
		return sb.toString();
	}
	
	/**
	 * 获得当前节点的位置
	 * @param c
	 * @param width
	 * @return
	 */
	public static Location getLocation(char c,int width){
		Location l = new Location();
		if(c>='A'&&c<='Z'){
			int cInt = Integer.valueOf(c);
			cInt = cInt - 26;
			c = (char) cInt;
		}else if(c>='a'&&c<='z'){
			;
		}else{
			return null;
		}
		int sequence = c - 'a';
		l.row = sequence/width;
		l.line = sequence%width;
		return l;
	}
	
	/**
	 * 根据两点的坐标,和字母表宽度获得两点之间的路径,要注意应避免进入最后一的空位置
	 * @param curr
	 * @param target
	 * @param width
	 * @return
	 */
	public static String getPathBetweenTwoLocation(Location curr,Location target,int width){
		StringBuffer sb = new StringBuffer();
		Location lastLocation = getLocation('z',width);
		
		if(curr.line == lastLocation.line&&target.line != lastLocation.line && lastLocation.row > target.row){
			//需要先往上绕过转角
			int heightDistance = target.row - curr.row;
			int distance = Math.abs(heightDistance);
			while(distance>0){
				sb.append("U");
				distance--;
			}
			int widthDistance = target.line - curr.line;
			distance = Math.abs(widthDistance);
			while(distance>0){
				sb.append("R");
				distance--;
			}
			sb.append("!");
		}else if(target.line==lastLocation.line && curr.line != lastLocation.line && curr.row > lastLocation.row){
			//此时需先向左绕过转角
			int widthDistance = curr.line - target.line;
			int distance = Math.abs(widthDistance);
			while(distance>0){
				sb.append("L");
				distance--;
			}
			int heigthDistance = target.row - curr.row;
			while(heigthDistance>0){
				sb.append("D");
			}
			sb.append("!");
		}else{
			int widthDistance = curr.line - target.line;
			int heigthDistance = curr.row - target.row;
			while(widthDistance > 0){
				sb.append("L");
				widthDistance--;
			}
			while(widthDistance < 0){
				sb.append("R");
				widthDistance++;
			}
			while(heigthDistance > 0){
				sb.append("U");
				heigthDistance--;
			}
			while(heigthDistance < 0){
				sb.append("D");
				heigthDistance++;
			}
			sb.append("!");
		}
		return sb.toString();
	}
}
class Location{
	int row;
	int line;
	public Location(){
		
	}
	public Location(int row,int line ){
		this.row = row;
		this.line = line;
	}
	public String toString(){
		return row+"-"+line;
	}
	
}

  

posted on 2013-10-13 23:00 逍遥骑士 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/caijing/p/3367316.html

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值