zoukankan      html  css  js  c++  java
  • $P1596 [USACO10OCT]湖计数Lake Counting$

    (problem)

    其实这题吧(DFS)好写一点(大雾

    所以就不讲(DFS)了 em

    (BFS)的话 主要是 判重。 方向。 队列。(没了吧

    至于位置 用两个队列?还是(pair)?还是结构体?

    [ ext{结构体大法好啊。emm} ]

    #ifdef Dubug
    
    #endif
    #include <bits/stdc++.h>
    using namespace std;
    typedef long long LL ;
    inline LL In() { LL res(0),f(1); register char c ;
    	while(isspace(c=getchar())) ; c == '-'? f = -1 , c = getchar() : 0 ;
    	while(res = (res << 1) + (res << 3) + (c & 15) , isdigit(c=getchar())) ;
    	return res * f ;
    }
    int n , m ;
    const int N = 100 + 5 ;//范围
    char c[N][N] ;
    bool vis[N][N] ;
    struct node {
    	int x , y ;
    };
    queue < node > q ;
    int dx[] = {0,0,0,1,-1,1,1,-1,-1} ;
    int dy[] = {0,1,-1,0,0,-1,1,-1,1} ;//8个方向。
    int ans = 0 ;
    inline void bfs(int i,int j) {//累加ans的值 因为一个水坑只会判断一次。
    	ans ++ , q.push(node{i,j}) , vis[i][j] = true ;//第一个特殊处理。
    	while(!q.empty()) {
    		int x = q.front().x , y = q.front().y ; q.pop() ;//取出来一个。
    		for(register int i=1;i<=8;i++){//不习惯 下标从 0 开始
    			int cx = x + dx[i] , cy = y + dy[i] ;
    			if(vis[cx][cy] or cx > n or cx < 1 or cy > m or cy < 1 or c[cx][cy] != 'W') continue ;//不符合条件就跳过。
    			q.push(node{cx,cy}) , vis[cx][cy] = true ;//入队。
    		}
    	}
    }
    signed main() {
    	memset(vis,0,sizeof(vis)) ;
    	n = In() , m = In() ;
    	for(register int i=1;i<=n;i++)
    		for(register int j=1;j<=m;j++) cin >> c[i][j] ;
    	for(register int i=1;i<=n;i++)
    		for(register int j=1;j<=m;j++) if(!vis[i][j] and c[i][j] =='W') bfs(i,j) ;//bfs
    	cout << ans << endl ;//输出答案
    	return 0 ;
    }
    
    不存在十全十美的文章 如同不存在彻头彻尾的绝望
  • 相关阅读:
    Hbase与Maven工程的Spring配置笔记
    CentOS7.0+Hadoop2.7.2+Hbase1.2.1搭建教程
    利用Python进行博客图片压缩
    Qt下Armadillo矩阵函数库的添加
    Qt下Eigen矩阵函数库的添加
    OpenCV2.4.13+VS2013配置方法
    OpenCV2.4.13+Qt5.6.2配置方法
    异想家Win10系统安装的软件与配置
    异想家Win7系统安装的软件与配置
    STM32学习笔记:基础例子
  • 原文地址:https://www.cnblogs.com/qf-breeze/p/10589646.html
Copyright © 2011-2022 走看看