解题思路:
因为,测试数据有的雷块多,有的雷块少;这里加个判断使得函数调用最少:雷块少,调用用轰炸法,若安全块少,调用用排雷法;
参考代码:
#include <stdio.h> void Minesweeper( int x, int y );//轰炸法 void Sweepmine( int x, int y );//排雷法 int X, Y, M, N = 0; int num_mine, num_safe; //分别记录雷快的数目,和安全块的数目 int num = 1; char b[100][100]; /*-------------------------------------------------------------*/ int main() { while ( scanf( "%d%d", &X, &Y ) && !(X == 0 && Y == 0) ) { num_mine = 0; num_safe = 0; for ( int i = 0; i < X; i++ ) { getchar(); /*clear data buffer cache*/ for ( int j = 0; j < Y; j++ ) { scanf( "%c", &b[i][j] ); if ( b[i][j] == '.' ) { b[i][j] = '0'; num_safe++; }else num_mine++; } } N++; /*the token of field;*/ if ( num_mine <= num_safe ) //判断用哪种方法 { for ( int i = 0; i < X; i++ ) for ( int j = 0; j < Y; j++ ) if ( b[i][j] == '*' ) Minesweeper( i - 1, j - 1 ); //printf( "Minesweeper!\n" ); }else { for ( int i = 0; i < X; i++ ) for ( int j = 0; j < Y; j++ ) if ( b[i][j] != '*' ) Sweepmine( i - 1, j - 1 ); //printf( "Sweepmine!\n" ); } printf( "Field #%d:\n", N ); for ( int i = 0; i < X; i++ ) for ( int j = 0; j < Y; j++ ) { printf( "%c", b[i][j] ); if ( (j + 1) % Y == 0 ) printf( "\n" ); } printf( "\n" ); } } /*-------------------------------------------------------------*/ void Minesweeper( int x, int y ) { for ( int i = x; i <= x + 2; i++ ) for ( int j = y; j <= y + 2; j++ ) if ( i >= 0 && i < X && j >= 0 && j < Y && b[i][j] != '*' ) b[i][j] += 1; return; } /*-------------------------------------------------------------*/ void Sweepmine( int x, int y ) { for ( int i = x; i <= x + 2; i++ ) for ( int j = y; j <= y + 2; j++ ) if ( i >= 0 && i < X && j >= 0 && j < Y && b[i][j] == '*' ) b[x + 1][y + 1] += 1; return; }
别忘点赞哦-.-
0.0分
40 人评分
C语言训练-阶乘和数* (C语言代码)浏览:1060 |
C语言程序设计教程(第三版)课后习题9.6 (C语言代码)浏览:596 |
A+B for Input-Output Practice (VI) (C++代码)浏览:445 |
【绝对值排序】 (C++代码)浏览:720 |
C语言程序设计教程(第三版)课后习题5.4 (C语言代码)浏览:941 |
Pascal三角 (C语言代码)浏览:1252 |
C语言训练-求函数值 (C语言代码)浏览:600 |
C语言程序设计教程(第三版)课后习题3.7 (C语言代码)浏览:467 |
完数 (C语言代码)浏览:757 |
最小公倍数 (C语言代码)浏览:1107 |