Mine Sweeper

FJNU.1579

Description
The game Minesweeper is played on an n by n grid. In this grid are hidden m mines, each at a distinct grid location. The player repeatedly touches grid positions. If a position with a mine is touched, the mine explodes and the player loses. If a positon not containing a mine is touched, an integer between 0 and 8 appears denoting the number of adjacent or diagonally adjacent grid positions that contain a mine. A sequence of moves in a partially played game is illustrated below.

Here, n is 8, m is 10, blank squares represent the integer 0, raised squares represent unplayed positions, and the figures resembling asterisks represent mines. The leftmost image represents the partially played game. From the first image to the second, the player has played two moves, each time choosing a safe grid position. From the second image to the third, the player is not so lucky; he chooses a position with a mine and therefore loses. The player wins if he continues to make safe moves until only m unplayed positions remain; these must necessarily contain the mines.
Your job is to read the information for a partially played game and to print the corresponding board.

Input
The first line of input contains a single postitive integer n <= 10. The next n lines represent the positions of the mines. Each line represents the contents of a row using n characters: a period indicates an unmined positon while an asterisk indicates a mined position. The next n lines are each n characters long: touched positions are denoted by an x, and untouched positions by a period. The sample input corresponds to the middle figure above.

Output
Your output should represent the board, with each position filled in appropriately. Positions that have been touched and do not contain a mine should contain an integer between 0 and 8. If a mine has been touched, all positions with a mine should contain an asterisk. All other positions should contain a period.

Sample Input
8
...**..*
......*.
....*...
........
........
.....*..
...**.*.
.....*..
xxx.....
xxxx....
xxxx....
xxxxx...
xxxxx...
xxxxx...
xxx.....
xxxxx...

Sample Output
001.....
0013....
0001....
00011...
00001...
00123...
001.....
00123...

My Program

#include < iostream >
#define  N 15
using   namespace  std;

int  main()
{
    
int m,n,i,j,flag=0;
    
char c,mine[N][N],out[N][N];
    
int sweep[N][N];
    cin
>>n;
    
for(i=0;i<n;i++)
        
for(j=0;j<n;j++)
            cin
>>mine[i][j];
    
for(i=0;i<n;i++)
        
for(j=0;j<n;j++)
        
{
            cin
>>c;
            
if(c=='x')
                sweep[i][j]
=1;
            
else
                sweep[i][j]
=0;
        }

    
for(i=0;i<n;i++)
    
{
        
for(j=0;j<n;j++)
            
if(sweep[i][j]==1)
            
{
                m
=0;
                
if(mine[i][j]=='*')
                    flag
=1;
                
if(i>0&&j>0&&mine[i-1][j-1]=='*')
                    m
++;
                
if(i>0&&mine[i-1][j]=='*')
                    m
++;
                
if(i>0&&j<n&&mine[i-1][j+1]=='*')
                    m
++;
                
if(i<n&&j>0&&mine[i+1][j-1]=='*')
                    m
++;
                
if(i<n&&mine[i+1][j]=='*')
                    m
++;
                
if(i<n&&j<n&&mine[i+1][j+1]=='*')
                    m
++;
                
if(j<n&&mine[i][j+1]=='*')
                    m
++;
                
if(j>0&&mine[i][j-1]=='*')
                    m
++;
                
out[i][j]=m+'0';
            }

            
else
                
out[i][j]='.';
    }

    
if(flag==1)
        
for(i=0;i<n;i++)
            
for(j=0;j<n;j++)
                
if(mine[i][j]=='*')
                    
out[i][j]='*';
    
for(i=0;i<n;i++)
    
{
        
for(j=0;j<n;j++)
            cout
<<out[i][j];
        cout
<<endl;
    }

    
return 0;
}

 

YOYO's Note:
┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄它是华丽的分隔线

【题意简述】

扫雷,从第一张图我们知道雷放在哪几个格子中,现在按第二张图的位置将X格子打开,显示出打开后雷的情况。


【粗略分析】

简单的模拟。
先读入第一张棋盘,标记好雷区,
接着读第二张棋盘,判定是否踩到雷,
显示出相关数字即可。


【C++源代码】

#include < iostream >
#define  N 15
using   namespace  std;

int  main()
{
    
int m,n,i,j,flag=0;
    
char c,mine[N][N],out[N][N];
    
int sweep[N][N];
    cin
>>n;
    
for(i=0;i<n;i++)
        
for(j=0;j<n;j++)
            cin
>>mine[i][j];
    
for(i=0;i<n;i++)
        
for(j=0;j<n;j++)
        
{
            cin
>>c;
            
if(c=='x')
                sweep[i][j]
=1;
            
else
                sweep[i][j]
=0;
        }

    
for(i=0;i<n;i++)
    
{
        
for(j=0;j<n;j++)
            
if(sweep[i][j]==1)
            
{
                m
=0;
                
if(mine[i][j]=='*')
                    flag
=1;
                
if(i>0&&j>0&&mine[i-1][j-1]=='*')
                    m
++;
                
if(i>0&&mine[i-1][j]=='*')
                    m
++;
                
if(i>0&&j<n&&mine[i-1][j+1]=='*')
                    m
++;
                
if(i<n&&j>0&&mine[i+1][j-1]=='*')
                    m
++;
                
if(i<n&&mine[i+1][j]=='*')
                    m
++;
                
if(i<n&&j<n&&mine[i+1][j+1]=='*')
                    m
++;
                
if(j<n&&mine[i][j+1]=='*')
                    m
++;
                
if(j>0&&mine[i][j-1]=='*')
                    m
++;
                
out[i][j]=m+'0';
            }

            
else
                
out[i][j]='.';
    }

    
if(flag==1)
        
for(i=0;i<n;i++)
            
for(j=0;j<n;j++)
                
if(mine[i][j]=='*')
                    
out[i][j]='*';
    
for(i=0;i<n;i++)
    
{
        
for(j=0;j<n;j++)
            cout
<<out[i][j];
        cout
<<endl;
    }

    
return 0;
}

【注意事项】

※ 如果踩到雷的话,将数字和雷全部显示(即模拟第三张图)。
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值