我正在对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中尝试了数十种随机生成的案例,并且它们都奏效了。任何帮助将不胜感激。 谢谢!