POJ 2386 Lake Counting (水题,DFS)

题意:给定一个n*m的矩阵,让你判断有多少个连通块。

析:用DFS搜一下即可。

代码如下:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <list>
#include <sstream>
#define frer freopen("in.txt", "r", stdin)
#define frew freopen("out.txt", "w", stdout)
using namespace std;

typedef long long LL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 1e2 + 5;
const int mod = 1e9 + 7;
const int dr[] = {-1, 1, 0, 0, 1, 1, -1, -1};
const int dc[] = {0, 0, 1, -1, 1, -1, 1, -1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline int Min(int a, int b){ return a < b ? a : b; }
inline int Max(int a, int b){ return a > b ? a : b; }
inline LL Min(LL a, LL b){ return a < b ? a : b; }
inline LL Max(LL a, LL b){ return a > b ? a : b; }
inline bool is_in(int r, int c){
    return r >= 0 && r < n && c >= 0 && c < m;
}
char s[maxn][maxn];
int vis[maxn][maxn];

void dfs(int r, int c){
    vis[r][c] = 1;
    for(int i = 0; i < 8; ++i){
        int x = r + dr[i];
        int y = c + dc[i];
        if(is_in(x, y) && !vis[x][y] && s[x][y] == ‘W‘)  dfs(x, y);
    }
}
int main(){
    while(scanf("%d %d", &n, &m) == 2){
        for(int i = 0; i < n; ++i)  scanf("%s", s[i]);
        memset(vis, 0, sizeof vis);
        int ans = 0;
        for(int i = 0; i < n; ++i)
            for(int j = 0; j < m; ++j)
                if(!vis[i][j] && s[i][j] == ‘W‘)  dfs(i, j), ++ans;
        printf("%d\n", ans);
    }
    return 0;
}
时间: 2024-10-01 02:50:22

POJ 2386 Lake Counting (水题,DFS)的相关文章

《挑战》2.1 POJ 2386 Lake Counting (简单的dfs)

1 # include<cstdio> 2 # include<iostream> 3 4 using namespace std; 5 6 # define MAX 123 7 8 char grid[MAX][MAX]; 9 int nxt[8][2] = { {1,0},{0,-1},{-1,0},{0,1},{1,1},{1,-1},{-1,1},{-1,-1} }; 10 int n,m; 11 12 int can_move( int x,int y ) 13 { 14

POJ 2386 Lake Counting 搜索题解

简单的深度搜索就可以了,看见有人说什么使用并查集,那简直是大算法小用了. 因为可以深搜而不用回溯,故此效率就是O(N*M)了. 技巧就是增加一个标志P,每次搜索到池塘,即有W字母,那么就认为搜索到一个池塘了,P值为真. 搜索过的池塘不要重复搜索,故此,每次走过的池塘都改成其他字母,如'@',或者'#',随便一个都可以. 然后8个方向搜索. #include <stdio.h> #include <vector> #include <string.h> #include

poj 2386 Lake Counting

Lake Counting Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 24578   Accepted: 12407 Description Due to recent rains, water has pooled in various places in Farmer John's field, which is represented by a rectangle of N x M (1 <= N <= 10

poj 2386 Lake Counting(BFS解法)

Lake Counting Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 45142   Accepted: 22306 Description Due to recent rains, water has pooled in various places in Farmer John's field, which is represented by a rectangle of N x M (1 <= N <= 10

poj - 2386 Lake Counting &amp;&amp; hdoj -1241Oil Deposits (简单dfs)

http://poj.org/problem?id=2386 http://acm.hdu.edu.cn/showproblem.php?pid=1241 求有多少个连通子图.复杂度都是O(n*m). 1 #include <cstdio> 2 3 char filed[110][110]; 4 int n,m; 5 void dfs(int x,int y) 6 { 7 for(int i=-1;i<=1;i++) 8 for(int j=-1;j<=1;j++) //循环遍历8

POJ 2386 Lake Counting(DFS)

题意:有一个大小为N×M的园子,雨后积起了水.八连通的积水被认为是连在一起的.求园子里一共有多少水洼? * * * * W*    (八连通指的就是左图中相对W的*的部分) * * * Sample Input 10 12 W........WW. .WWW.....WWW ....WW...WW. .........WW. .........W.. ..W......W.. .W.W.....WW. W.W.W.....W. .W.W......W. ..W.......W. Sample O

POJ 2386 lake counting DFS

朴素DFS 计算有多少个水坑 TEST 10 12 W........WW. .WWW.....WWW ....WW...WW. .........WW. .........W.. ..W......W.. .W.W.....WW. W.W.W.....W. .W.W......W. ..W.......W. 3 /* DFS,挑战程序设计竞赛 巫 */ #include<cstdio> #include<iostream> using namespace std; string

dfs/poj 2386 Lake Counting

1 #include<cstdio> 2 using namespace std; 3 const int b[8][2]={{-1,-1},{-1,0},{-1,1},{1,-1},{1,0},{1,1},{0,-1},{0,1}}; 4 int n,m; 5 char a[110][110]; 6 7 void dfs(int x,int y) 8 { 9 a[x][y]='.'; 10 for (int k=0;k<8;k++) 11 { 12 int dx=x+b[k][0];

POJ 2386——Lake Counting

链接:http://poj.org/problem?id=2386 题解 #include<cstdio> #include<stack> using namespace std; const int MAX_M=105,MAX_N=105; char a[MAX_N][MAX_M]; int N,M; //现在位置 (x,y) void dfs(int x,int y){ a[x][y]='.'; //将现在所在位置替换为'.',即旱地 for(int dx=-1;dx<=