0
点赞
收藏
分享

微信扫一扫

Minesweeper(c++代码)

芝婵 2022-03-31 阅读 25
数据结构

原题链接

#include <iostream>
#include <cstring>
using namespace std;
int main()
{
    int a, b;
    int num = 1;
    while (cin >> a >> b)
    {
        if (a == 0 && b == 0)
            break;
        string* s = new string[a];
        for (int i = 0; i < a; i++)
        {
            cin >> s[i];
        }
        cout << "Field #" << num << ":" << endl;
        string* str = new string[a + 2];
        string s1 = "";
        for (int i = 0; i < b + 2; i++)
        {
            s1 += '0';
        }
        string s2 = s1;
        str[0] = s1;
        str[a + 1] = s2;
        for (int i = 0; i < a; i++)
        {
            str[i + 1] = '0' + s[i] + '0';
        }
        for (int y = 1; y < a + 1; y++)
        {
            for (int x = 1; x < b + 1; x++)
            {
                if (str[y][x] == '*')
                {
                    cout << '*';
                    continue;
                }
                else
                {
                    int c = 0;
                    if (str[y - 1][x - 1] == '*')
                        c++;
                    if (str[y - 1][x] == '*')
                        c++;
                    if (str[y - 1][x + 1] == '*')
                        c++;
                    if (str[y][x - 1] == '*')
                        c++;
                    if (str[y][x + 1] == '*')
                        c++;
                    if (str[y + 1][x - 1] == '*')
                        c++;
                    if (str[y + 1][x] == '*')
                        c++;
                    if (str[y + 1][x + 1] == '*')
                        c++;
                    cout << c;
                }
            }
            cout << endl;
        }
        num++;
        cout << endl;
    }
    return 0;
}
举报

相关推荐

打怪C++代码

【C++代码】链表

C++代码重构

煎饼C++代码

蓝屏代码(C++)

C++常见代码

0 条评论