题目链接:https://vjudge.net/problem/HDU-1241

题意:问有几个油田,一个油田由相邻的‘@’,组成。

思路:bfs,dfs都可以,只需要遍历地图,遇到‘@’,跑一遍搜索,标记跑过的点,然后油田数+1.


 #include <iostream>
#include <cstring>
#include<vector>
#include<string>
#include <cmath>
#include <map>
#include <queue>
#include <algorithm>
using namespace std; #define inf (1LL << 31) - 1
#define rep(i,j,k) for(int i = (j); i <= (k); i++)
#define rep__(i,j,k) for(int i = (j); i < (k); i++)
#define per(i,j,k) for(int i = (j); i >= (k); i--)
#define per__(i,j,k) for(int i = (j); i > (k); i--) const int N = ;
int mv[][] = { { , }, { , - }, { , }, { -, },
{ , }, { -, - }, { -, }, { , - } };
char mp[N][N];
bool vis[N][N];
int n, m; struct node{
int x, y;
}; inline void init(){
rep(i, , n) rep(j, , m) vis[i][j] = false;
} inline void input(){ init();
rep(i, , n) rep(j, , m) cin >> mp[i][j];
} inline bool check(int x, int y){
return x >= && x <= n && y >= && y <= m;
} void bfs(int now_x, int now_y){ vis[now_x][now_y] = true; queue<node> que;
que.push(node{ now_x, now_y }); while (!que.empty()){ node tmp = que.front();
que.pop(); rep__(p, , ){
int dx = tmp.x + mv[p][];
int dy = tmp.y + mv[p][]; if (check(dx, dy) && !vis[dx][dy] && mp[dx][dy] == '@'){
vis[dx][dy] = true;
que.push(node{ dx, dy });
}
}
}
} void work(){ int ans = ; rep(i, , n) rep(j, , m){
//附加条件,这个‘@’没被访问过,说明是新的油田的一部分
if (mp[i][j] == '@' && !vis[i][j]){
bfs(i, j);
++ans;
}
} cout << ans << endl;
} int main(){ ios::sync_with_stdio(false);
cin.tie(); while (cin >> n >> m){ if (m == ) break; input();
work();
} return ;
}

kuangbin专题 专题一 简单搜索 Oil Deposits HDU - 1241的更多相关文章

  1. Oil Deposits HDU - 1241 (dfs)

    Oil Deposits HDU - 1241 The GeoSurvComp geologic survey company is responsible for detecting undergr ...

  2. (深搜)Oil Deposits -- hdu -- 1241

    链接: http://acm.hdu.edu.cn/showproblem.php?pid=1241 Time Limit: 2000/1000 MS (Java/Others)    Memory ...

  3. Oil Deposits HDU 1241

    The GeoSurvComp geologic survey company is responsible for detecting underground oil deposits. GeoSu ...

  4. kuangbin专题总结一 简单搜索

    A - 棋盘问题:在一个给定形状的棋盘(形状可能是不规则的)上面摆放棋子,棋子没有区别.要求摆放时任意的两个棋子不能放在棋盘中的同一行或者同一列,请编程求解对于给定形状和大小的棋盘,摆放k个棋子的所有 ...

  5. 搜索入门_简单搜索bfs dfs大杂烩

    dfs题大杂烩 棋盘问题  POJ - 1321 和经典的八皇后问题一样.  给你一个棋盘,只有#区域可以放棋子,同时同一行和同一列只能有一个棋子. 问你放k个棋子有多少种方案. 很明显,这是搜索题. ...

  6. [kuangbin带你飞]专题一 简单搜索

            ID Origin Title 454 / 1008 Problem A POJ 1321 棋盘问题   328 / 854 Problem B POJ 2251 Dungeon Ma ...

  7. 搜索专题:HDU1241 Oil Deposits

    Oil Deposits Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Tot ...

  8. hdu 1241 Oil Deposits (简单搜索)

    题目:   The GeoSurvComp geologic survey company is responsible for detecting underground oil deposits. ...

  9. HDU 1241 Oil Deposits DFS(深度优先搜索) 和 BFS(广度优先搜索)

    Oil Deposits Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total ...

随机推荐

  1. QT Linux Demo程序编译

    我手上的qt源码包为:qt-everywhere-opensource-src-4.7.0.tar.gz 在Linux下编译比较容易,解压后直接 ./configure,一般会报缺少什么库这些.自己遇 ...

  2. jquery 表格练习

    <!DOCTYPE html><html lang="en" xmlns="http://www.w3.org/1999/xhtml"> ...

  3. linux系统中ls命令的用法

    普通文件: -,f目录文件: d链接文件(符号链接): L设备文件:字符设备:c块设备:b命名管道: p套接字文件: s linux文件时间戳 时间分为三种类型:创建时间,修改时间:open访问时间: ...

  4. 用Delphi将数据导入到Excel并控制Excel

    一.调用Excel的方法:一般情况下有两种方法调用Excel:1.  直接使用Delphi自带的组件:在Form中分别放入ExcelApplication, ExcelWorkbook和ExcelWo ...

  5. delphi dom动态创建节点方法

    在一次测试demo中 需要动态的创建xml节点并添加,实现方法如下: var NewItem:IXMLDOMElement; NewItem:=ConfigDoc.createElement('ite ...

  6. Android 查看App冷启动时间/热启动时间/页面打开时间

    Android 查看App冷启动时间/热启动时间/页面打开时间 冷启动时间 热启动时间 页面打开时间 通过adb查看 adb shell am start -W packageName/Activit ...

  7. [PowerDesign]将数据库从SQL Server数据库转换为MySQL

    原文:[PowerDesign]将数据库从SQL Server数据库转换为MySQL 一.迁移Database Schema. 首先使用Sybase Powerdesigner的逆向工程功能,逆向出S ...

  8. GzipStream的简单使用压缩和解压

    压缩和解压都需要用到三个流实例,分别是文件读取流.文件写入流.压缩流. 读取流和写入流有多种形式,压缩流就一种GzipStream. 不同的是对于压缩,是需要用文件写入流作为创建压缩流实例的参数, 压 ...

  9. 使用EurekaLog时遇到的问题

    1.在DLL项目中千万不要加入EurekaLog,不然在主程序调用时就会出现莫名其妙的内存问题. 2.要使用EurekaLog发邮件的功能,发邮件的SMTP服务器必须支持8bit MIME编码.如SI ...

  10. Qt在Windows上的调试器安装与配置

    如果安装Qt时使用的是Visual Studio的预编译版,那么很有可能就会缺少调试器(Debugger),而使用MSVC的Qt对应的原生调试器是CDB(对应MinGW的Qt使用GDB调试器).本文介 ...