本文主要是介绍Lake Counting S,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
Lake Counting S
洛谷P1596
一道典型深搜题 把字符存成数字就变成了种子填充的题
思路:
- 设置变量和flag
- 输入 将字符变成数字
- 用深搜判断
- 输出结果
#include <iostream>
using namespace std;int g[100][100];
bool flag[100][100];
int n,m,ans=0;
char c=0;void dfs (int x,int y)
{if (x>=n || y>=m || x<0 || y<0 || flag[x][y]==1)return ;flag[x][y]=1;if (g[x][y]==1){dfs(x+1,y);dfs(x-1,y);dfs(x,y+1);dfs(x,y-1);dfs(x-1,y-1);dfs(x-1,y+1);dfs(x+1,y+1);dfs(x+1,y-1);}elsereturn ;}int main ()
{cin >> n >> m;for (int i=0;i<n;i++){for (int j=0;j<m;j++){cin >> c;if (c=='W')g[i][j]=1;elseg[i][j]=0;}}for (int i=0;i<n;i++){for (int j=0;j<m;j++){if (flag[i][j]==0 && g[i][j]==1){dfs(i,j);ans++;}}}cout << ans << endl;return 0;
}
此题解已AC,也欢迎指出更多优化方法~
❀❀❀❀❀❀❀❀❀❀❀❀❀❀❀❀❀❀❀❀❀❀❀❀❀❀❀❀❀❀❀❀❀❀❀❀❀❀❀❀
这篇关于Lake Counting S的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!