#UVa:10189-Minesweeper

利用二維陣列來做計算。

C++(6ms, 762KB)

/*******************************************************/
/* UVa 10189 Minesweeper                               */
/* Author: LanyiKnight [at] knightzone.studio             */
/* Version: 2012/03/02                                 */
/*******************************************************/
#include<iostream>
#include<cstdio>
#define MAX_N 100
#define MAX_M 100
#define MINE -100 // can't be added to positive number
using namespace std;
int main(){
  int n, m, field_num = 0;
  char square;
  while( scanf( "%d%d", &n, &m ) != EOF
         && !(n == 0 && m == 0) ){
    getchar(); //ignore EOL
    if( field_num ) printf( "\n" );
    int field[MAX_N+5][MAX_M+5] = {0};
    for( int i = 1 ; i <= n ; i++ ){
      for( int j = 1 ; j <= m ; j++ ){
        square = getchar();
        if( square == '*' ){
          field[i][j] = MINE;
          for( int k = -1 ; k <= 1 ; k++ )
            for( int l = -1 ; l <= 1 ;l++ )
              field[i+k][j+l]++;
        }
      }
      getchar(); //ignore EOL
    }
    printf( "Field #%d:\n", ++field_num );
    for( int i = 1 ; i <= n ; i++ ){
      for( int j = 1 ; j <= m ; j++ )
        if( field[i][j] < 0 ) printf( "*" );
        else printf( "%d", field[i][j] );
      printf( "\n" );
    }
  }
  return 0;
}
廣告

Comment

There is no comment on this post. Be the first one.

發表迴響

這個網站採用 Akismet 服務減少垃圾留言。進一步瞭解 Akismet 如何處理網站訪客的留言資料

%d 位部落客按了讚: