2012-02-29 110 views
-3

這裏的鏈接的問題:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=13&page=show_problem&problem=1130UVA 10189掃雷

這是我的代碼,它完美的作品;然而,只要我提交它就會給出錯誤的答案。有人知道爲什麼嗎?

注:我用2個額外的行和列填充矩陣,以便當我檢查第一列的左邊或最後一行的底部時,我不會收到錯誤。

public class Main { 
public void convert(char[][] maze, int fieldNum) { 
    if (fieldNum != 1) 
     System.out.println(); 
    System.out.println("Field #" + fieldNum + ":"); 
    int n = maze.length; 
    int m = maze[0].length; 
    char[][] result = new char[n][]; 
    for (int i = 0; i < n; i++) { 
     result[i] = new char[m]; 
    } 
    for (int i = 0; i < n; i++) { 
     for (int j = 0; j < m; j++) { 
      result[i][j] = '0'; 
     } 
    } 
    for (int i = 1; i < n - 1; i++) { 
     for (int j = 1; j < m - 1; j++) { 
      if (maze[i][j] == '*') { 
      this.fill(result, i, j); 
     } 
     } 
    } 
    for (int i = 1; i < n - 1; i++) { 
     for (int j = 1; j < m - 1; j++) { 
      System.out.print(result[i][j]); 
     } 
     System.out.println(); 
    } 
} 
private void fill(char[][] maze, int i, int j) { 
    if (maze[i-1][j-1] != '*') 
     maze[i-1][j-1] += 1; 
    if (maze[i][j-1] != '*') 
     maze[i][j-1] += 1; 
    if (maze[i+1][j-1] != '*') 
     maze[i+1][j-1] += 1; 
    if (maze[i-1][j] != '*') 
     maze[i-1][j] += 1; 
    maze[i][j] = '*'; 
    if (maze[i+1][j] != '*') 
     maze[i+1][j] += 1; 
    if (maze[i-1][j+1] != '*') 
     maze[i-1][j+1] += 1; 
    if (maze[i][j+1] != '*') 
     maze[i][j+1] += 1; 
    if (maze[i+1][j+1] != '*') 
     maze[i+1][j+1] += 1; 
} 
public static void main(String[] args) throws IOException { 
    Main sweeper = new Main(); 
    BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); 
    String line = null; 
    int fieldNum = 1; 
    while ((line = reader.readLine()) != null) { 
     String[] xy = line.trim().split("\\s+"); 
     int n = Integer.parseInt(xy[0]); 
     int m = Integer.parseInt(xy[1]); 
     if (n == 0 && m == 0) 
      break; 
     n += 2; 
     m += 2; 
     char[][] maze = new char[n][]; 
     for (int i = 0; i < n; i++) { 
      maze[i] = new char[m]; 
     } 
     for (int i = 1; i < n - 1; i++) { 
      line = reader.readLine(); 
      for (int j = 1; j < n - 1; j++) { 
       maze[i][j] = line.charAt(j - 1); 
      } 
     } 
     sweeper.convert(maze, fieldNum); 
     fieldNum++; 
    } 
} 

}

+3

如果它給你一個錯誤的答案,那麼它不能正常工作。此外,人們不會爲您調試您的代碼 - 您是否有一個您認爲可以在此得到解答的具體問題? – simchona 2012-02-29 03:49:26

+0

我不知道爲什麼uva給RE,但編程挑戰給了WA。 – 2012-02-29 04:02:26

+0

什麼是RE和WA? – simchona 2012-02-29 04:09:27

回答

2

不是告訴別人調試長碼的,你可以使用this site來生成給定的輸入輸出和UVA的問題,並與您的解決方案進行比較。它支持大約1500個問題。

+0

謝謝。可能是由於格式問題。我會比較。 – 2012-02-29 08:44:32