Problem description can be found here.
A. Alien Numbers
Follow is the solution:
public String getTargetNumber(String alienNumber,String srcLang,String targetLang) {
String ret = "";
// convert alienNumber to decimal number first
int decimalNumber = 0;
//the base of src language. 几进制
int srcBaseNum = srcLang.length();
//the base of target language.
int targetBaseNum = targetLang.length();
// alien number length
int alienLen = alienNumber.length();
//begin to calculate decimal number corresponding to alienNumber
int i = alienLen;
while(i>0) {
decimalNumber += srcLang.indexOf(alienNumber.charAt(alienLen-i))*Math.pow(srcBaseNum,i-1);
i--;
}
//convert decimal number to target alien number system's nubmer
// refer the information about how to convert decimal to binary or hex
while(decimalNumber!=0) {
int remainder = decimalNumber%targetBaseNum;
decimalNumber = decimalNumber/targetBaseNum;
ret = targetLang.charAt(remainder) + ret;
}
return ret;
}
B. Always Turn Left
Follow is the solution:
public class AlwaysTurnLeft {
Map<Integer,Integer> dict = new HashMap<Integer, Integer>();
final int NORTH_VALUE = 1; //0001
final int SOUTH_VALUE = 2; //0010
final int WEST_VALUE = 4; //0100
final int EAST_VALUE = 8; //1000
final int NORTH =0;
final int WEST = 1;
final int SOUTH = 2;
final int EAST = 3;
public AlwaysTurnLeft() {
dict.put(NORTH,NORTH_VALUE);
dict.put(WEST,WEST_VALUE);
dict.put(SOUTH,SOUTH_VALUE);
dict.put(EAST,EAST_VALUE);
}
class Coordinate {
public Coordinate() {
}
public Coordinate(int _x, int _y) {
x = _x;
y = _y;
}
public int x;
public int y;
}
int process(String path, int direction, List<Map<Integer,Integer>> mazeInfo, Coordinate curCoor) {
char[] arr = path.toCharArray();
for(int i=1;i<arr.length-1;i++) {
if(mazeInfo.size()<=curCoor.x) {
mazeInfo.add(new HashMap<Integer,Integer>());
}
Map<Integer,Integer> rowMap = mazeInfo.get(curCoor.x);
if(rowMap.get(curCoor.y)==null) {
rowMap.put(curCoor.y,0);
}
int cellInfo = rowMap.get(curCoor.y);
char c = arr[i];
switch(c) {
case 'W':
int toAddValue = dict.get(direction);
if(cellInfo/toAddValue%2==0) {
rowMap.put(curCoor.y,cellInfo+toAddValue);
}
switch(direction) {
case NORTH: curCoor.x--; break;
case WEST: curCoor.y--; break;
case SOUTH: curCoor.x++; break;
case EAST: curCoor.y++; break;
}
break;
case 'R':
direction--;
break;
case 'L':
direction++;
break;
}
direction += 4;
direction %= 4;
}
return direction;
}
public List<String> getMazeInfo(String entrance_to_exit, String exit_to_entrance) {
// At begin, the direction is SOUTH.
int direction = SOUTH;
// mazeInfo represent information for maze
// Map<Integer, Integer>
// key is maze column index(maybe a negative number, because the entrance is at [0,0].).
// value represents information for all directions that current cell can walk to.
List<Map<Integer,Integer>> mazeInfo = new ArrayList<Map<Integer,Integer>>();
// add information for entrance.
mazeInfo.add(new HashMap<Integer,Integer>());
mazeInfo.get(0).put(0,0);
Coordinate curCoor = new Coordinate(0,0);
List<String> ret = new ArrayList<String>();
// process path for entrance to exit
direction = process(entrance_to_exit,direction,mazeInfo,curCoor);
// for entrance cell, add north possibility
mazeInfo.get(0).put(0,mazeInfo.get(0).get(0)+NORTH_VALUE);
// for exit cell, add corresponding direction possibility. should add information for exit first.
if(mazeInfo.size()<=curCoor.x) {
mazeInfo.add(new HashMap<Integer,Integer>());
}
if(mazeInfo.get(curCoor.x).get(curCoor.y)==null) {
mazeInfo.get(curCoor.x).put(curCoor.y,0);
}
mazeInfo.get(curCoor.x).put(curCoor.y,mazeInfo.get(curCoor.x).get(curCoor.y)+dict.get(direction));
// turn right twice(180 degrees clockwise)
direction += 2;
direction %= 4;
//process path for exit to entrance
direction = process(exit_to_entrance,direction,mazeInfo,curCoor);
for(Map<Integer,Integer> m : mazeInfo) {
SortedSet<Integer> keySet = new TreeSet<Integer>(m.keySet());
StringBuilder sb = new StringBuilder();
for(Integer key : keySet) {
sb.append(Integer.toHexString(m.get(key)));
}
ret.add(sb.toString());
}
return ret;
}
public static void main(String args[]) throws Exception {
AlwaysTurnLeft t = new AlwaysTurnLeft();
List<String> list = t.getMazeInfo("WWRWLWLWRRWLWLWRRWLWWLWLWRRWWWWRRWLWLWRRWRWLWLWRRWLWLWRWWWWRWLWLWW","WWLW");
for(String s : list) {
System.out.println(s);
}
}
}
<style type="text/css">.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>
<style type="text/css">.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>
分享到:
相关推荐
A solution of contest problem in Google Code Jam
抱歉,CSDN目前无法设置免费资源,感兴趣的话在我博客下留言,我可以用邮件发给你。 Small input of Google code jam - World Finals 2017-Problem A. Dice Straight
google code jam
google codejam2014源代码
2008 Google Code Jam 第三轮网赛,lym和windy两位大牛的代码,很值得学习!
It's easy to find basic SQL syntax and keyword information online. What's hard to find is challenging, well... It contains challenging problems, which develop your ability to write high quality SQL code.
自己写的源码,希望各位看官指出不足之处,好交流进步
Google Codejam
Google Code Jam 练习题的解决方案 在与给定问题具有相同标题的 java 类中找到解决方案。 在这一点上,所有解决方案都针对在明确提到的作为良好起点的问题 每个解决方案都需要从命令行运行一个参数,一个包含问题...
Google Code Jam统计用于检查Google Code Jam轮次统计信息的网站为什么? 已经有一些很棒的网站(例如和 ),但它们仅提供截至2017年的数据。该网站使用于2018年推出的新Google Competitions API来收集并显示有关...
Code of Practice for Project Management for Construction and Development(5th) 英文无水印原版pdf 第5版 pdf所有页面使用FoxitReader、PDF-XChangeViewer、SumatraPDF和Firefox测试都可以打开 本资源转载自...
代码堵塞 Google Code Jam 解决方案
我的Google Code Jam旅程记录我的GCJ旅程的回购。 每年更新。 年日期圆形的结果评论2020年5月2日1C2020年4月20日1B数学太多2020年4月11日1A2020年4月4日资质做得好2019年5月4日1C搞砸了2019年4月28日1B需要更多练习...
这个资料库包含我在2021年之前解决Google Code Jam竞赛问题的解决方案,无论是在竞赛期间提交还是经过深思熟虑。 源代码使用Java,每个源文件都包含对所用算法的描述。 src目录中的Java软件包包含按Code Jam版本和...
谷歌codejam 我对Google Codejam的解决方案
BS, EN 6349-1-3-2021 General — Code of practice for geotechnical design.pdf
googlecodejam 我的Google Code Jam解决方案内容(比赛中的结果)-当前20 质量: Reversort(正确)-已解决月亮和雨伞(正确,正确,吗?)-已解决| 不是测试用例3 Reversort Engineering(正确,错误的答案)-特定...
Google Code Jam for I / O for Women 2021年4月17日我的解决方案(在Java SDK 11中)针对Google Code Jam for Women 2021的解决方案。您可以在此处查看比赛: : 请客气; 这是一个正在进行的工作! 问题摘要如下。 ...
README File for 8051 Jam Byte-Code Player v1.0 What Does v1.0 Support? The source code supports programming devices from the 8051 family of microprocessors. Jam Byte-Code (.jbc) files from the ...
Google-Codejam Google举办时间最长的全球编码竞赛Code Jam呼吁世界各地的程序员日以继夜地解决具有挑战性的算法难题。 参赛者将进行四轮在线托管竞赛,以参加每年在不同的Google国际办公室举行的年度Code Jam世界总...