您的位置:首页 > 其它

油田(Oil Deposits, UVa 572)

2018-03-19 21:20 573 查看
输入一个m行n列的字符矩阵,统计字符“@”组成多少个八连块。如果两个字符“@”所在
的格子相邻(横、竖或者对角线方向),就说它们属于同一个八连块。例如,图6-9中有两
个八连块。 #include <iostream>
#include <cstring>
using namespace std;
const int boundary = 105;
char graph[boundary][boundary];
int is_first[boundary][boundary] = { 0 }, r, c;
void dfs(int ar, int ac, int anums)
{
if (ar<0 || ac<0 || ar >= r || ac >= c || graph[ar][ac] != '@' || is_first[ar][ac] != 0)
return;
is_first[ar][ac] = anums;
for (int i = -1; i <= 1; ++i)
for (int j = -1; j <= 1; ++j)
if (i || j)
dfs(ar + i, ac + j, anums);
}
int main()
{
while (cin>>r>>c&&r&&c)
{
int nums = 0;//row行 column列
for (int i = 0; i<r; ++i)
cin >> graph[i];
for (int i = 0; i<r; ++i)
{
for (int j = 0; j<c; ++j)
{
if (graph[i][j] == '@' && !is_first[i][j])
dfs(i, j, ++nums);
}
}
cout << nums << endl;
memset(is_first, 0, sizeof(is_first));
}
return 0;
}
/*
1 1
*
3 5
*@*@*
**@**
*@*@*
1 8
@@****@*
5 5
****@
*@@*@
*@**@
@@@*@
@@**@
0 0
*/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: