给定位置判断雷数量,该位置,周围有三种情况、
1,分析所有情况
第一种,情况数据量在,黑色区域内,周围判断八个格子的数据进行累加,
![](https://i-blog.csdnimg.cn/blog_migrate/9ac737384bf02083a5efdaa9f93ea234.png)
第二种情况,坐标在下图四个点中,只有每个点周围只有三个点需要计算
![](https://i-blog.csdnimg.cn/blog_migrate/98280f3555b06f6e107243d8b13b404c.png)
第三种情况坐标在四个黑色区域内,需要判断的格数是五个
![](https://i-blog.csdnimg.cn/blog_migrate/30f72ed1469e14fae9d119903107e30d.png)
2,分析做法
我们发现第二种第三种情况进行八个方向判断的时候,越标了,那么我们只需要给第二种第三种情况加一个限定条件:横坐标在0——14内,(因为数组是15*15下标就是从0——14),纵坐标相同,只需要判断这个条件内的坐标,然后在进行加减,那么这三种情况就都OK了。
3,代码实现
![](https://i-blog.csdnimg.cn/blog_migrate/6ac05c474bdf0707ac4c92c0e3e94211.png)
package 最后18天;
import java.util.Scanner;
public class 扫雷初级版 {
static int y[]= {1,1,1,0,-1,-1,-1,0};
static int x[]= {-1,0,1,1,1,0,-1,-1};
public static void main(String[] args) {
int zu[][]= {{0,1,0,1,1,0,0,1,0,0,0,1,0,0,1},
{0,0,0,0,0,0,0,0,0,0,0,1,0,0,0},
{1,0,1,0,0,0,1,0,0,1,0,0,0,1,0},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{0,0,0,1,0,0,0,0,0,0,1,0,0,0,0},
{0,1,0,0,0,0,1,0,0,0,0,0,1,0,0},
{0,0,0,1,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,1,0,0,0,0,0,1},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{1,0,0,0,1,0,0,0,1,0,0,1,0,0,0},
{0,0,1,0,0,0,0,0,0,0,0,0,0,0,0},
{1,0,0,0,0,0,1,0,0,1,0,0,0,1,0},
{1,0,0,1,0,0,0,0,0,0,0,1,0,0,0},
{1,0,0,0,0,0,0,1,0,0,0,0,0,0,1},
{0,1,1,0,0,1,0,0,0,0,0,0,1,0,0}};
Scanner sc = new Scanner(System.in);
int heng = sc.nextInt();
int zong = sc.nextInt();
int num=0;
if (zu[heng][zong]==1) {
System.out.println("游戏结束");
return;
}
for (int i = 0; i <8; i++) {
int h=heng+y[i];
int z=zong+x[i];
if (h>=0&&z>=0&&h<=14&&z<=14) {
if (zu[h][z]==1) {
num++;
}
}
}
System.out.println(num);
}
}
三种情况都的位置都测试一遍
![](https://i-blog.csdnimg.cn/blog_migrate/3c6d55ad26e561ec6f01b7858e0f44d4.png)
![](https://i-blog.csdnimg.cn/blog_migrate/dabdfb9337ef1676eb87fed88ac584d9.png)
![](https://i-blog.csdnimg.cn/blog_migrate/a223d5b28a8fdc3304a8d3922b512bfe.png)