简单的一道搜索题, 题目意思是给你一幅图, *代表没油, @代表有油, 让你求连在一起的油有几块。
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <algorithm>
#include <queue>
#define mem(a) memset(a, 0, sizeof(a))
#define maxn 105
using namespace std;
int ox[8] = {-1, 1, 0, 0, 1, -1, -1, 1}, oy[8] = {0, 0, -1, 1, 1, -1, 1, -1}, n, m;
char map[maxn][maxn];
void bfs(int sx, int sy)
{
int i, x, y;
queue<int> q;
q.push(sx);
q.push(sy);
while(!q.empty())
{
x = q.front();
q.pop();
y = q.front();
q.pop();
for(i = 0;i < 8;i++)
{
if((x + ox[i]) >= 0&&(x + ox[i]) < n&&(y + oy[i]) >= 0&&(y + oy[i]) < m&&map[x + ox[i]][y + oy[i]] == '@')
{
map[x + ox[i]][y + oy[i]] = '*';
q.push(x + ox[i]);
q.push(y + oy[i]);
}
}
}
}
int main(int argc, char *argv[])
{
int i, j, k, ans;
for(;;)
{
ans = 0;
mem(map);
scanf("%d%d%*c",&n, &m);
if(n == 0&&m == 0)
break;
for(i = 0;i < n;i++)
{
scanf("%s", map[i]);
}
for(i = 0;i < n;i++)
{
for(j = 0;j < m;j++)
{
if(map[i][j] == '@')
{
ans++;
map[i][j] = '*';
bfs(i, j);
}
}
}
printf("%d\n", ans);
}
return 0;
}