SRM590 Div2

ゲームプログラミングに弱い...orz

250

問題
http://community.topcoder.com/stat?c=problem_statement&pm=12744&rd=15702

public class FoxAndGomoku {
    public String win(String[] b) {
        String ok = "not found";
        String bad = "found";

        int w = b[0].length();
        int h = b.length;

        // check column
        for (int i=0; i < h; i++) {
            for (int j=0; j<=w-5; j++) {
                if(b[i].charAt(j+0) == 'o' && 
                    b[i].charAt(j+1) == 'o' &&
                    b[i].charAt(j+2) == 'o' &&
                    b[i].charAt(j+3) == 'o' &&
                    b[i].charAt(j+4) == 'o') 
                {
                    return bad;
                }
            }
        }

        // check row
        for (int i=0; i <= h-5; i++) {
            for (int j=0; j < w; j++) {
                if(b[i+0].charAt(j) == 'o' &&
                    b[i+1].charAt(j) == 'o' &&
                    b[i+2].charAt(j) == 'o' &&
                    b[i+3].charAt(j) == 'o' &&
                    b[i+4].charAt(j) == 'o') 
                {
                    return bad;
                }
            }
        }

        // check L verical
        for (int i=0; i < h-5; i++) {
            for (int j=w-1; j >= w-5; j--) {
                if(b[i+0].charAt(j-0) == 'o' &&
                    b[i+1].charAt(j-1) == 'o' &&
                    b[i+2].charAt(j-2) == 'o' &&
                    b[i+3].charAt(j-3) == 'o' &&
                    b[i+4].charAt(j-4) == 'o')
                {
                    return bad;
                }
            }
        }

        // check R verical
        for (int i=0; i <= h-5; i++) {
            for (int j=0; j <= w-5; j++) {
                if(b[i+0].charAt(j+0) == 'o' &&
                    b[i+1].charAt(j+1) == 'o' &&
                    b[i+2].charAt(j+2) == 'o' &&
                    b[i+3].charAt(j+3) == 'o' &&
                    b[i+4].charAt(j+4) == 'o')
                {
                    return bad;
                }
            }
        }

        return ok;

    }

  // BEGIN CUT HERE
    public static void main(String[] args) {
        if (args.length == 0) {
            FoxAndGomokuHarness.run_test(-1);
        } else {
            for (int i=0; i<args.length; ++i)
                FoxAndGomokuHarness.run_test(Integer.valueOf(args[i]));
        }
    }
    // END CUT HERE
}
500

問題
http://community.topcoder.com/stat?c=problem_statement&pm=12743&rd=15702

import java.util.*;
import java.math.*;

public class FoxAndGo {

    private static String[] board;

    public static int maxKill(String[] b) {

        int ret = 0;
        int h = b.length;
        int w = b[0].length();

        for ( int i=0; i<h; i++) {
            for( int j=0; j<w; j++) {

                if(b[i].charAt(j) != '.')
                    continue;

                FoxAndGo.board = Arrays.copyOf(b, b.length);
                FoxAndGo.board[i] = FoxAndGo.board[i].substring(0, j) 
                    + 'x' 
                    + FoxAndGo.board[i].substring(j+1);

                int max = 0;
                for(int x=0; x<h; x++) {
                    for(int y=0; y<w; y++) {
                        if(FoxAndGo.board[x].charAt(y) == 'o'
                                && isDead(x, y)) {
                            max += getDeadGroup(x, y);
                                }
                    }
                }

                ret = Math.max(ret, max);
            }
        }
        return ret;
    }

    public static boolean isDead(int x, int y) {
        if( x < 0 || y < 0 || 
            x >= board.length || y >= board[0].length())
            return true;

        if(board[x].charAt(y) == 'x' || board[x].charAt(y) == '-')
            return true;
        else if(board[x].charAt(y) == '.')
            return false;


        if (board[x].charAt(y) == 'o')
            board[x] = board[x].substring(0, y) + '-' + board[x].substring(y+1);

        if(!isDead(x, y+1))
            return false;
        if(!isDead(x+1, y))
            return false;
        if(!isDead(x, y-1))
            return false;
        if(!isDead(x-1, y))
            return false;

        return true;
    }

    public static int getDeadGroup(int x, int y) {
        int N = 0;
        if(x < 0 || y < 0 ||
            x >= board.length || y >= board[0].length())
            return N;

        if(board[x].charAt(y) != '-')
            return N;

        board[x] = board[x].substring(0, y) + '*' + board[x].substring(y+1);
        N = 1;
        return N + getDeadGroup(x+1, y) 
                 + getDeadGroup(x, y+1)
                 + getDeadGroup(x-1, y)
                 + getDeadGroup(x, y-1);
    }
   // BEGIN CUT HERE
    public static void main(String[] args) {
        if (args.length == 0) {
            FoxAndGoHarness.run_test(-1);
        } else {
            for (int i=0; i<args.length; ++i)
                FoxAndGoHarness.run_test(Integer.valueOf(args[i]));
        }
    }
    // END CUT HERE
}

© karahiyo