您的位置:首页 > 其它

hdu-1241-Oil Deposits(dfs水)

2017-07-03 18:03 393 查看
题目链接

dfs水题

#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<time.h>
#include<set>
#include<stack>
#include<vector>
#include<map>

#define pi acos(-1)
#define maxn 111111
#define maxm 11111
#define INF 0x3F3F3F3F
#define eps 1e-8

#define pb push_back

#define mem(a) memset(a,0,sizeof a)

using namespace std;

const long long mod = 1000000007;
/**lyc**/
int n, m;
char smp[111][111];
int mp[111][111];
bool vis[111][111];
int stepx[9] = { 0, -1, 0, 1, 0, -1, -1, 1, 1 };
int stepy[9] = { 0, 0, -1, 0, 1, -1, 1, -1, 1 };
bool pan(int x, int y) {
if (x >= 1 && x <= n && y >= 1 && y <= m) return 1;
return 0;
}
void dfs(int x, int y) {
for (int i = 1; i <= 8; i++) {
int xx = x + stepx[i];
int yy = y + stepy[i];
if (pan(xx, yy) && !vis[xx][yy] && !mp[xx][yy] ) {
//  cout << xx << " " << yy << endl;
vis[xx][yy] = 1;
dfs(xx, yy);
}
}
}
int main() {
while (scanf("%d%d", &n, &m) && (n != 0 && m != 0)) {
for (int i = 0; i < n; i++) {
scanf("%s", smp[i]);
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
if (smp[i - 1][j - 1] == '*') mp[i][j] = 1;
else mp[i][j] = 0;
}
}
/*  for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
cout << mp[i][j] << " ";
}
cout << endl;
}*/
memset(vis, 0, sizeof vis);
int cnt = 0;
for(int i = 1; i <= n;i++)
for (int j = 1; j <= m; j++) {
if (!vis[i][j] && !mp[i][j]) {
cnt++;
vis[i][j] = 1;
dfs(i, j);
}
}
printf("%d\n", cnt);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  dfs