zoukankan      html  css  js  c++  java
  • 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;
    	}
    	
    }
    

      

  • 相关阅读:
    Hystrix熔断原理
    FeignClient Hystrix超时重试降级
    Windows上Docker Toolbox修改镜像源
    Windows10家庭版安装docker
    Class类文件结构--访问标志
    MySQL常见的七种锁详细介绍()
    maven setting.xml 阿里云镜像 没有一句废话
    mysql 批量操作,已存在则修改,不存在则insert,同时判断空选择性写入字段
    hdu5730
    3月部分题目简要题解
  • 原文地址:https://www.cnblogs.com/caijing/p/3367316.html
Copyright © 2011-2022 走看看