简单的遍历搜索 Minesweeper

简单的遍历搜索 Minesweeper

 

#include  < iostream >
using   namespace  std;
char  map[ 102 ][ 102 ];
int  dir[ 8 ][ 2 =   //八个方向 从左上开始
    {-1,-1},{0,-1},{1,-1},{1,0},{1,1},{0,1},{-1,1},{-1,0}
}
;
int  n,m;

bool  isN( int  i)
{
    
if(i<n&&i>=0)
        
return true;
    
return false;
}

bool  isM( int  i)
{
    
if(i<m&&i>=0)
        
return true;
    
return false;
}


int  count_weeper( int  i, int  j)
{
    
char c;
    
int d ,total, x ,y;;
    
if(map[i][j]=='*')
        
return -1;
    
else
    
{
        
        
        
for(total = 0,d = 0; d < 8; d ++)
        
{
            x 
= i+dir[d][0];
            y 
= j+dir[d][1];
            
if(map[x][y]=='*'&&(isN(x))&&isM(y))
                total 
++;
        }

        
return total;
    }

}


int  main()
{
    
int i,j,temp;
    
int T = 1;;
    
while(1)
    
{
        
        
//scanf("%d%d",&n,&m);
        
        cin
>>n>>m;
        
if(m==0&&n==0)
            
break;
        
if(T>=2)printf("\n");
        
//fflush(stdin);
        
//for(i = 0; i < n;i++)
        
//    gets(map[i]);
        for(i = 0; i < n; i ++)
            
for(j = 0; j < m; j ++)
                cin
>>map[i][j];//scanf("%c",&map[i][j]);
        printf("Field #%d:\n",T++);
        
for(i = 0;i < n;i++)
        
{
            
for(j = 0;j < m;j ++)
            
{
                temp 
= count_weeper(i,j);
                
if(temp ==-1)
                    printf(
"*");
                
else
                    printf(
"%d",temp);
            }

            printf(
"\n");
        }

        
if(T==1)printf("\n");
        
    }

    
return 0;
}

你可能感兴趣的:(简单的遍历搜索 Minesweeper)