Seventeen


私信TA

用户名:2860619132

访问量:1171

签 名:

acacacacaca

等  级
排  名 23117
经  验 598
参赛次数 1
文章发表 2
年  龄 0
在职情况 学生
学  校 清华大学
专  业

  自我简介:

TA的其他文章

解题思路:

注意事项:

参考代码:

#include<iostream>

using namespace std;

const int N = 1010;

char a[N][N];

int d[][2] = { 0 , 1 , 0 , -1 , 1 , 0 , -1 , 0 };

int n;

int ans = 0;

int ans_island = 0;

int flag;


void dfs(int x, int y)

{

if (a[x][y] != '#')

{

return;

}

if (x < 1 || x > n || y < 1 || y > n)

{

return;

}

if (!flag)

{

int cnt = 0;

for (int i = 0; i < 4; i++)

{

int dx = x + d[i][0];

int dy = y + d[i][1];

if (dx > 0 && dx <= n && dy > 0 && dy <= n && a[dx][dy] != '.')

{

cnt++;

}

if (cnt == 4)

{

ans++;

flag = true;

}

}

}

a[x][y] = '*';

for (int i = 0; i < 4; i++)

{

int dx = x + d[i][0];

int dy = y + d[i][1];

if (dx > 0 && dx <= n && dy > 0 && dy <= n && a[dx][dy] != '.')

{

dfs(dx, dy);

}

}

}


int main()

{

cin >> n;

for (int i = 1; i <= n; i++)

{

for (int j = 1; j <= n; j++)

{

cin >> a[i][j];

}

}

for (int i = 1; i <= n; i++)

{

for (int j = 1; j <= n; j++)

{

if (a[i][j] == '#')

{

ans_island++;

flag = false;

dfs(i, j);

}

}

}

cout << ans_island - ans << endl;

return 0;

}



 

0.0分

1 人评分

看不懂代码?想转换其他语言的代码? 或者想问其他问题? 试试问问AI编程助手,随时响应你的问题:

编程语言转换

万能编程问答  

代码解释器

代码纠错

SQL生成与解释

  评论区