C♯の勉強

C♯4.0 で TopCoder の過去問を解きます。

TopCoder SRM 590: FoxAndGomoku

問題文

全探索

public class FoxAndGomoku {
    static public bool Between(long a, long x, long b) { return a <= x && x < b; }

    static public int[] dx = { 0, 1, 1, -1};
    static public int[] dy = { 1, 0, 1, 1 };

    public string win(string[] board) {
        int n = board.Length;
        
        for (int y = 0; y < n; y++) {
            for (int x = 0; x < n; x++) {
                for (int k = 0; k < 4; k++) {
                    int token = 0;
                    for (int t = 0; t < 5; t++) {
                        int nx = x + dx[k] * t;
                        int ny = y + dy[k] * t;
                        if (Between(0, nx, n) && Between(0, ny, n) && board[ny][nx] == 'o')
                            token++;
                    }
                    if (token == 5) return "found";
                }
            }
        }
        return "not found";
    }
}