HDU-2952 Counting Sheep(bfs/dfs)

版权声明:IT交流QQ群328880248,欢迎大家来一起学习一起交流~本篇文章未经同意请勿转载! https://blog.csdn.net/m0_38072683/article/details/87627510

Counting Sheep

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other)

Total Submission(s) : 9   Accepted Submission(s) : 8

Font: Times New Roman | Verdana | Georgia

Font Size: ← →

Problem 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

思路

连在一起的是一个羊群,让输出羊群的个数。

先找到一只羊,然后搜索所有和他属于一个羊群的羊,并把该羊群的所有点标志为已搜索

第一次搜索结束之后,res++ ,接着,从另一个未被搜过的羊开始找另一个羊群。

......循环

直到找到这片区域。

深搜代码:

#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;

const int maxn = 100 + 10;
int dir[8] = {1,0,-1,0,0,1,0,-1};
char a[maxn][maxn];
bool visited[maxn][maxn];
int h,w,res;

void dfs(int n, int m){
	
	visited[n][m] = true;
	for (int i = 0; i < 8; i += 2) {
		if ((n+dir[i]) >= 0 && (m+dir[i+1]) >= 0 && (n+dir[i]) < h && (m+dir[i+1]) < w
			&& !visited[n+dir[i]][m+dir[i+1]] && a[n+dir[i]][m+dir[i+1]] == '#') {
				dfs(n+dir[i], m+dir[i+1]);
			}
	}
}

int main() {
	int t;
	scanf("%d", &t);
	while(t--) {
		scanf("%d%d", &h, &w);
		memset(a, 0, sizeof(a));
		memset(visited, false, sizeof(visited));
		res = 0;
		for (int i = 0; i < h; i++) {
			scanf("%s", a[i]);
		}
		for (int i = 0; i < h; i++) {
			for (int j = 0; j < w; j++) {
				if (!visited[i][j] && a[i][j] == '#') {
					res++;
					dfs(i, j);
				}
			}
		}
		printf("%d\n", res);
	}
	return 0;
}

广搜代码:

#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;


struct node{
	int x;
	int y;
	node(int xx, int yy):x(xx),y(yy){}
};
const int maxn = 100 + 10;
int dir[8] = {1,0,-1,0,0,1,0,-1};
char a[maxn][maxn];
bool visited[maxn][maxn];
int h,w,res;


void bfs(int n, int m){
	queue<node> q;
	q.push(node(n,m));
	res++;
	while(!q.empty()){
		node p = q.front();
		q.pop();
		int x = p.x;
		int y = p.y;
		for (int i = 0; i < 8; i += 2) {
			
			if (x + dir[i] < h &&  y + dir[i+1] < w && (y + dir[i+1]) >= 0 && (x + dir[i]) >= 0
			  && !visited[x + dir[i] ][ y + dir[i+1] ] && a[x + dir[i]][y + dir[i+1]] == '#') {
			  	q.push(node(x + dir[i],y + dir[i+1]));
			  	visited[x + dir[i]][y + dir[i+1]] = true;
			  }
		}
		
		
	}
	
}


int main() {
	int t;
	scanf("%d", &t);
	while(t--) {
		scanf("%d%d", &h, &w);
		memset(a, 0, sizeof(a));
		memset(visited, false, sizeof(visited));
		res = 0;
		for (int i = 0; i < h; i++) {
			scanf("%s", a[i]);
		}
		for (int i = 0; i < h; i++) {
			for (int j = 0; j < w; j++) {
				if (!visited[i][j] && a[i][j] == '#') {
					bfs(i, j);
				}		
			}
		}
		printf("%d\n", res);
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/m0_38072683/article/details/87627510