<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 閱讀(350) | 評論 (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 閱讀(358) | 評論 (1)編輯 收藏

    僅列出標題  
    主站蜘蛛池模板: 热re99久久6国产精品免费| 激情婷婷成人亚洲综合| 在线观看肉片AV网站免费| 中文字幕亚洲激情| 国产JIZZ中国JIZZ免费看| 国产成人亚洲影院在线观看| 特级毛片爽www免费版| 亚洲精品影院久久久久久| 国产高清不卡免费视频| 久久精品国产亚洲AV无码麻豆| 久久久久成人精品免费播放动漫| 日本最新免费不卡二区在线| 亚洲Av永久无码精品黑人 | 国产免费内射又粗又爽密桃视频| 亚洲国产成人精品无码久久久久久综合 | 好紧我太爽了视频免费国产| 香蕉视频在线观看亚洲| 久久国产免费福利永久| 亚洲AV无码无限在线观看不卡| 四色在线精品免费观看| 一级看片免费视频囗交| 午夜色a大片在线观看免费| 国产精品亚洲精品日韩电影| 久久夜色精品国产亚洲av| 久久精品一区二区免费看| 亚洲欧洲日产专区| 永久免费毛片手机版在线看| 人妻仑乱A级毛片免费看| 亚洲国产精品不卡在线电影| 黄色片在线免费观看| 一级特黄a免费大片| 久久亚洲私人国产精品vA | 国产99视频精品免费视频7| 香蕉视频在线免费看| 亚洲视频一区在线| 国产成人无码a区在线观看视频免费| 亚州**色毛片免费观看| 亚洲视频在线播放| 免费成人黄色大片| 久久精品国产免费观看三人同眠| 国产精品亚洲lv粉色|