Problem Statement
|
|
There are several cannons located on a plane. Each cannon is represented as a point, and can shoot in one of four directions: left, right, up or down. Given the coordinates of the cannons, assign a direction to each one such that no cannon shoots another cannon. Cannon A shoots cannon B if and only if B is located on A's line of fire. For example, if cannon A is located at (5,7) and cannon B is located at (9,7), cannon A cannot shoot to the right and cannon B cannot shoot to the left.
You are given the coordinates of the cannons in the int[]s x and y. The ith element of x is the x-coordinate of the ith cannon, and the ith element of y is the y-coordinate of the ith cannon. Return a String where the ith character is the direction assigned to the ith cannon. 'L' represents left, 'R' represents right, 'D' represents down, and 'U' represents up. If there is more than one valid return string, return the one that comes first alphabetically. If there is no possible arrangement, return the empty string.
|
Definition
|
|
Class: |
Cannons |
Method: |
getDirections |
Parameters: |
int[], int[] |
Returns: |
String |
Method signature: |
String getDirections(int[] x, int[] y) |
(be sure your method is public) |
|
|
|
Notes
|
- |
Cannon A is located to the left(right) of the cannon B if and only if the y coordinates of A and B are equal and the x coordinate of A is less(greater) than the x coordinate of B. |
- |
Cannon A is located to the down(up) of the cannon B if and only if the x coordinates of A and B are equal and the y coordinate of A is less(greater) than the y coordinate of B. |
Constraints
|
- |
x will contain between 1 and 50 elements, inclusive. |
- |
y will contain between 1 and 50 elements, inclusive. |
- |
x and y will contain the same number of elements. |
- |
Each element of x and y will be between -1000000 and 1000000, inclusive. |
- |
No two cannons will be located in the same point. |
Examples
|
0) |
|
|
|
Returns: "DDLD"
|
All cannons except for the third can shoot down. The third cannon can not shoot down, but can shoot right. |
|
|
1) |
|
|
|
Returns: "D"
|
There is only one cannon here, so it can shoot in any direction. 'D' is chosen because it comes first alphabetically. |
|
|
2) |
|
|
{-3,0,9,0,0}
|
{0,0,0,7,-4}
|
|
Returns: ""
|
The second cannon is enclosed by the other cannons and cannot shoot in any of the four directions, so the empty string must be returned. |
|
|
3) |
|
|
{0,-4,0,5,0,-4}
|
{0,0,-7,3,3,3}
|
|
Returns: "RDDDUL"
|
|
|
4) |
|
|
{-761813,348943,749420,-444982,993810,-548742,-444982,-761813,993810, 363833,728226,28752,461997,544181,776052,-761813,776052,993810,789745, 75502,348943,449038,158889,-548742,-659628,75502,363833,917729,993810, 158889,776052,728226,367390,-689680,917729,748629,367390,917729,859379, 728226}
|
{393728,835962,-593971,-593971,-36149,-922640,376562,-368880,-368880, -212107,-368880,-595655,-786227,393728,-463260,763742,790220,63191, -212107,-786227,-805377,-912205,-368880,883897,-912205,803745,63191, 803745,790220,-212107,-593971,939489,-912205,939489,-291544,790220, 795389,835962,795389,763742}
|
|
Returns: "LLDDLDLDDDDDDDLLURDDDDDLDLLRRLDRDDDDLRDR"
|
|
|
public class Cannons {
public String getDirections(int[] x, int[] y) {
StringBuilder buf = new StringBuilder();
for (int i = 0; i < x.length; i++) {
boolean d = true, l = true, r = true, u = true;
for (int j = 0; j < x.length; j++) {
if (i == j)
continue;
if (x[i] == x[j] && y[j] < y[i])
d = false;
if (y[i] == y[j] && x[j] < x[i])
l = false;
if (y[i] == y[j] && x[j] > x[i])
r = false;
if (x[i] == x[j] && y[j] > y[i])
u = false;
}
if (d)
buf.append('D');
else if (l)
buf.append('L');
else if (r)
buf.append('R');
else if (u)
buf.append('U');
else
return "";
}
return buf.toString();
}
}
分享到:
相关推荐
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`函数输出计算结果,包括基站数量、链路接...
BER通常以百分比形式表示,例如1×10^-6表示每百万个比特中有1个比特出错。 #### 支持的测试环境与信道 CMU300支持多种测试环境与信道,包括但不限于: - **上行链路(UL)与下行链路(DL)信号**:包括信道编号...
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则专注于网页...