fjnu 1579 Mine Sweeper

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

 
  

Sample Output

 
  
 
  
 
  
Source:

#include
< iostream >
using   namespace  std;

int  n;
char  a[ 15 ][ 15 ];
char  b[ 15 ][ 15 ];
char  c[ 15 ][ 15 ];
int  flag;

void  input()
{
    
int i,j;
    
for(i=1;i<=n;i++)
        
for(j=1;j<=n;j++)
            cin
>>a[i][j];
    
for(i=1;i<=n;i++)
        
for(j=1;j<=n;j++)
            cin
>>b[i][j];
    
for(i=1;i<=n;i++)
        
for(j=1;j<=n;j++)
            c[i][j]
='.';

}


int  count( int  x, int  y)
{
    
if(a[x][y]=='*') flag=1;
    
int t=0;
    
if(x-1>0&&y-1>0&&a[x-1][y-1]=='*') t++;
    
if(x-1>0&&a[x-1][y]=='*') t++;
    
if(x-1>0&&y+1<=n&&a[x-1][y+1]=='*') t++;
    
if(y-1>0&&a[x][y-1]=='*') t++;
    
if(y+1<=n&&a[x][y+1]=='*') t++;
    
if(x+1<=n&&y-1>0&&a[x+1][y-1]=='*') t++;
    
if(x+1<=n&&a[x+1][y]=='*') t++;
    
if(x+1<=n&&y+1<=n&&a[x+1][y+1]=='*') t++;
    
return t;
}


void  fun()
{
    
int i,j;
    
for(i=1;i<=n;i++)
        
for(j=1;j<=n;j++)
        
{
            
if(b[i][j]=='x')
            
{
                c[i][j]
=count(i,j)+48;
            }

        }

    
if(flag==1)
    
{
        
for(i=1;i<=n;i++)
            
for(j=1;j<=n;j++)
            
{
                
if(a[i][j]=='*') c[i][j]=a[i][j];
            }

    }

}


void  output()
{
    
int i,j;
    
for(i=1;i<=n;i++)
    
{    
        
for(j=1;j<=n;j++)
            cout
<<c[i][j];
        cout
<<endl;
    }

}




int  main()
{
//    freopen("fjnu_1579.in","r",stdin);
    while(cin>>n)
    
{
        flag
=0;
        input();
        fun();
        output();
        cout
<<endl;
    }

    
return 0;
}




    



 

你可能感兴趣的:(☆ACM,解题报告☆)