UVA 10189 Java

  1. import java.util.*;
  2. import static java.lang.System.*;
  3.  
  4. public class main{
  5. public static void main(String[] args) {
  6. Scanner sc=new Scanner(System.in);
  7. int t = 0, h = 0, w =0;
  8. while(sc.hasNextInt())
  9. {
  10. h = sc.nextInt();
  11. w = sc.nextInt();
  12. if(h == 0 || w == 0)
  13. break;
  14. if(t != 0)
  15. System.out.println();
  16. System.out.printf("Field #%d:\r\n", ++t);
  17. char[][] map = new char[h][w];
  18. for(int i = 0; i < h; i++)
  19. {
  20. String str = sc.next();
  21. for(int j = 0; j < w; j++)
  22. map[i][j] = str.charAt(j);
  23. }
  24. for(int i = 0; i < h; i++)
  25. {
  26. for(int j = 0; j < w; j++)
  27. {
  28. int count = 0;
  29. if(map[i][j] == '*')
  30. System.out.print("*");
  31. else
  32. {
  33. if( i - 1 >= 0 && map[i - 1][j] == '*')
  34. count++;
  35. if( i + 1 < h && map[i + 1][j] == '*')
  36. count++;
  37. if( j - 1 >= 0 && map[i][j - 1] == '*')
  38. count++;
  39. if( j + 1 < w && map[i][j + 1] == '*')
  40. count++;
  41. if( i - 1 >= 0 && j - 1 >= 0 && map[i - 1][j - 1] == '*')
  42. count++;
  43. if( i - 1 >= 0 && j + 1 < w && map[i - 1][j + 1] == '*')
  44. count++;
  45. if( i + 1 < h && j - 1 >= 0 && map[i + 1][j - 1] == '*')
  46. count++;
  47. if( i + 1 < h && j + 1 < w && map[i + 1][j + 1] == '*')
  48. count++;
  49. System.out.print(count);
  50. }
  51. }
  52. System.out.println();
  53. }
  54. }
  55. }
  56. };
  57.  

留言

這個網誌中的熱門文章

C# 井字遊戲