亚洲av成人无遮挡网站在线观看,少妇性bbb搡bbb爽爽爽,亚洲av日韩精品久久久久久,兔费看少妇性l交大片免费,无码少妇一区二区三区

  免費(fèi)注冊 查看新帖 |

Chinaunix

  平臺 論壇 博客 文庫
最近訪問板塊 發(fā)新帖
查看: 2342 | 回復(fù): 2
打印 上一主題 下一主題

我的google code jam的題解,希望一起交流 [復(fù)制鏈接]

論壇徽章:
0
跳轉(zhuǎn)到指定樓層
1 [收藏(0)] [報告]
發(fā)表于 2005-12-20 11:43 |只看該作者 |倒序瀏覽
那是十幾號呢,上午十點(diǎn)五十分接到topecoder的提示郵件,說距離google競賽將在今日上午十二點(diǎn)準(zhǔn)時結(jié)束。才想起自己也注冊了,所以就進(jìn)去看了看,但當(dāng)時正要開會。所以僅把試題copy了下來。

然后程序練習(xí)編碼了一下,基本是一位實習(xí)的研究生編的。后來其程序受到一位IBM新加坡高手的指點(diǎn)改進(jìn),其主要從事RFID的開發(fā)。

俺呢,把程序發(fā)到這兒,當(dāng)然匯總了一下,比如格式化啊(用eclipse的format功能哩),但還稱之為俺的題解吧,但其實俺僅是把程序讀懂了而已,還沒理解……
1、BusStops

Problem Statement
????
You are given a String[] cityMap representing the layout of a city. The city consists of blocks. The first element of cityMap represents the first row of blocks, etc. A 'B' character indicates a location where there is a bus stop. There will be exactly one 'X' character, indicating your location. All other characters will be '.'. You are also given an int walkingDistance, which is the maximum distance you are willing to walk to a bus stop. The distance should be calculated as the number of blocks vertically plus the number of blocks horizontally. Return the number of bus stops that are within walking distance of your current location.
Definition
????
Class:
BusStops
Method:
countStops
Parameters:
String[], int
Returns:
int
Method signature:
int countStops(String[] cityMap, int walkingDistance)
(be sure your method is public)
????

Constraints
-
cityMap will contain between 1 and 50 elements, inclusive.
-
Each element of cityMap will contain between 1 and 50 characters, inclusive.
-
Each element of cityMap will contain the same number of characters.
-
Each character of each element of cityMap will be 'B', 'X', or '.'.
-
There will be exactly one 'X' character in cityMap.
-
walkingDistance will be between 1 and 100, inclusive.
Examples
0)

????
{"...B.",
".....",
"..X.B",
".....",
"B...."}
3
Returns: 2
You can reach the bus stop at the top (3 units away), or on the right (2 units away). The one in the lower left is 4 units away, which is too far.
1)

????
{"B.B..",
".....",
"B....",
".....",
"....X"}
8
Returns: 3
A distance of 8 can get us anywhere on the map, so we can reach all 3 bus stops.
2)

????
{"BBBBB",
"BB.BB",
"B.X.B",
"BB.BB",
"BBBBB"}
1
Returns: 0
Plenty of bus stops, but unfortunately we cannot reach any of them.
3)

????
{"B..B..",
".B...B",
"..B...",
"..B.X.",
"B.B.B.",
".B.B.B"}
3
Returns: 7

This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2003, TopCoder, Inc. All rights reserved.

