POJ LAKE COUNTING 2386

基本的dfs的题,看了挑战程序设计这本书对这个算法有了些初步的了解,通过不断的查找不断的更改,找到连在一起的块,提交poj的时候compile error 了两次,第一次没有修改编译器,可能是很久没做题的原因了吧,然后就是纠结是否输入的时候要加循环,导致while 的括号多了一个,我的codeblocks也没发现这个bug,算了,acm的题还是多练吧

#include<iostream>

#include<cstdio>

using namespace std;

const int MAX_N=101,MAX_M=101;

int n,m;

char field[MAX_N][MAX_M];

void dfs(int x,int y){

field[x][y]=‘.‘;

for(int xn=-1;xn<=1;xn++){

for(int yn=-1;yn<=1;yn++){

int xx=xn+x;

int yy=yn+y;

if(0<=xx&&xx<=n&&0<=yy&&yy<=m&&field[xx][yy]==‘W‘)

dfs(xx,yy);

}

}

}

void solve(){

int res=0;

for(int i=0;i<n;i++){

for(int j=0;j<m;j++){

if(field[i][j]==‘W‘){

dfs(i,j);

res++;

}

}

}

printf("%d\n",res);

}

int main(){

cin>>n>>m;

// char field[MAX_N][MAX_N+1];

for(int i=0;i<n;i++){

for(int j=0;j<m;j++){

cin>>field[i][j];

}

}

solve();

return 0;

}

时间: 2024-10-03 03:35:26

POJ LAKE COUNTING 2386的相关文章

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: 20003   Accepted: 10063 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

《挑战》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

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 (dfs+染色)

-->Lake Counting 直接上中文了 Descriptions: 由于近日阴雨连天,约翰的农场中中积水汇聚成一个个不同的池塘,农场可以用 N x M (1 <= N <= 100; 1 <= M <= 100) 的正方形来表示.农场中的每个格子可以用'W'或者是'.'来分别代表积水或者土地,约翰想知道他的农场中有多少池塘.池塘的定义:一片相互连通的积水.任何一个正方形格子被认为和与它相邻的8个格子相连. 给你约翰农场的航拍图,确定有多少池塘 Input Line 1

poj2386 Lake Counting(简单DFS)

转载请注明出处:http://blog.csdn.net/u012860063?viewmode=contents 题目链接:http://poj.org/problem?id=1562 ---------------------------------------------------------------------------------------------------------------------------------------------------------- 欢

【POJ2386】Lake Counting

Lake Counting Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 22861   Accepted: 11530 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

1751: [Usaco2005 qua]Lake Counting

1751: [Usaco2005 qua]Lake Counting Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 190  Solved: 150[Submit][Status][Discuss] Description Due to recent rains, water has pooled in various places in Farmer John's field, which is represented by a rectangle