1. 程式人生 > >UVA 572 油田(BFS)

UVA 572 油田(BFS)

Discription

給定一個包含@ *的二維陣列,輸出有幾個@是聯通的,如果一個@在另外一個@相鄰的八個方向則視作相鄰。

Input

包含@ *的二維陣列。

Output

輸出聯通的個數

Sample Input

1 1
*
3 5
*@*@*
**@**
*@*@*
1 8
@@****@*
5 5
****@
*@@*@
*@**@
@@@*@
@@**@
0 0

Sample Output

0
1
2
2

Solution

BFS模板題

Code

#include <iostream>
#include <cstdio>
#include <algorithm> #include <vector> #include <cstring> #include <utility> #include <queue> using namespace std; int dx[8] = {-1, 1, 0, 0, 1, 1, -1, -1}; int dy[8] = {0, 0, -1, 1, 1, -1, 1, -1}; int col, row; char m[111][111]; int visit[111][111]; void bfs(int px, int
py) { queue<pair<int, int>> q; pair<int, int> start = make_pair(px, py); visit[px][py] = 1; q.push(start); while (!q.empty()) { pair<int, int> pos = q.front(); q.pop(); for (int i = 0; i < 8; i++) { int dx2 = pos.first + dx[i], dy2 = pos.second + dy[i]; if
(dx2 >= 0 && dx2 < row && dy2 >= 0 && dy2 < col && !visit[dx2][dy2] && m[dx2][dy2] == '@') { visit[dx2][dy2] = 1; pair<int, int> tmp = make_pair(dx2, dy2); q.push(tmp); } } } } int main() { freopen("in.txt", "r", stdin); while (~scanf("%d%d", &row, &col) && col && row) { int ans = 0; memset(visit, 0, sizeof(visit)); for (int i = 0; i < row; i++) scanf("%s", &m[i]); for (int i = 0; i < row; i++) for (int j = 0; j < col; j++) { if (!visit[i][j] && m[i][j] == '@') { ans++; bfs(i, j); } } printf("%d\n", ans); } return 0; }