hdu 2952 Counting Sheep

题目连接

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

Counting Sheep

Description

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.

Input

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.

Output

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

Sample Input

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

Sample Output

6
3

dfs求连通块个数。。

 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 }

时间: 2024-08-02 15:10:56

hdu 2952 Counting Sheep的相关文章

HDU 2952 Counting Sheep (DFS找联通块)

题目链接:请戳这里.   题目大意及思路:读懂题意就好了,就是DFS找联通块. 没什么好说的,见代码吧. #include<cstdio> #include<cstring> #include<algorithm> #define N 100+5 using namespace std; int n,m; char g[N][N]; int dir[4][2]={1,0,0,1,-1,0,0,-1}; void dfs(int x,int y) { for(int i=

HDOJ 2952 Counting Sheep

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2952 Counting Sheep Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2231    Accepted Submission(s): 1474 Problem Description A while ago I had tro

HDUJ 2952 Counting Sheep 搜索

Counting Sheep Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2039    Accepted Submission(s): 1342 Problem Description A while ago I had trouble sleeping. I used to lie awake, staring at the c

ACM HDU-2952 Counting Sheep A while ago I had trouble sleeping

Counting Sheep Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2060    Accepted Submission(s): 1359 Problem Description A while ago I had trouble sleeping. I used to lie awake, staring at the c

hdu 1396 Counting Triangles (递推)

Counting Triangles Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 2012    Accepted Submission(s): 966 Problem Description Given an equilateral triangle with n the length of its side, program to

【DFS深搜初步】HDOJ-2952 Counting Sheep、NYOJ-27 水池数目

[题目链接:HDOJ-2952] Counting Sheep Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2476    Accepted Submission(s): 1621 Problem Description A while ago I had trouble sleeping. I used to lie awake,

HDU 1264 Counting Squares (线段树-扫描线-矩形面积并)

Problem A : Counting Squares From:HDU, 1264 Problem Description Your input is a series of rectangles, one per line. Each rectangle is specified as two points(X,Y) that specify the opposite corners of a rectangle. All coordinates will be integers in t

HDU 3046 Pleasant sheep and big big wolf(最小割)

HDU 3046 Pleasant sheep and big big wolf 题目链接 题意:一个n * m平面上,1是羊,2是狼,问最少要多少围墙才能把狼全部围住,每有到达羊的路径 思路:有羊和狼,要分成两个集合互不可达,显然的最小割,建图源点连狼,容量无穷,羊连汇点,容量无穷,然后相邻格子连边,容量为1 代码: #include <cstdio> #include <cstring> #include <queue> #include <algorithm

Counting sheep...

Counting sheep... Description: Consider an array of sheep where some sheep may be missing from their place. We need a function that counts the number of sheep present in the array (true means present). For example, [true, true, true, false, true, tru