Skip to content

Instantly share code, notes, and snippets.

@them0nk
Created April 30, 2012 01:45
Show Gist options
  • Save them0nk/2554793 to your computer and use it in GitHub Desktop.
Save them0nk/2554793 to your computer and use it in GitHub Desktop.
Minesweeper: PC/UVa IDs: 110102/10189
/*
* Main.java
* java program model for www.programming-challenges.com
*/
import java.io.*;
import java.util.*;
class minesweeper implements Runnable{
static String ReadLn(int maxLength){ // utility function to read from stdin,
// Provided by Programming-challenges, edit for style only
byte line[] = new byte [maxLength];
int length = 0;
int input = -1;
try{
while (length < maxLength){//Read untill maxlength
input = System.in.read();
if ((input < 0) || (input == '\n')) break; //or untill end of line ninput
line [length++] += input;
}
if ((input < 0) && (length == 0)) return null; // eof
return new String(line, 0, length);
}catch (IOException e){
return null;
}
}
public static void main(String args[]) // entry point from OS
{
minesweeper myWork = new minesweeper(); // Construct the bootloader
myWork.run(); // execute
}
public void run() {
new Q102().run();
}
}
class Q102 implements Runnable{
public void sweepmines(int row, int col, int arr[][])
{
char res;
for(int i=1; i<=row; i++)
{
for(int j=1; j<=col; j++)
{
if (arr[i][j] == 1)
{
res = '*';
}
else
{
res = Integer.toString(arr[i-1][j] + arr[i-1][j-1] + arr[i-1][j+1] + arr[i][j-1] + arr[i][j+1]+arr[i+1][j-1]+arr[i+1][j]+arr[i+1][j+1]).charAt(0);
}
System.out.print(res);
}
System.out.println();
}
}
public void run(){
try
{
int num = 1;
BufferedReader is = new BufferedReader(new InputStreamReader(System.in));
String inputLine;
while ((inputLine = is.readLine( )) != null) {
String a[] = inputLine.trim().split("\\s+");
int n = Integer.parseInt(a[0]);
int m = Integer.parseInt(a[1]);
int arr[][] = new int[n+2][m+2];
if ((n==0) && (m==0))
{
break;
}
for(int i=0; i<n+2;i++)
{
for(int j=0; j<m+2;j++)
{
arr[i][j] = 0;
}
}
for (int i =1; i <= n; i++)
{
String row = is.readLine();
row = row.trim();
for(int j=1;j<= m;j++)
{
if (row.charAt(j-1) == '*')
arr[i][j] = 1;
}
}
if (num == 1)
System.out.println("Field #"+num++ + ":");
else
System.out.println("\nField #"+num++ + ":");
sweepmines(n, m, arr);
}
is.close( );
} catch (Exception e) {
System.out.println(e);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment