[洛谷 P1506] 拯救oibh总部

原题传送门

蒟蒻已经菜的连垃圾DFS都不会了,练一道难题。。。

直接上代码。。

#include
using namespace std;
char ch;
int x, y, ans, a[550][550], dx[5] = {0, 1, -1, 0, 0}, dy[5] = {0, 0, 0, 1, -1};
void dfs(int m, int n)
{
    if (m < 0 || n < 0 || m > x + 1 || n > y + 1 || a[m][n])
        return;
    a[m][n] = 2;
    for (int i = 1; i <= 4; i++)
        dfs(m + dx[i], n + dy[i]);
}
int main()
{
    scanf("%d%d", &x, &y);
    for (int i = 1; i <= x; i++)
        for (int j = 1; j <= y; j++)
        {
            cin >> ch;
            if (ch == '0')
                a[i][j] = 0;
            else
                a[i][j] = 1;
        }
    dfs(0, 0);
    for (int i = 1; i <= x; i++)
        for (int j = 1; j <= y; j++)
            if (!a[i][j])
                ans++;
    printf("%d", ans);
    return 0;
}

你可能感兴趣的:([洛谷 P1506] 拯救oibh总部)