<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    Spirit

    2005年12月19日 #

    Google Code Jam - WordPath

    - Implementation
    public class WordPath {    
        
    public int countPaths(String[] grid, String find) {
            
    int length = grid.length + 2;      
            
            
    //convert "grid" from String to char matrix
            char[][] charGrid = new char[length][length];
            
    for (int i = 0; i < length; i++) {
                
    for (int j = 0; j < length; j++) {
                    
    if (i == 0 || i == length - 1 || j == 0 || j == length - 1)
                        charGrid[i][j] 
    = '0';
                    
    else
                        charGrid[i][j] 
    = grid[i - 1].charAt(j - 1);
                }
            }
            
            
    //convert "find" from String to char array
            char[] charFind = new char[find.length()];
            
    for (int k = 0; k < find.length(); k++)
                charFind[k] 
    = find.charAt(k);

            
    //use three dimensions "degree" to hold the in-degree of the vertex of graph
            int[][][] degree = new int[length][length][find.length()];        

            
    // k=0
            for (int i = 0; i < length; i++)
                
    for (int j = 0; j < length; j++) {
                    degree[i][j][
    0= ((charGrid[i][j] == charFind[0]) ? 1 : 0);
                }
            
            
    // fill the degree 
            for (int k = 1; k < find.length(); k++) {
                
    for (int i = 0; i < length; i++)
                    
    for (int j = 0; j < length; j++) {
                        
    if (charGrid[i][j] != charFind[k])
                            degree[i][j][k] 
    = 0;
                        
    else {
                            degree[i][j][k] 
    = degree[i - 1][j - 1][k - 1+ degree[i - 1][j][k - 1]
                                    
    + degree[i - 1][j + 1][k - 1+ degree[i + 1][j - 1][k - 1]
                                    
    + degree[i + 1][j][k - 1+ degree[i + 1][j + 1][k - 1]
                                    
    + degree[i][j - 1][k - 1+ degree[i][j + 1][k - 1];

                        }

                    }
            }
            
            
    //calculate the sum
            int sum = 0;
            
    for (int i = 0; i < length; i++)
                
    for (int j = 0; j < length; j++) {
                    sum 
    += degree[i][j][find.length() - 1];
                    
    if (sum > 1000000000) {
                        
    return -1;
                    }
                }
            
    return sum;
        }
    }

    - TestCase
    public class WordPathTest extends TestCase {
        
    public void testCountPaths() {
            WordPath wordPath 
    = new WordPath();
            assertEquals(
    2, wordPath.countPaths(new String[] { "ABC""FED""GAI" }, "ABCDEA"));
            assertEquals(
    0, wordPath.countPaths(new String[] { "ABC""DEF""GHI" }, "ABCD"));
            assertEquals(
    108, wordPath.countPaths(new String[] { "AA""AA" }, "AAAA"));
            assertEquals(
    56448, wordPath.countPaths(new String[] { "ABABA""BABAB""ABABA""BABAB""ABABA" },
                    
    "ABABABBA"));
            assertEquals(
    -1, wordPath.countPaths(new String[] { "AAAAA""AAAAA""AAAAA""AAAAA""AAAAA" },
                    
    "AAAAAAAAAAA"));
            assertEquals(
    0, wordPath.countPaths(new String[] { "AB""CD" }, "AA"));
        }
    }

    This testcase finished after 0.015 seconds on my pc.

    http://forum.javaeye.com/viewtopic.php?p=106316#106316

    posted @ 2005-12-19 16:36 Spirit 閱讀(349) | 評論 (0)編輯 收藏

    2005年12月15日 #

    Google Code Jam - SkipStones的一種遞歸算法

    public class SkipStones {
        
    private String water = "X";

        
    public int maxDistance(String water) {
            
    this.water = water;
            
    int max = 0;
            
    int sum = 0;
            
    for (int initial = 1; initial < water.length() + 1; initial++) {
                sum 
    = bounce(0, initial);
                max 
    = (sum > max ? sum : max);
            }
            
    return max;
        }

        
    private int bounce(int startDistance, int bounceDistance) {
            
    if (bounceDistance == 0)
                
    return startDistance;

            
    if ((startDistance + bounceDistance) > water.length())
                
    return -1;

            
    if (water.charAt(startDistance + bounceDistance - 1== 'X')
                
    return startDistance;

            
    return bounce(startDistance + bounceDistance, bounceDistance / 2);
        }

        
    public static void main(String[] args) {
            SkipStones skipStones 
    = new SkipStones();
            System.out.println(skipStones.maxDistance(args[
    0]));
        }
    }

    posted @ 2005-12-15 13:29 Spirit 閱讀(357) | 評論 (1)編輯 收藏

    僅列出標(biāo)題  
    主站蜘蛛池模板: 国产一级淫片a免费播放口之| 国产乱子精品免费视观看片| 最近免费中文字幕大全免费版视频 | 亚洲一区无码精品色| 西西人体大胆免费视频| 国产美女被遭强高潮免费网站| 亚洲啪啪AV无码片| 免费人成在线观看视频高潮| 亚洲精品无码av人在线观看| 免费中文字幕视频| 亚洲中文字幕不卡无码| 日韩免费视频一区二区| 亚洲国产成人手机在线电影bd | 午夜电影免费观看| 亚洲小说图区综合在线| 日韩a级毛片免费视频| 一级看片免费视频| 亚洲国产AV无码专区亚洲AV | 久久精品国产亚洲沈樵| 性无码免费一区二区三区在线| 国产gav成人免费播放视频| 欧洲乱码伦视频免费国产| 精品国产_亚洲人成在线高清| 亚洲无线一二三四区| 好爽…又高潮了免费毛片| 一区二区视频免费观看| 国产亚洲福利精品一区| 国产精品爱啪在线线免费观看| 亚洲中久无码永久在线观看同| 亚洲一级视频在线观看| 毛片免费视频观看| 永久免费精品影视网站| 亚洲AV日韩AV鸥美在线观看| 成人激情免费视频| 成年免费a级毛片免费看无码| 日本特黄特色免费大片| 女同免费毛片在线播放| 亚洲中文字幕无码中文字| 国产偷国产偷亚洲高清日韩| 91人人区免费区人人| 亚洲AV成人无码网站|