扫雷(java)

写了一个比较简单的代码

import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner scan =new Scanner(System.in);
		int n=scan.nextInt();
		int m=scan.nextInt();
		//一个二维数组储存输入的点
		int[][] the_y=new int[n][m];
		//一个二维数组储存输出的点,注意这个输出的二维数组比输入的大一圈
		/*目的下面会给出。
		 * 例如:
		 * 输入:5*6
		 * 1 1 1 1 1 1
		 * 1 1 1 1 1 1
		 * 1 1 1 1 1 1
		 * 1 1 1 1 1 1
		 * 1 1 1 1 1 1
		 * 输出:7*8
		 * 0 0 0 0 0 0 0 0
		 * 0 0 0 0 0 0 0 0
		 * 0 0 0 0 0 0 0 0
		 * 0 0 0 0 0 0 0 0
		 * 0 0 0 0 0 0 0 0
		 * 0 0 0 0 0 0 0 0
		 * 0 0 0 0 0 0 0 0
		 */
		int[][] the_l=new int[n+2][m+2];
		//输入填充the_y,并且置the_l全部为零
		for(int i=0;i<n;i++) {
			for(int j=0;j<m;j++) {
				the_y[i][j]=scan.nextInt();
				the_l[i][j]=0;
			}
		}
		//这里会遍历输入数组的所有值,如果是1,则置9,周围全加1,因为the_l比the_y大一圈,所以不用考虑边界。
		for(int i=1;i<n+1;i++) {
			for(int j=1;j<m+1;j++) {
				if(the_y[i-1][j-1]==1) {
					the_l[i][j]=9;
					the_l[i-1][j-1]++;
					the_l[i-1][j]++;
					the_l[i-1][j+1]++;
					the_l[i][j-1]++;
					the_l[i][j+1]++;
					the_l[i+1][j-1]++;
					the_l[i+1][j]++;
					the_l[i+1][j+1]++;
				}
			}
		}
		//打印,注意,这里要将大于9的数置9
		for(int i=1;i<n+1;i++) {
			for(int j=1;j<m+1;j++) {
				if(the_l[i][j]>9)
					the_l[i][j]=9;
				System.out.print(the_l[i][j]+" ");
			}
			System.out.println();
	}

}
}

  • 5
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值