# Minesweeper Numbers

**URL:** <https://forum.kirupa.com/t/minesweeper-numbers/187272>\
**Category:** open source\
**Created:** [April 27, 2006, 11:34pm UTC](https://forum.kirupa.com/t/minesweeper-numbers/187272 "2006-04-27T23:34:11Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jeff\_Wheeler](https://avatars.discourse-cdn.com/v4/letter/j/59ef9b/32.png) [@Jeff\_Wheeler](https://forum.kirupa.com/u/Jeff_Wheeler)\
**Post date:** [April 27, 2006, 11:34pm UTC](https://forum.kirupa.com/t/minesweeper-numbers/187272/1 "2006-04-27T23:34:11Z")

</div>

This is in **JAVA** , by the way…

This problem actually sounds much harder than it is. They gave you the rows and columns of a minesweeper game, and then list the mines. Your job, is to output the table, and show the number of mines touching each place (just like in Microsoft’s version). I made the table have a 1 number border on all sides to avoid out of bounds exceptions (each point inefficiently checks all neighboring points, as that was the _quickest_ approach, and these contests are all about speed).

**[size=5]USAGE[/size]**

The 7 represents the rows, the 5 is the columns, and the 7 represents the number of following mines. Each mine is x, y, starting at top left.

```auto
7 5 7
1 4
5 2
5 3
2 5
3 3
4 4
4 2

```

Here’s what this will output

```auto
001*2
0123*
12*32
2*5*1
2**21
12210
00000

```

**[size=5]SOURCE[/size]**

```auto
import java.io.*;
import java.util.*;

public class pr91 {
	public static void main (String args[]) throws IOException {
		Scanner scanner = new Scanner (new FileReader("pr91.txt"));
		int rows = scanner.nextInt();
		int columns = scanner.nextInt();
		int mines = scanner.nextInt();
		
		int [][] table = new int [rows+2][columns+2]; // leave a border
		
		for (int i=0; i<mines; i++) {
			table[scanner.nextInt()][scanner.nextInt()] = -1;
		}
		
		for (int r=1; r<table.length-1; r++) {
			for (int c=1; c<table[r].length-1; c++) {
				if (table[r][c]>-1) {
					int sum = 0;
					if (table[r-1][c-1]==-1) sum++;
					if (table[r][c-1]==-1) sum++;
					if (table[r+1][c-1]==-1) sum++;
					if (table[r-1][c]==-1) sum++;
					if (table[r+1][c]==-1) sum++;
					if (table[r-1][c+1]==-1) sum++;
					if (table[r][c+1]==-1) sum++;
					if (table[r+1][c+1]==-1) sum++;
					
					table[r][c]=sum;
				}
			}
		}
		
		// Print
		for (int i=1; i<table.length-1; i++) {
			for (int j=1; j<table*.length-1; j++) {
				if (table*[j]==-1)
					System.out.print("*");
				else
					System.out.print(table*[j]);
			}
			
			System.out.println();
		}
	}
}

```

Keep in mind these contests are all about speed.

This uses **Java 1.5**.

Hope you like it. 😃
