TopCoder SRM 156 DIV 1 Easy 練習
問題
マインスイーパ的ゲームで、最初の一個が爆弾なら負け、最初の一個が爆弾でなくその周囲にも爆弾が存在しないなら勝ちとする。ゲームフィールドが与えられるので勝率を求めなさい。
解
やるだけ。これは解き慣れるしかないぜ。
using System; using System.Text; using System.Text.RegularExpressions; using System.Collections; using System.Collections.Generic; public class BombSweeper { public double winPercentage(string[] board) { bool[,] gb = new bool[board.Length + 2, board[0].Length + 2]; for (int i = 0; i < board.Length + 2; ++i) gb[i, 0] = gb[i, board[0].Length + 1] = false; for (int i = 0; i < board[0].Length + 2; ++i) gb[0, i] = gb[board.Length + 1, i] = false; for (int y = 1; y < board.Length + 1; ++y) for (int x = 1; x < board[0].Length + 1; ++x) gb[y, x] = board[y - 1][x - 1] == 'B'; int wins = 0, loses = 0; for (int y = 1; y < board.Length + 1; ++y) for (int x = 1; x < board[0].Length + 1; ++x) if (gb[y, x]) loses++; else { bool ng = false; for (int i = -1; i <= 1; ++i) for (int j = -1; j <= 1; ++j) ng |= gb[y + i, x + j]; if (!ng) wins++; } return 100.0 * wins / (wins + loses); } }