扫雷问题

 

1.问题描述

[输入]

The input will consist of an arbitrary number of fields. The first line of each field contains two integers
n and m (0 < n, m ≤ 100) which stands for the number of lines and columns of the field respectively.
The next n lines contains exactly m characters and represent the field.
Each safe square is represented by an ‘.’ character (without the quotes) and each mine square
is represented by an ‘*’ character (also without the quotes). The first field line where n = m = 0
represents the end of input and should not be processed.

[输出]

对于每对整数 i 和 j,按原来的顺序输出 i 和 j,然后输出二者之间的整数中的最大循环节长度。
这三个整数应该用单个空格隔开,且在同一行输出。对于读入的每一组数据,在输出中应位于单独的一行。
[样例输入输出]

4 4         Field #1:

*...         *100
....         2210
.*..        1*10
....         1110

3 5         Field #2:
**...        **100
.....         33200
.*...        1*100

0 0

2.我的答案

遇到难题了,就不贴出来了。

3.标准答案

1> 对输入输出要控制好,竞赛是机器判定,做严格比对的,错一个地方都不行,如果思路都有但因细节处理错误而丢分就不划算了。

2> 对二维数组(矩阵)中的一个点轮询其周边的8个点时有双重循环小技巧,注意判断边界。

/*

//轮询上下左右8个点
for (int k = i - 1; k <= i + 1; k++) {
  for (int l = j - 1; l <= j + 1; l++) {
    //注意边界
    if (k >= 0 && k < n && l >= 0 && l < m && data[k][l] == '*') {
      count++;
    }
  }
}
*/
import java.util.Scanner;

public class W2_UVa10189_Minesweeper {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int count = 1;
    while (true) {
      int n = sc.nextInt();
      int m = sc.nextInt();
      sc.nextLine();
      if (n == 0 && m == 0) break;
      char[][] data = new char[n][m];
      for (int i = 0; i < n; i++) {
        data[i] = sc.nextLine().toCharArray();
      }

      if (count > 1) System.out.println();
      System.out.println("Field #" + count + ":");
      deal(data, n, m);
      count++;
    }
  }
  private static void deal(char[][] data, int n, int m) {
    //判断每个点
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        if (data[i][j] == '*') {
          System.out.print("*");
          continue;
        }
        int count = 0;
        //轮询上下左右8个点
        for (int k = i - 1; k <= i + 1; k++) {
          for (int l = j - 1; l <= j + 1; l++) {
            //注意边界
            if (k >= 0 && k < n && l >= 0 && l < m && data[k][l] == '*') {
             count++;
            }
          }
        }
        System.out.print(count);
      }
      System.out.println();
    }
  }
}

 

4.分析解读

        本题中需要学习的是,Scanner中的nextLine(),以及把一行字符串拆解成字符数组,toCharArray()方法,同时在边界的判断上由于使用的是二位数组,所以行列值的考虑条件需要注意。

        同时,我一开始就想复杂了,只需要记录一个二维数组即可,输出是在判断的时候,根据相应的条件输出,而非输出一个二维数组。否则,数组中存储的就是不同的对象,需要各种转换,非常麻烦。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值