2014-06-30

10189-Problem B: Minesweeper




import java.util.*;
import java.lang.*;

public class Main{
 private static char[][] inField ;
 private static int n , m ;
 public static void main(String... args){
 Scanner sc = new Scanner(System.in);
 int fieldNum = 0;
 n = sc.nextInt();
 m = sc.nextInt();
 while(n!=0 && m!=0){
  
  inField = new char[n][m];
  int i , j;
  
  for(i = 0 ; i < n ; i++){
   String temp = sc.next();
   for(j = 0 ; j < m ; j++)
        inField[i][j] = temp.charAt(j);    
    }
  if( fieldNum!=0) 
  System.out.println();
  System.out.printf("Field #%d:%n",++fieldNum);  
     
   for(i = 0 ; i < n ; i++){
     for(j = 0 ; j < m ; j++){
      if(inField[i][j] == '*'){
        System.out.print("*");
        continue;
      }      
      printNum(i,j);
     }

     System.out.println();
   }
   
   n = sc.nextInt();
   m = sc.nextInt();
 } 
 }
 public static void printNum(int i , int j){
  int count = 0;
 if( i-1 >=0 && j-1>=0 ){
   if(inField[i-1][j-1] == '*')
   count++;
  }
  if( i-1>=0 ){
   if(inField[i-1][j]=='*')
   count++;
  }
  if( i-1>=0 && j+1<=m-1 ){
   if(inField[i-1][j+1]=='*')
   count++;
  }
  if( j-1>=0){
  if(inField[i][j-1] == '*')
  count++;          
         } 
  if( j+1<=m-1){
  if(inField[i][j+1] == '*')
  count++;
 }
  if(i+1<=n-1 && j-1>=0 ){
  if(inField[i+1][j-1]=='*' )
  count++;
 }
  if( i+1 <= n-1){
  if(inField[i+1][j] == '*')
  count++;
 }
  if(i+1<=n-1 && j+1<=m-1){
  if(inField[i+1][j+1] == '*')
  count++;
 }
  System.out.print(count);
 }
}

沒有留言:

張貼留言

(VM) Ubuntu enable ssh

OS版本:14.04 LTS 相關指令: sudo apt-get install openssh-server Port forwarding設定 : 以virtual box為例子,網路->進階->連接阜轉送(port forwarding) ...