java - 我在这个挑战问题上做错了什么?
问题描述
我在 Hackerrank 上做一个挑战,我不知道我做错了什么,我的老师也不知道。每次我提交代码时,测试用例 0(它不会告诉您它测试什么)都会失败。这是挑战:
You are helping search and rescue analyze data from their radar to help locate a boat lost at sea. The problem is due to the water not being flat there are a lot of items that appear on the radar that might be a boat, but are not a boat.
Input Format
The first line of input will provide the number of rows and columns for the radar data. The lines after will provide the radar information.
Sample Input
4 3
w w w
w b b
w w w
w b w
Constraints
The data comes in as a 2D array of characters. Water is labelled with the character 'w' and a possible boat has the label 'b'. Fake boats are easy to identify, because they have another boat next to them. The actual boat will only have water next to it. Everything the radar cannot see (the edges of the 2D array) is assumed to be water. Examples (we don't worry about the diagonals):
w b
w b w boat w b w fake boat
w w
Output Format
Your program will output the row and column where the boat is located (there is at max one boat) or -1, -1 if there is no boat.
Sample Output
3, 1
Sample Input 0
4 3
w w w
w b b
w w w
w b w
Sample Output 0
3, 1
Sample Input 1
3 6
w w w w w w
w b b w w w
w w w w b b
Sample Output 1
-1, -1
这是我的代码:
import java.io.*;
import java.util.*;
public class Solution {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
boolean[][] isBoat = new boolean[sc.nextInt()][sc.nextInt()];
for(int r = 0; r < isBoat.length; r++) {
//System.out.println();
for(int c = 0; c < isBoat[0].length; c++) {
isBoat[r][c] = (sc.next().equals("b") ? true : false);
//System.out.print(isBoat[r][c] + ", ");
}
}
int boatR = -1, boatC = -1;
for(int r = 0; r < isBoat.length; r++) {
//System.out.println();
for(int c = 0; c < isBoat[0].length; c++) {
if(isBoat[r][c]) {
boolean above = false, below = false, left = false, right = false;
if(r != 0 && isBoat[r - 1][c]) {
above = true;
}
if(r != isBoat.length - 1 && isBoat[r + 1][c]) {
below = true;
}
if(c != 0 && isBoat[r][c - 1]) {
left = true;
}
if(c != isBoat[r].length - 1 && isBoat[r][c + 1]) {
right = true;
}
if(!above && !below && !left && !right) {
boatR = r;
boatC = c;
}
//System.out.print((boatR == r && boatC == c) + ", ");
}
}
}
System.out.println(boatR + ", " + boatC);
}
}
我在 Eclipse 中尝试了几十个随机生成的案例,它们都奏效了。任何帮助将不胜感激。谢谢!
解决方案
推荐阅读
- javascript - 在 React.createElement 中将 `onSubmit` 作为道具传递
- python - 删除熊猫数据框中每一行的最后一个单词
- python - 框架浮雕
- python-3.x - 如何解决错误静态文件找不到django
- ios - 如何在 UI 表格视图后面放置黑色不透明背景?
- python - 使用python在嵌套列表中的字符之前添加字符串
- javascript - 将按钮添加到自动幻灯片
- excel - 使用 vba 从 XML 文件中提取数据到 MS excel
- asp.net - 为什么在 Asp.Net Core 中使用 Steeltoe.Discovery.Client 库会不断增加内存?
- wpf - 从 Winform 过渡到 WPF