hdu 1241 BFS

不是一般的水。。。。。。

AC代码如下:

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

typedef struct{
	int x, y;
}Node;

int map[100][100];
int N, M;
bool mark[100][100];
int sum;
int moves[8][2] = { 1, 0, -1, 0, 0, 1, 0, -1, -1, -1, -1, 1, 1, -1, 1, 1 };


void BFS(){
	for( int i = 0; i < N; i++ ){
		for( int j = 0; j < M; j++ ){
			if( mark[i][j] == true || map[i][j] == 0 ){
				continue;
			}
			Node start;
			start.x = i;
			start.y = j;		
			sum++;
			mark[start.x][start.y] = true;
			queue<Node> q;
			q.push( start );
			while( !q.empty() ){
				Node n = q.front();
				q.pop();
				for( int l = 0; l < 8; l++ ){
					Node temp = n;
					temp.x += moves[l][0];
					temp.y += moves[l][1];
					if( mark[temp.x][temp.y] ){
						continue;
					}
					if( map[temp.x][temp.y] == 0 ){
						continue;
					}
					if( temp.x < 0 || temp.x >= N || temp.y < 0 || temp.y >= M ){
						continue;
					}
					mark[temp.x][temp.y] = true;
					q.push( temp );
				}
			}
		}	
	}
}

int main(){
	char s[101];
	while( cin >> N >> M && !( N == 0 && M == 0 ) ){
		for( int i = 0; i < N; i++ ){
			scanf( "%s", s );
			for( int j = 0; j < M; j++ ){
				if( s[j] == '@' ){
					map[i][j] = 1;
				}else{
					map[i][j] = 0;
				}
			}
		}
		memset( mark, false, sizeof(mark) );
		sum = 0;
		BFS();
		cout << sum << endl;
	}
	return 0;
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值