HDU 2952 Counting Sheep

Source: Internet
Author: User
Tags flock

Topic Connection

http://acm.hdu.edu.cn/showproblem.php?pid=2952

Counting sheepdescription

A while ago I had trouble sleeping. I used to lie awake, staring on the ceiling, for hours and hours. Then one day my grandmother suggested I tried counting sheep after I ' d gone to bed. As always while my grandmother suggests things, I decided to try it out. The only problem is, there were no sheep around to being counted when I went to bed.



Creative as I am, that is wasn ' t going to stop me. I sat down and wrote a computer program this made a grid of characters, where # represents a sheep, while. is grass (or whatever-like, just not sheep). The counting a little more interesting, I also decided I wanted to count flocks of sheep instead of single sheep. The sheep is 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 was in the same flock as sheep C, then sheeps A and C were in The same flock.


Now, I ' ve got a new problem. Though counting these sheep actually helps me fall asleep, I find that it's 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 the write this program for me.

Input

The first line of input contains a is a single number T, and the number of test cases to follow.

Each test case is begins with a line containing of numbers, H and W, the height and width of the sheep grid. Then follows H lines, each containing W characters (either # or.), describing this part of the grid.

Output

For each test case, output a line containing a single number, the amount of sheep flock son, grid according to the Rul Es stated in the problem description.

Notes and Constraints
0 < T <= 100
0 < h,w <= 100

Sample Input

2
4 4
#.#.
.#.#
#.##
.#.#
3 5
###.#
.. #..
#.###

Sample Output

6
3

DFS to find the number of connected blocks:
1#include <algorithm>2#include <iostream>3#include <cstdlib>4#include <cstring>5#include <cstdio>6#include <vector>7#include <map>8 usingstd::cin;9 usingstd::cout;Ten usingStd::endl; One usingStd::find; A usingStd::sort; - usingStd::map; - usingstd::p air; the usingstd::vector; - usingStd::multimap; - #definePB (E) push_back (e) - #defineSZ (c) (int) (c). Size () + #defineMP (A, b) Make_pair (A, B) - #defineAll (c) (c). Begin (), (c). End () + #defineITER (c) Decltype ((c). Begin ()) A #defineCLS (arr,val) memset (arr,val,sizeof (arr)) at #defineCpresent (c, E) (Find (All (c), (e))! = (c). End ()) - #defineRep (i, n) for (int i = 0; i < (int) (n); i++) - #defineTR (c, I) for (ITER (c) i = (c). Begin (); I! = (c). end (); ++i) - Const intN = the; -typedef unsignedLong Longull; - CharG[n][n]; in BOOLVis[n][n]; - intH, W; to Const intDx[] = {0,0, -1,1}, dy[] = {-1,1,0,0 }; + voidDfsintXinty) { -Vis[x][y] =true; theRep (I,4) { *         intNX = x + dx[i], NY = y +Dy[i]; $         if(NX <0|| NX >= H | | NY <0|| NY >= W)Continue;Panax Notoginseng         if(Vis[nx][ny] | | G[nx][ny] = ='.')Continue; - DFS (NX, NY); the     } + } A intMain () { the #ifdef LOCAL +Freopen ("In.txt","R", stdin); -Freopen ("OUT.txt","w+", stdout); $ #endif $     intT, ans; -scanf"%d", &t); -      while(t--) { theAns =0; -CLS (Vis,false);Wuyiscanf"%d%d", &h, &W); theRep (i, H) scanf ("%s", G[i]); - Rep (i, H) { Wu Rep (J, W) { -                 if(!vis[i][j] && g[i][j] = ='#') ans++, Dfs (i, j); About             } $         } -printf"%d\n", ans); -     } -     return 0; A}
View Code

HDU 2952 Counting Sheep

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.