我的程序:


  1. /**
  2. * @author:Ouds.Zhang
  3. * @version:v1.0
  4. * @Date:10:52:54,2005-12-13,2005 file name:BusStops.java type name:BusStops
  5. *                                package name: project name:google test
  6. */
  7. public class BusStops {
  8.         /**
  9.          * BusStops.java
  10.          */
  11.         private int xLocationX = -1;

  12.         private int xLocationY = -1;

  13.         private int counter = 0;
  14.        
  15.         private int walkingDistance;
  16.        
  17.         private String[] cityMap;
  18.        
  19.         private static final String BUSSTOP = "B";
  20.        
  21.         private static final String YOUAREHERE = "X";
  22.        
  23.         private int x0 = -1;
  24.         private int x1 = -1;
  25.         private int y0 = -1;
  26.         private int y1 = -1;
  27.        
  28.         public int countStops(String[] cityMap, int walkingDistance) {
  29.                
  30.                 this.walkingDistance = walkingDistance;
  31.                 this.cityMap = cityMap;
  32.                
  33.                 this.processXLocation();
  34.                
  35.                 this.process();
  36.                
  37.                 System.out.print(counter);
  38.                 return counter;
  39.                
  40.         }
  41.        
  42.         private void processXLocation(){
  43.                 for (int i = 0; i < cityMap.length; i++) {
  44.                          xLocationX = cityMap[i].indexOf(YOUAREHERE);
  45.                         if(xLocationX > -1){
  46.                                 xLocationY = i;
  47.                                 System.out.println(xLocationX+","+xLocationX);
  48.                                
  49.                                 int x0tmp = xLocationX - walkingDistance;
  50.                                 x0 = x0tmp > -1? x0tmp:0;
  51.                                
  52.                                 int x1tmp = xLocationX + walkingDistance;
  53.                                 x1 = x1tmp >= cityMap[i].length()-1? cityMap[i].length():x1tmp;
  54.                                
  55.                                 int y0tmp = xLocationY - walkingDistance;
  56.                                 y0 = y0tmp > -1? y0tmp:0;
  57.                                
  58.                                 int y1tmp = xLocationY + walkingDistance;
  59.                                 y1 = y1tmp >= cityMap.length-1? cityMap.length:y1tmp;
  60.                                
  61.                                 System.out.println("x0 = " + x0);
  62.                                 System.out.println("x1 = " + x1);
  63.                                 System.out.println("y0 = " + y0);
  64.                                 System.out.println("y1 = " + y1);
  65.                                 break;
  66.                         }
  67.                 }
  68.                
  69.                 if(xLocationX == -1 || xLocationY == -1) throw new IllegalArgumentException("No current location found");
  70.         }
  71.        
  72.         private void process(){
  73.                 for(int j = y0; j < y1; j ++){
  74.                         String xSegment = cityMap[j].substring(x0, x1);
  75.                         System.out.println("Now examing row[" + j + "] = " + xSegment);
  76.                         int res = xSegment.indexOf(BUSSTOP);
  77.                         while(res > -1){
  78.                                
  79.                                 //if res + j > walkingdistance, no point to continue search
  80.                                 System.out.println("res = " + res);
  81.                                 int stepX = Math.abs(xLocationX - res);
  82.                                 int stepY = Math.abs(xLocationY - j);
  83.                                
  84.                                 if((stepX + stepY)<= walkingDistance) {

  85.                                         String xDirection = xLocationX - res > 0?"left":"right";
  86.                                         String yDirection = xLocationY - j > 0 ? "up":"down";
  87.                                        
  88.                                         System.out.println("you may go " + stepX + " step " + xDirection + " and " + stepY + " step " + yDirection);
  89.                                         counter ++;
  90.                                 }
  91.                                 else break;
  92.                                
  93.                                 res = xSegment.indexOf(BUSSTOP, res + 1);
  94.                         }
  95.                 }
  96.         }
  97.                
  98.         public static void main(String[] arg){
  99.                 /**
  100.                 String[] cityMap={"...B.",
  101.                                                   ".....",
  102.                                                   "..X.B",
  103.                                                   ".....",
  104.                                                   "B...."};
  105.                 int walkingDistance = 3;
  106.                 */
  107.                 validateArguments(arg);
  108.                
  109.                 String[] cityMap= new String[arg.length -1];
  110.                 int walkingDistance = -1;
  111.                
  112.                 System.arraycopy(arg,0,cityMap,0,arg.length-1);
  113.                 walkingDistance = Integer.parseInt(arg[arg.length-1]);
  114.                
  115.                 /**
  116.                  * Now print debug info
  117.                  */
  118.                 System.out.println("==================================");
  119.                 System.out.println("==============City Map============");
  120.                 for(int i=0; i<cityMap.length; i ++){
  121.                         System.out.println("\t" + cityMap[i]);
  122.                 }
  123.                 System.out.println("Walking Distance = " + walkingDistance);
  124.                 System.out.println("==================================");
  125.                 new BusStops().countStops(cityMap, walkingDistance);
  126.         }
  127.        
  128.         public static void usage(){
  129.                 System.out.println("java BusStops mapRow1, mapRow2, .... mapRowN, walkingDistance");
  130.         }
  131.        
  132.         public static void validateArguments(String[] args){
  133.                 if(args == null || args.length <= 1){
  134.                         usage();
  135.                         System.exit(0);
  136.                 }
  137.                
  138.                 try{
  139.                         Integer.parseInt(args[args.length-1]);
  140.                 }catch(Exception e){
  141.                         usage();
  142.                         System.exit(0);
  143.                 }
  144.                
  145.                 int len = args[0].length();
  146.                 for(int i=1; i<args.length -1; i ++){
  147.                         if(len != args[i].length()){
  148.                                 System.out.println("Not all city map row are equal in length");
  149.                                 System.exit(0);
  150.                         }
  151.                 }
  152.         }
  153. }

