东华oj 43扫雷

43 扫雷

作者: Turbo时间限制: 1S章节: 基本练习(数组)

问题描述 :

扫雷游戏你一定玩过吧!现在给你若干个n×m的地雷阵,请你计算出每个矩阵中每个单元格相邻单元格内地雷的个数,每个单元格最多有8个相邻的单元格。 0<n,m<=100

输入说明 :

输入包含若干个矩阵,对于每个矩阵,第一行包含两个整数n和m(0<n,m<=100),分别表示这个矩阵的行数和列数。接下来n行每行包含m个字符。安全区域用‘.’表示,有地雷区域用’*'表示。当n=m=0时输入结束。

输出说明 :

对于第i个矩阵,首先在单独的一行里打印序号:“Field #i:”,接下来的n行中,读入的’.'应被该位置周围的地雷数所代替。输出的每两个矩阵必须用一个空行隔开。

输入范例 :

4 4
*...
....
.*..
....
3 5
**...
.....
.*...
0 0

输出范例 :

Field #1:
*100
2210
1*10
1110

Field #2:
**100
33200
1*100

代码:

#include<stdio.h>
#include<algorithm>
#include<vector>
#include<stdlib.h>
#include<iostream>
#include <sstream>
using namespace std;
static int number=1;
int main()
{
    int row,col;
    while(cin>>row>>col)
    {
        if(row==0&&col==0)
          break;
        int count=0;
        char table[row][col];
        for(int i=0;i<row;i++)
        {
            for(int j=0;j<col;j++)
            {
                cin>>table[i][j];
            }
        }
        for(int i=0;i<row;i++)
        {
            for(int j=0;j<col;j++)
            {
                if(table[i][j]=='*')
                    continue;
                int time=0;
                if(i==0)
                {
                    if(j==0)
                    {
                        for(int m=i;m<=i+1;m++)
                        {
                            for(int n=j;n<=j+1;n++)
                            {
                                if(table[m][n]=='*')
                                    time++;
                            }
                        }
                    }
                    else if(j==col-1)
                    {
                        for(int m=i;m<=i+1;m++)
                        {
                            for(int n=j-1;n<col;n++)
                            {
                                if(table[m][n]=='*')
                                    time++;
                            }
                        }
                    }
                    else
                    {
                        for(int m=i;m<=i+1;m++)
                        {
                            for(int n=j-1;n<=j+1;n++)
                            {
                                if(table[m][n]=='*')
                                    time++;
                            }
                        }
                    }
                }
                else if(i==row-1)
                {
                    if(j==0)
                    {
                        for(int m=i-1;m<=row;m++)
                        {
                            for(int n=j;n<=j+1;n++)
                            {
                                if(table[m][n]=='*')
                                    time++;
                            }
                        }
                    }
                    else if(j==col-1)
                    {
                        for(int m=i-1;m<=row;m++)
                        {
                            for(int n=j-1;n<col;n++)
                            {
                                if(table[m][n]=='*')
                                    time++;
                            }
                        }
                    }
                    else
                    {
                        for(int m=i-1;m<=row;m++)
                        {
                            for(int n=j-1;n<=j+1;n++)
                            {
                                if(table[m][n]=='*')
                                    time++;
                            }
                        }
                    }
                }
                else if(j==0)
                {
                    for(int m=i-1;m<=i+1;m++)
                    {
                        for(int n=j;n<=j+1;n++)
                        {
                            if(table[m][n]=='*')
                                    time++;
                        }
                    }
                }
                else if(j==col-1)
                {
                    for(int m=i-1;m<=i+1;m++)
                    {
                        for(int n=j-1;n<=j;n++)
                        {
                            if(table[m][n]=='*')
                                    time++;
                        }
                    }
                }
                else
                {
                    for(int m=i-1;m<=i+1;m++)
                    {
                        for(int n=j-1;n<=j+1;n++)
                        {
                            if(table[m][n]=='*')
                                    time++;
                        }
                    }
                }
                table[i][j]=time+'0';
            }
        }
        cout<<"Field #"<<number<<":"<<endl;
        for(int i=0;i<row;i++)
        {
            for(int j=0;j<col;j++)
            {
                cout<<table[i][j];
            }
            cout<<endl;
        }
        number++;
        cout<<endl;
    }
    return 0;
}

方法有点笨,而且oj有一个例子过不了,求指正。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值