TopCoder 300 points 25-SRM 156 DIV 1 90.77/300 30.26%

大家都玩过扫雷,扫雷当中的格子分为三种:雷,空白,数字。数字代表了在它周围的格子中雷的个数。

这道题和扫雷差不多,题目是这样的:

在一个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

 
Class:BombSweeper
Method:winPercentage
Parameters:String[]
Returns:double
Method signature:double winPercentage(String[] board)
(be sure your method is public)
 
 

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) 
 
{".....",
 ".....",
 "..B..",
 ".....",
 "....."}
Returns: 94.11764705882354

If you uncover the bomb in the center of the gameboard, you lose. If you uncover one of the eight squares surrounding the bomb, you would need to take another turn. If you uncover one of the 16 squares on the perimeter of the gameboard, you would win, since none of these squares has a neighboring square which is a bomb. Since there are 16 ways to win and only 1 way to die, your odds of winning are 16/17, or about 94.1%.

1) 
 
{"BBBBB",
 "B...B",
 "B...B",
 "B...B",
 "BBBBB"}
Returns: 5.882352941176471

As far as your odds of winning are concerned, this board is the opposite of the previous one. The only way to win is to uncover the square in the exact center of the gameboard, while any of the 16 bombs on the perimeter will cause you to lose. 1/17 is about 0.0588, or about 5.88%.

2) 
 
{".........",
 ".B..B..B.",
 ".........",
 ".........",
 ".B..B..B.",
 ".........",
 ".........",
 ".B..B..B.",
 "........."}
Returns: 0.0

Every square on this board is either a bomb, or has a bomb as a neighbor. Therefore, there is no way to win.

3) 
 
{".........................",
 ".........................",
 ".........................",
 "........................."}
Returns: 100.0

Now there are no bombs, so any square you uncover will cause you to win.

4) 
 
{"......B.......B..B...........................B....",
 "..............B..................BB..B............",
 "B.B.B.............B.....B..............B..........",
 "...................B...B....................BB....",
 "...B.....B.........................B.......B.....B",
 "B.B.........B.....B.......B..B......B.B...B.BB....",
 "..B...................BB...............B..........",
 ".........B...B................B..B................",
 ".......BB.......B....B................B.....BBB...",
 ".......BB..........B..............B......BB.......",
 "...................BB.....................B.......",
 "...B.B.B......B..............B...B......B.........",
 "B................B................................",
 "....B..........B.....B..BB....B...............BB..",
 "..B....B.....B.............B.....B............B...",
 "...................B.B........B..B.........B.B....",
 ".....B.....B......................................",
 "...........BB......BB...B.B........B...B..........",
 ".....BBB..........................................",
 ".B...........B....B...BB......B......B...B.B......",
 "..................B........BB................B....",
 "...............................B..B....BBB.B....B.",
 "..........B.......................................",
 ".....B..........B....BB......B.B......B......B....",
 ".....B..................B........B................",
 "............B.....B..B....BB...B....BB........B...",
 "..B.................B.........B...................",
 ".BB..............B................................",
 "...B....B..................B.................B....",
 "......B...B......B......................B.B.......",
 "..............B..................B.......B........",
 ".....B........BB...B.....B........................",
 ".......B......B.B..B..........B...........B....B..",
 "B...B...........B...B...B......B.B...B..B......B..",
 "....B..B.....B.B.......BB..B............B.B....B..",
 "B.......B..........B.........B...B.BB......B......",
 "....B...............................B.............",
 ".....B.B..........................................",
 "..........B............B......B.B..B....B.........",
 "....B...B.......................B.................",
 "B.................B...........B..B....B...........",
 "...B.....B........................................",
 "...B..B......................................BBB..",
 ".B...B....................................B....B..",
 "...B...B..........B...B.B.........................",
 ".....B.............B...BB..........B..BBB.BB......",
 "....................B.....B.......................",
 "........B..BB..........B.B....B...........B......B",
 ".........B.....BB..B.............B....BB..........",
 "....B..B..............B...B..B..........B........."}
Returns: 77.67558528428094

This board has 267 bombs, 1304 empty spaces with bomb neighbors, and 929 empty spaces without bomb neighbors. (929 / (929 + 267)) is about 0.7768.

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;
	}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值