復(fù)制代碼

[ 本帖最后由 aoeiu 于 2005-12-20 11:49 編輯 ]

論壇徽章:
0
2 [報告]
發(fā)表于 2005-12-20 11:47 |只看該作者

WordPaths

Problem Statement
????
You are given a String[] grid representing a rectangular grid of letters. You are also given a String find, a word you are to find within the grid. The starting point may be anywhere in the grid. The path may move up, down, left, right, or diagonally from one letter to the next, and may use letters in the grid more than once, but you may not stay on the same cell twice in a row (see example 6 for clarification).
You are to return an int indicating the number of ways find can be found within the grid. If the result is more than 1,000,000,000, return -1.
Definition
????
Class:
WordPath
Method:
countPaths
Parameters:
String[], String
Returns:
int
Method signature:
int countPaths(String[] grid, String find)
(be sure your method is public)
????

Constraints
-
grid will contain between 1 and 50 elements, inclusive.
-
Each element of grid will contain between 1 and 50 uppercase ('A'-'Z') letters, inclusive.
-
Each element of grid will contain the same number of characters.
-
find will contain between 1 and 50 uppercase ('A'-'Z') letters, inclusive.
Examples
0)

????
{"ABC",
"FED",
"GHI"}
"ABCDEFGHI"
Returns: 1
There is only one way to trace this path. Each letter is used exactly once.
1)

????
{"ABC",
"FED",
"GAI"}
"ABCDEA"
Returns: 2
Once we get to the 'E', we can choose one of two directions for the final 'A'.
2)

????
{"ABC",
"DEF",
"GHI"}
"ABCD"
Returns: 0
We can trace a path for "ABC", but there's no way to complete a path to the letter 'D'.
3)

????
{"AA",
"AA"}
"AAAA"
Returns: 108
We can start from any of the four locations. From each location, we can then move in any of the three possible directions for our second letter, and again for the third and fourth letter. 4 * 3 * 3 * 3 = 108.
4)

????
{"ABABA",
"BABAB",
"ABABA",
"BABAB",
"ABABA"}
"ABABABBA"
Returns: 56448
There are a lot of ways to trace this path.
5)

????
{"AAAAA",
"AAAAA",
"AAAAA",
"AAAAA",
"AAAAA"}
"AAAAAAAAAAA"
Returns: -1
There are well over 1,000,000,000 paths that can be traced.
6)

????
{"AB",
"CD"}
"AA"
Returns: 0
Since we can't stay on the same cell, we can't trace the path at all.
This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2003, TopCoder, Inc. All rights reserved.

