UVA上不去,有没有dalao帮忙交一下
查看原帖
UVA上不去,有没有dalao帮忙交一下
843596
Otion楼主2023/1/8 13:18
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int N = 105;
char map[N][N];
int view[N][N];
int total_row, total_col;
int count;
void dfs(int row, int col)
{
    int shift_x[] = {-1, -1, -1, 0, 0, 1, 1, 1};
    int shift_y[] = {-1, 0, 1, 1, -1, -1, 0, 1};
    for (int i = 0; i < 8; i++)
    {
        if (row + shift_x[i] < 0 || col + shift_y[i] < 0)
        {
            continue;
        }
        if (row + shift_x[i] >= total_row || col + shift_y[i] >= total_col)
        {
            continue;
        }
        if (view[row + shift_x[i]][col + shift_y[i]] == 1)
        {
            continue;
        }
        if (map[row + shift_x[i]][col + shift_y[i]] == '@')
        {
            view[row + shift_x[i]][col + shift_y[i]] = 1;
            dfs(row + shift_x[i], col + shift_y[i]);
        }
    }
}
int main()
{
    cin >> total_row >> total_col;
    do
    {
        if (total_row == 0 || total_col == 0)
        {
            break;
        }
        for (int i = 0; i < total_row; i++)
        {
            scanf("%s", map[i]);
        }
        for (int i = 0; i < total_row; i++)
        {
            for (int j = 0; j < total_col; j++)
            {
                if (view[i][j] == 1)
                {
                    continue;
                }
                if (map[i][j] == '@')
                {
                    count++;
                    dfs(i, j);
                }
            }
        }
        cout << count << endl;
        count = 0;
        cin >> total_row >> total_col;
        memset(map, 0, sizeof(map));
        memset(view, 0, sizeof(view));
    } while (total_row != 0 && total_col != 0);
    return 0;
}
2023/1/8 13:18
加载中...