zoukankan      html  css  js  c++  java
  • 洛谷题解 P1596 [USACO10OCT]Lake Counting S

    原题传送门

    0.前言 你谷重题水题这么多......
    1.思路
    这道题是P1331 海战的弱化版 ~~~~
    实际上就是一个DFS求联通块个数的问题
    (这一篇题解看不懂的同学不妨看看P1331 海战的题解)
    2.代码

    #include<iostream>
    #include<cstdio>
    using namespace std;
    inline void read(int &x){	//快读 
    	int f=1;
    	char ch=getchar();
    	while(ch<'0'||ch>'9'){
    		if(ch=='-') f=-1;
    		ch=getchar();
     	}
     	while(ch>='0'&&ch<='9'){
     		x=x*10+ch-'0';
     		ch=getchar();
    	}
    	x*=f; 
    }
    int n,m;
    char map[101][101];	//存图 
    int cnt;	//计数 
    int dx[9]={0,1,-1,0,0,1,-1,-1,1},dy[9]={0,0,0,1,-1,1,1,-1,-1};	//遍历八个方向 
    inline void search(int p,int q){
    	if(p<0||q<0||p>n+1||q>m+1||map[p][q]=='.') return;	//边界条件 
    	map[p][q]='*';	//把同一个水坑标记成一样的 
    	for(int i=1;i<=8;i++){	 
    		if(map[p+dx[i]][q+dy[i]]=='W') search(p+dx[i],q+dy[i]);	//还有水就继续搜 
    	}
    }
    int main(){
    	read(n);read(m);
    	for(int i=1;i<=n;i++){
    		for(int j=1;j<=m;j++){
    			cin>>map[i][j];
    		}
    	}
    	for(int i=1;i<=n;i++){
    		for(int j=1;j<=m;j++){
    			if(map[i][j]=='W'){	//只要有水就开始搜 
    				cnt++;	//肯定存在一个水坑 
    				search(i,j);	//搜出整个水坑 
    			}
    		}
    	}
    	printf("%d",cnt);
    	return 0;
    } 
    
  • 相关阅读:
    3.16 使用Zookeeper对HDFS HA配置自动故障转移及测试
    4、html的body内标签之input系列
    Gym
    Gym
    Gym
    Gym
    Gym
    Big Event in HDU HDU1171 (多重背包)
    Coins POJ 1742 多重背包部分和
    HDU 1059 Dividing 多重背包
  • 原文地址:https://www.cnblogs.com/-pwl/p/13692580.html
Copyright © 2011-2022 走看看