油田(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
*/
的格子相邻(横、竖或者对角线方向),就说它们属于同一个八连块。例如,图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
*/
相关文章推荐
- UVa 572 - Oil Deposits(油田 dfs) 错的离谱的我
- 算法竞赛入门经典 例题 6-12 油田(Oil Deposits, UVa 572)
- 用DFS求连通块(油田Oil Deposits,UVa 572)
- 油田 (Oil Deposits,Uva 572)
- 油田(Oil Deposits,UVa 572)
- 油田(Oil Deposits, UVa 572)
- 油田(Oil Deposits,UVa 572)
- uva 572 Oil Deposits(油田)—— DFS
- UVA 572-Oil Deposits
- Oil Deposits UVA - 572
- uva 572 Oil Deposits(DFS遍历图)
- UVa 572 Oil Deposits(DFS)
- UVa 572 - Oil Deposits
- UVa 572 - Oil Deposits 搜索专题
- UVA 572 - Oil Deposits
- UVA - 572 Oil Deposits
- UVa 572/POJ 1562/HDU 1241 Oil Deposits(DFS,两种写法)
- UVA - 572---Oil Deposits.
- UVA-572 Oil Deposits(BFS)
- Uva - 572 - Oil Deposits