http://acm.hdu.edu.cn/showproblem.php?pid=2952
A while ago I had trouble sleeping. I used to lie awake, staring at the ceiling, for hours and hours. Then one day my grandmother suggested I tried counting sheep after I'd gone to bed. As always when my grandmother suggests things, I decided to try it out. The only problem was, there were no sheep around to be counted when I went to bed.
Creative as I am, that wasn't going to stop me. I sat down and wrote a computer program that made a grid of characters, where # represents a sheep, while . is grass (or whatever you like, just not sheep). To make the counting a little more interesting, I also decided I wanted to count flocks of sheep instead of single sheep. Two sheep are in the same flock if they share a common side (up, down, right or left). Also, if sheep A is in the same flock as sheep B, and sheep B is in the same flock as sheep C, then sheeps A and C are in the same flock.
Now, I've got a new problem. Though counting these sheep actually helps me fall asleep, I find that it is extremely boring. To solve this, I've decided I need another computer program that does the counting for me. Then I'll be able to just start both these programs before I go to bed, and I'll sleep tight until the morning without any disturbances. I need you to write this program for me.
The first line of input contains a single number T, the number of test cases to follow.
Each test case begins with a line containing two numbers, H and W, the height and width of the sheep grid. Then follows H lines, each containing W characters (either # or .), describing that part of the grid.
For each test case, output a line containing a single number, the amount of sheep flock son that grid according to the rules stated in the problem description.
Notes and Constraints
0 < T <= 100
0 < H,W <= 100
2
4 4
#.#.
.#.#
#.##
.#.#
3 5
###.#
..#..
#.###
6
3
1 #include<algorithm> 2 #include<iostream> 3 #include<cstdlib> 4 #include<cstring> 5 #include<cstdio> 6 #include<vector> 7 #include<map> 8 using std::cin; 9 using std::cout; 10 using std::endl; 11 using std::find; 12 using std::sort; 13 using std::map; 14 using std::pair; 15 using std::vector; 16 using std::multimap; 17 #define pb(e) push_back(e) 18 #define sz(c) (int)(c).size() 19 #define mp(a, b) make_pair(a, b) 20 #define all(c) (c).begin(), (c).end() 21 #define iter(c) decltype((c).begin()) 22 #define cls(arr,val) memset(arr,val,sizeof(arr)) 23 #define cpresent(c, e) (find(all(c), (e)) != (c).end()) 24 #define rep(i, n) for (int i = 0; i < (int)(n); i++) 25 #define tr(c, i) for (iter(c) i = (c).begin(); i != (c).end(); ++i) 26 const int N = 110; 27 typedef unsigned long long ull; 28 char G[N][N]; 29 bool vis[N][N]; 30 int H, W; 31 const int dx[] = { 0, 0, -1, 1 }, dy[] = { -1, 1, 0, 0 }; 32 void dfs(int x, int y) { 33 vis[x][y] = true; 34 rep(i, 4) { 35 int nx = x + dx[i], ny = y + dy[i]; 36 if (nx < 0 || nx >= H || ny < 0 || ny >= W) continue; 37 if (vis[nx][ny] || G[nx][ny] == '.') continue; 38 dfs(nx, ny); 39 } 40 } 41 int main() { 42 #ifdef LOCAL 43 freopen("in.txt", "r", stdin); 44 freopen("out.txt", "w+", stdout); 45 #endif 46 int t, ans; 47 scanf("%d", &t); 48 while (t--) { 49 ans = 0; 50 cls(vis, false); 51 scanf("%d %d", &H, &W); 52 rep(i, H) scanf("%s", G[i]); 53 rep(i, H) { 54 rep(j, W) { 55 if (!vis[i][j] && G[i][j] == '#') ans++, dfs(i, j); 56 } 57 } 58 printf("%d\n", ans); 59 } 60 return 0; 61 }