實現(xiàn)程序:


  1. public class WordPath {
  2.        
  3.         private char[][] charMatrix;
  4.         private String key;
  5.        
  6.         private int x_min = 0;
  7.         private int y_min = 0;
  8.         private int x_max;
  9.         private int y_max;
  10.        
  11.         private static final int MAX_NUMBER = 100000000;
  12.         private static final int MAX_OPERATION = 8;
  13.        
  14.         public int countPaths(String[] grid, String find){
  15.                 this.key = find;
  16.                 this.buildMatrix(grid);
  17.                
  18.                 int total = this.calculatePath(init());
  19.                
  20.                 System.out.println("Total number of paths = " + total);
  21.                 return total <= MAX_NUMBER? total:-1;
  22.         }
  23.        
  24.         private int[][] init(){
  25.                 char initKey = key.charAt(0);
  26.                 int[][] points = new int[x_max*y_max][2];
  27.                 int idx = -1;
  28.                 for(int i=x_min; i < x_max; i++){
  29.                         int jIdx = charMatrix[i].length;
  30.                         for(int j = 0;j<jIdx; j++){
  31.                                 idx = addOperation(i, j, points, initKey, idx);
  32.                         }
  33.                 }
  34.                
  35.                 int[][] res = idx == -1? null:trimIntArray(points, idx);
  36.                 return res;
  37.         }
  38.        
  39.         private void buildMatrix(String[] elements){
  40.                 y_max = elements.length;
  41.                
  42.                 for(int j = y_min; j<y_max; j++){
  43.                         char[] tmp = elements[j].toCharArray();
  44.                        
  45.                         //initialize stringMatrix
  46.                         if(charMatrix == null){
  47.                                 x_max = tmp.length;
  48.                                 charMatrix = new char[y_max][x_max];
  49.                         }
  50.                        
  51.                         System.arraycopy(tmp, 0, charMatrix[j], 0, x_max);
  52.                 }
  53.                
  54.                 printArray(charMatrix);
  55.         }
  56.        
  57.         /**
  58.          * Return current char index of charMatrix
  59.          * @param point
  60.          * @param keyIdx
  61.          * @return
  62.          */
  63.         private int calculatePath(int[][] point){
  64.                
  65.                 if(point == null) return 0;
  66.                
  67.                 int res = 0;
  68.                
  69.                 for(int i=0; i < point.length; i++){
  70.                         int x = point[i][0];
  71.                         int y = point[i][1];
  72.                         System.out.println("\n#####################INITIAL SERACH at (" + x + ", " + y + ")####################");
  73.                         res += calculatePathFromPoint(x, y, 0);
  74.                 }
  75.                
  76.                 return res;
  77.         }
  78.        
  79.         private int calculatePathFromPoint(int x, int y, int currentIdx){
  80.                
  81.                 if(currentIdx == key.length() -1){
  82.                         System.out.println("Searching complete!!! at point(" + x + ", " + y + ")");
  83.                         return 1;
  84.                 }else{
  85.                         currentIdx ++;
  86.                         System.out.println("Calculating from point (" + x + ", " + y + "), searching for " + key.charAt(currentIdx) + " with idx = " + currentIdx);
  87.                         int[][] nextPoints = getNextPoints(x, y, currentIdx);
  88.                        
  89.                         if(nextPoints == null) return 0;
  90.                        
  91.                         int count = nextPoints.length;
  92.                        
  93.                         int tmp = 0;
  94.                         for(int i=0;i<nextPoints.length;i++){
  95.                                 int point_x = nextPoints[i][0];
  96.                                 int point_y = nextPoints[i][1];
  97.                                
  98.                                 tmp += calculatePathFromPoint(point_x, point_y, currentIdx);
  99.                         }
  100.                        
  101.                         return tmp;
  102.                        
  103.                 }
  104.         }
  105.        
  106.         private int[][] getNextPoints(int point_x, int point_y, int keyIdx){
  107.                 //8 possible directions
  108.                 char keyChar = key.charAt(keyIdx);
  109.                
  110.                 int[][] operation = new int[MAX_OPERATION][2];
  111.                
  112.                 int idx = -1;
  113.                
  114.                 idx = addOperation(point_x - 1, point_y,operation, keyChar, idx);
  115.                 idx = addOperation(point_x - 1, point_y - 1,operation, keyChar, idx);
  116.                 idx = addOperation(point_x - 1, point_y + 1,operation, keyChar, idx);
  117.                 idx = addOperation(point_x + 1, point_y, operation, keyChar, idx);
  118.                 idx = addOperation(point_x + 1, point_y - 1,operation, keyChar, idx);
  119.                 idx = addOperation(point_x + 1, point_y + 1,operation, keyChar, idx);
  120.                 idx = addOperation(point_x, point_y + 1,operation, keyChar, idx);
  121.                 idx = addOperation(point_x, point_y - 1,operation, keyChar, idx);
  122.                
  123.                
  124.                 return idx == -1? null:trimIntArray(operation, idx);
  125.         }
  126.        
  127.        
  128.         private int addOperation(int x, int y, int[][] operation, final char keyChar, int idx){
  129.                 if(!isOutOfRange(x, y)){
  130.                         if(keyChar == charMatrix[x][y]){
  131.                                 idx ++;       
  132.                                 operation[idx][0] = x;
  133.                                 operation[idx][1] = y;
  134.                         }
  135.                 }
  136.                 return idx;
  137.         }
  138.        
  139.         private boolean isOutOfRange(int x, int y){
  140.                
  141.                 return (x > x_max-1 || x < x_min || y > y_max-1 || y< y_min);
  142.         }
  143.        
  144.        
  145.         private static int[][] trimIntArray(int[][] arr, int lastIdx){
  146.                 int column = arr[0].length;
  147.                 int[][] res = new int[lastIdx + 1][column];
  148.                
  149.                 for(int i =0; i<lastIdx +1; i++){
  150.                         for(int j = 0; j < column; j ++)
  151.                                 res[i][j] = arr[i][j];
  152.                 }
  153.                 //printArray(res);
  154.                 return res;
  155.         }
  156.        
  157.        
  158.         private static void printArray(char[][] args){
  159.                 System.out.println("===============================================");
  160.                 for(int i=0;i<args.length;i++){
  161.                         System.out.print("{");
  162.                         for(int j=0;j< args[i].length;j++){
  163.                                 System.out.print(args[i][j] + ((j == args[i].length - 1)? "":","));
  164.                         }
  165.                         System.out.print("}");
  166.                         System.out.println();
  167.                 }
  168.                 System.out.println("===============================================");
  169.         }
  170.        
  171.         private static void printArray(int[][] args){
  172.                 System.out.println("===============================================");
  173.                 for(int i=0;i<args.length;i++){
  174.                         System.out.print("{");
  175.                         for(int j=0;j< args[i].length;j++){
  176.                                 System.out.print(args[i][j] + ((j == args[i].length - 1)? "":","));
  177.                         }
  178.                         System.out.print("}");
  179.                         System.out.println();
  180.                 }
  181.                 System.out.println("===============================================");
  182.         }
  183.        
  184.        
  185.         public static void main(String[] arg){
  186.                 String find = "ABABABBA";
  187.                 String[] val = {"ABABA", "BABAB", "ABABA", "BABAB", "ABABA"};
  188.                
  189.                 System.out.println("Keys to be Found");
  190.                 System.out.println(find);
  191.                 new WordPath().countPaths(val, find);
  192.         }
  193.        
  194.         public static void usage(){
  195.                 System.out.println("java WordPath strRow1, strRow2, .... strRowN, stringToBeFound");
  196.         }
  197.        
  198.         public static void validateArguments(String[] args){
  199.                 if(args == null || args.length <= 1){
  200.                         usage();
  201.                         System.exit(0);
  202.                 }
  203.                
  204.                 try{
  205.                         Integer.parseInt(args[args.length-1]);
  206.                 }catch(Exception e){
  207.                         usage();
  208.                         System.exit(0);
  209.                 }
  210.                
  211.                 int len = args[0].length();
  212.                 for(int i=1; i<args.length -1; i ++){
  213.                         if(len != args[i].length()){
  214.                                 System.out.println("Not all city map row are equal in length");
  215.                                 System.exit(0);
  216.                         }
  217.                 }
  218.         }

  219. }

復(fù)制代碼

論壇徽章:
0
3 [報告]
發(fā)表于 2005-12-20 12:33 |只看該作者
我可以說剛開始一兩天就去的,做完三題后發(fā)現(xiàn)自己超時了就沒再去了

老實說我只是去鍛煉英語而已
您需要登錄后才可以回帖 登錄 | 注冊

本版積分規(guī)則 發(fā)表回復(fù)

  

北京盛拓優(yōu)訊信息技術(shù)有限公司. 版權(quán)所有 京ICP備16024965號-6 北京市公安局海淀分局網(wǎng)監(jiān)中心備案編號:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年舉報專區(qū)
中國互聯(lián)網(wǎng)協(xié)會會員  聯(lián)系我們:huangweiwei@itpub.net
感謝所有關(guān)心和支持過ChinaUnix的朋友們 轉(zhuǎn)載本站內(nèi)容請注明原作者名及出處

清除 Cookies - ChinaUnix - Archiver - WAP - TOP