大家都玩过扫雷,扫雷当中的格子分为三种:雷,空白,数字。数字代表了在它周围的格子中雷的个数。
这道题和扫雷差不多,题目是这样的:
在一个M x N 的格子雷区里,格子要么是雷,要么是空白区域。现在有一个游戏,要计算玩这个游戏的成功的概率,规则如下:
.1. 只能在雷区里随机地点一次,如果是雷,游戏就失败。
2. 如果点的格子不是雷,并且在该格子周围不包含一颗雷(该区域属于空白区域),则游戏成功。
3. 如果点的格子不是雷,并且在该格子周围含有一颗以上的雷,则此次点击无效,再次点击直到出现前两种情况之一。
成功的概率 = 空白区域的格子数 / ( 空白区域的格子数 + 雷的个数)
下面是我的思路:
1.遍历雷区,计算出雷的个数。 复杂度 O(N)
2.遍历雷区,遇到雷,刚将雷周围所有的格子打上标记'N'。复杂度也为O(N)
3.遍历雷区,计算空白区域。复杂度也为O(N)
Problem Statement | |||||||||||||
The game of BombSweeper is a single-player game played on a rectangular grid. Each square in the grid is either a bomb (represented by 'B') or empty space (represented by '.'). The true identity of each square is hidden when the game begins. The object of the game is to correctly determine both the number of bombs on the board and their positions. You are trying to write a program which predicts the percent likelihood that you will win a given game of BombSweeper. You've recently become so good at the game that your success or failure depends only on your first few moves. To start, you pick a random square on the gameboard and uncover it. If the square you uncover is a bomb, you lose the game. If the square is not a bomb, but one or more of its horizontal, vertical, and diagonal neighbors is, you are no better off than when you started and you must take another turn. If the square is not a bomb, and none of its (up to) eight neighbors are bombs either, then you win the game. Given a String[] board, representing the game board, return a number between 0.0 and 100.0, inclusive, representing the percent likelihood of you winning the game. | |||||||||||||
Definition | |||||||||||||
| |||||||||||||
Notes | |||||||||||||
- | In calculating the probability, let wins be the number of non-bomb squares which have no bomb neighbors, and let losses be the number of bombs on the board. The odds of winning are then (wins / (wins + losses)), which returns a number between 0.0 and 1.0, inclusive. | ||||||||||||
- | As long as you calculate wins and losses correctly, you need not worry about minor double imprecisions. | ||||||||||||
- | Your solution need only be accurate to 1e-9 (relative or absolute). Thus, if your result is within 1e-9 (relative or absolute) of the result shown, your result will be judged correct. | ||||||||||||
Constraints | |||||||||||||
- | board will contain between 1 and 50 elements, inclusive. | ||||||||||||
- | Each element of board will contain between 1 and 50 characters, inclusive. | ||||||||||||
- | Each element of board will contain the same number of characters. | ||||||||||||
- | Each character in board will be either '.' or 'B'. | ||||||||||||
Examples | |||||||||||||
0) | |||||||||||||
| |||||||||||||
1) | |||||||||||||
| |||||||||||||
2) | |||||||||||||
| |||||||||||||
3) | |||||||||||||
| |||||||||||||
4) | |||||||||||||
|
This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2003, TopCoder, Inc. All rights reserved.
我的代码:
package srms21_40;
public class BombSweeper {
public static char arr[][];
public static int[][] move = { { 0, 1 }, { 1, 1 }, { 1, 0 }, { 1, -1 },
{ 0, -1 }, { -1, -1 }, { -1, 0 }, { -1, 1 } };
public static int sw;
public static int sl;
public static void main(String[] args) {
String arrays[] = { ".....", ".....", "..B..", ".....", "....." };
double f = winPercentage(arrays);
System.out.println(f);
}
public static double winPercentage(String[] board) {
boolean flag;
int w = board.length;
int l = board[0].length();
sw = w;
sl = l;
double numOfBoms = 0;
double numOfEmpty = 0;
arr = new char[w][l];
for (int i = 0; i < w; i++) {
String s = board[i];
int j;
char c;
for (j = 0; j < l; j++) {
c = s.charAt(j);
if (c == 'B')
numOfBoms++;
arr[i][j] = c;
}
}
for (int i = 0; i < w; i++)
for (int j = 0; j < l; j++) {
char c = arr[i][j];
if (c == 'B') {
flagDistrictAroundBomb(i, j);
}
}
for (int i = 0; i < w; i++)
for (int j = 0; j < l; j++) {
char c = arr[i][j];
if (c == 'B' || c == 'N')
continue;
else {
flag = isEmpty(i, j);
if (flag)
numOfEmpty++;
}
}
return numOfEmpty * 100 / (numOfEmpty + numOfBoms);
}
private static void flagDistrictAroundBomb(int x, int y) {
int tempX;
int tempY;
for (int i = 0; i < 8; i++) {
tempX = x;
tempY = y;
tempX += move[i][0];
tempY += move[i][1];
if (tempX >= 0 && tempX < sw && tempY >= 0 && tempY < sl) {
if (arr[tempX][tempY] == '.') {
arr[tempX][tempY] = 'N';
}
}
}
}
private static boolean isEmpty(int x, int y) {
int tempX;
int tempY;
boolean flag = true;
for (int i = 0; i < 8; i++) {
tempX = x;
tempY = y;
tempX += move[i][0];
tempY += move[i][1];
if (tempX >= 0 && tempX < sw && tempY >= 0 && tempY < sl) {
if (arr[tempX][tempY] == 'B') {
return false;
}
}
}
return true;
}
}