Problem Statement
|
|
You are playing a game with some friends. The game is played on a rectangular chess board where each cell may be either empty or occupied by a rook. You are given a String[] board representing the layout of the board. Each character of each element of board represents a single cell of the board. A '.' represents an empty cell, while a 'X' represents an occupied cell.
The goal of the game is to take turns placing rooks onto empty spaces of the board so that they are not within the line of sight of any other rook. The line of sight extends directly up, down, left, and right.
Your strategy is to place your next rook on the board so that the number of "safe" squares remaining on the board (those that are not within the line of sight of any existing rook) is minimized, while also making sure that your piece is not within the line of sight of any rook already on the board. Return the number of safe squares remaining after you make the best possible move. If it is impossible for you to make a move on a safe square, return -1.
|
Definition
|
|
Class: |
LineOfSight |
Method: |
bestPosition |
Parameters: |
String[] |
Returns: |
int |
Method signature: |
int bestPosition(String[] board) |
(be sure your method is public) |
|
|
|
Notes
|
- |
The placement of rooks already on the board may already include some that are within the line of sight of each other. |
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 have the same number of characters. |
- |
Each character of each element of board will be either '.' or uppercase 'X'. |
Examples
|
0) |
|
|
{".....",
".....",
".....",
".....",
"....."}
|
|
Returns: 16
|
Picking the center square is one of the best options here. The resulting board looks like this (with the 'o' representing an unsafe cell):
..o..
..o..
ooXoo
..o..
..o..
(In this case, there is more than one way to get the minimum number of safe cells.) |
|
|
1) |
|
|
|
Returns: 1
|
First, we need to consider which squares are safe. Consider the line of sight for the unit already there:
Xoo
o..
o..
Now, notice that any safe square can see the two of the others, so there will be exactly one safe square after we make our move. |
|
|
2) |
|
|
|
Returns: 1
|
On such a small board, the middle is a good location. The corners are the only safe moves. By the symmetry of the board, we know that each is equally good. |
|
|
3) |
|
|
{"...X...",
".X.....",
".......",
".......",
"......."}
|
|
Returns: 8
|
We need to consider multiple units that have already been placed. |
|
|
4) |
|
|
{"....X",
".....",
".....",
".....",
"X...."}
|
|
Returns: 4
|
|
|
5) |
|
|
|
Returns: -1
|
These three rooks have the entire board within their site, so there is no move that can be made. |
|
|
public class LineOfSight {
int m, n;
int[][] matrix;
public int bestPosition(String[] board) {
m = board.length;
n = board[0].length();
matrix = new int[m][n];
for (int i = 0; i < m; i++)
for (int j = 0; j < n; j++)
if (board[i].charAt(j) == 'X')
setCross(i, j);
int ret = 0, max = 0;
for (int i = 0; i < m; i++)
for (int j = 0; j < n; j++)
if (matrix[i][j] == 0) {
ret++;
max = Math.max(max, zerosToSee(i, j));
}
return ret - max - 1;
}
private void setCross(int x, int y) {
for (int i = 0; i < n; i++)
matrix[x][i] = 1;
for (int i = 0; i < m; i++)
matrix[i][y] = 1;
}
private int zerosToSee(int x, int y) {
int sum = 0;
for (int i = 0; i < n; i++)
if (matrix[x][i] == 0)
sum++;
for (int i = 0; i < m; i++)
if (matrix[i][y] == 0)
sum++;
return sum - 2;
}
}
分享到:
相关推荐
TCHS-SRM-1 SRM - 算法单轮比赛 2. USACO - C++11 礼物1.cpp 骑车.cpp 测试.cpp 3.乌拉尔 - - C++11,Java 1.8 乌拉尔在线法官的可能解决方案 反向Root.cpp 总和文件 求和程序 最终排名.cpp 磁暴.cpp 磁暴.java 寂寞...
- 计算`PDCHS_zhuan`和`TCHS_zhuan`(专用数据信道和语音信道转换后的数量)。 - 计算`DSP_pdch`和`DSP_tch`(所需的数字信号处理器数量)。 #### 输出结果 - 使用`printf`函数输出计算结果,包括基站数量、链路接...
电路交换业务信道(TCHs)测试是BER测试的重要组成部分,主要用于评估语音和低速数据服务的质量。这一部分将详细介绍如何设置测试环境并执行测试。 #### 分组数据业务信道(PDTCHs)测试 随着数据传输速度的提高,...
TCHs(时隙分配)、TRXSIG(发射信号)和OMUSIG(操作维护信道)是需要配置的。 5. **电源模块**:DE34基站的公共设备直流电源由CSUA模块提供。 6. **GSM多址方式**:GSM系统采用FDMA(频分多址)和TDMA(时分多址...
4. 配置ULTRASITE传输时,不需要配置EDAP(可能是指电子数据接入点),而需要配置TCHs(时隙信道)、TRXSIG(传输信号)和OMUSIG(操作维护信号)。 5. DE34基站的公共设备直流电源由CSUA模块提供,而非PWSB、PSUA...
要告诉技术人员验证元素,请将“tchs”属性添加到元素。 <input type="text" tchs=""></input> 技术人员利用规则来验证元素。 验证是在每个元素的基础上完成的,并且根据所使用的元素进行不同的工作...
此外,TopCoder竞赛提供了丰富的奖金和机会,如TopCoder Open(TCO)、TopCoder Collegiate Challenge(TCCC)和TopCoder High School(TCHS)等,涵盖算法、设计、开发和组装等领域。TopCoder Studio则专注于网页...