我試圖在弗吉尼亞大學(http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=1130)掃雷 - 在線法官 「錯誤的答案」,但它的工作原理上的Netbeans
樣品輸入提交掃雷問題:
4 4
*...
....
.*..
....
輸出示例:
*100
2210
1*10
1110
我開發了NetBeans上的代碼。我已經測試過它,它工作正常,但是當我嘗試在UVa上提交它時,它會導致對該子命令的錯誤答案。
我有兩個問題: 1)什麼是我的代碼的問題? 2)我應該使用什麼,在編碼UVa時不應該使用什麼?
- 如果有不同的標準,我應該遵循,請指教
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main {
public static void main(String[] args) throws IOException{
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String line = reader.readLine();
String REGEX_WHITESPACE = "\\s+";
String cleanLine = line.trim().replaceAll(REGEX_WHITESPACE, " ");
String[] numChar = cleanLine.split(REGEX_WHITESPACE);
int n = new Integer(numChar[0]).intValue();
int m = new Integer(numChar[1]).intValue();
char[][] mine = new char[n][m];
char[] curLine;
for(int i=0;i<n;i++){
line=reader.readLine();
cleanLine = line.trim().replaceAll(REGEX_WHITESPACE, " ");
curLine = cleanLine.toCharArray();
if(curLine.length==m){
mine[i]=curLine;
}
}
int starsCount=0;
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
if(mine[i][j]=='*'){
System.out.print('*');
}
else{
try {
if (mine[i][j - 1] == '*') {
starsCount++;
}
} catch (Exception e) {
}
try {
if (mine[i][j + 1] == '*') {
starsCount++;
}
} catch (Exception e) {
}
try {
if (mine[i-1][j] == '*') {
starsCount++;
}
} catch (Exception e) {
}
try {
if (mine[i+1][j] == '*') {
starsCount++;
}
} catch (Exception e) {
}
try {
if (mine[i-1][j - 1] == '*') {
starsCount++;
}
} catch (Exception e) {
}
try {
if (mine[i-1][j + 1] == '*') {
starsCount++;
}
} catch (Exception e) {
}
try {
if (mine[i+1][j - 1] == '*') {
starsCount++;
}
} catch (Exception e) {
}
try {
if (mine[i+1][j + 1] == '*') {
starsCount++;
}
} catch (Exception e) {
}
if(j==m-1){
System.out.println(starsCount);
}
else{
System.out.print(starsCount);
}
starsCount=0;
}
}
}
}
}
我們對網上法官的瞭解不多。它是否需要在stdout中輸出,還是期待函數調用? – Makoto 2013-03-18 00:29:05
@Makoto它需要提交一個類Main和它的main()包含要運行的代碼。不再。我在此標準之前提交了一個問題 – 2013-03-18 00:30:55
這不是對您的問題的回答,但是您的用於檢查鄰居的代碼真的非常難看。我建議改爲列舉所有可能的方向,每個方向都有一個'xdelta'和一個'ydelta',這個函數在一個提供的方向上檢查一個鄰居,並枚舉調用該方法的所有可能的方向。 – Patashu 2013-03-18 00:31:32