【紫书】Oil Deposits UVA - 572 dfs求联通块
题意:给你一个地图,求联通块的数量。
题解:
for(所有还未标记的‘@’点)
边dfs边在vis数组标记id,直到不能继续dfs。
输出id及可;
ac代码:
#define _CRT_SECURE_NO_WARNINGS
#include "stdio.h"
#include<stdio.h>
#include<algorithm>
#include<string>
#include<vector>
#include<list>
#include<set>
#include<iostream>
#include<string.h>
#include<queue>
#include<string>
#include<sstream>
using namespace std;
const int maxn = +;
string map[maxn];
int ans;
int idx[maxn][maxn];
int n, m;
void dfs(int r,int c,int id) {
if (r<||c<||r>=n||c>=m||map[r][c] == '*'||idx[r][c])return;
idx[r][c] = id;
for(int dr=-;dr<=;dr++)
for (int dc = -; dc <= ; dc++)
if(dr||dc)dfs(r + dr, c + dc,id);
}
int main(){ while (cin >> n >> m) {
if (n == && m == )break;
ans = ;
memset(idx, , sizeof(idx));
for (int i = ; i < n; i++) {
cin >> map[i];
}
for (int i = ; i < n; i++)
for (int j = ; j < m; j++)
if (idx[i][j] == && map[i][j] == '@') dfs(i, j, ++ans);
cout << ans<<endl;
}
return ;
}
【紫书】Oil Deposits UVA - 572 dfs求联通块的更多相关文章
- UVA 572 Oil Deposits油田(DFS求连通块)
UVA 572 DFS(floodfill) 用DFS求连通块 Time Limit:1000MS Memory Limit:65536KB 64bit IO Format: ...
- UVA 572 dfs求连通块
The GeoSurvComp geologic survey company is responsible for detecting underground oil deposits. GeoSu ...
- 利用DFS求联通块个数
/*572 - Oil Deposits ---DFS求联通块个数:从每个@出发遍历它周围的@.每次访问一个格子就给它一个联通编号,在访问之前,先检查他是否 ---已有编号,从而避免了一个格子重复访问 ...
- 用dfs求联通块(UVa572)
一.题目 输入一个m行n列的字符矩阵,统计字符“@”组成多少个八连块.如果两个字符所在的格子相邻(横.竖.或者对角线方向),就说它们属于同一个八连块. 二.解题思路 和前面的二叉树遍历类似,图也有DF ...
- Oil Deposits UVA - 572
The GeoSurvComp geologic survey company is responsible for detecting underground oil deposits. GeoSu ...
- POJ 1562 Oil Deposits (并查集 OR DFS求联通块)
Oil Deposits Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 14628 Accepted: 7972 Des ...
- ACM:油田(Oil Deposits,UVa 572)
/* Oil Deposits Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Tot ...
- HDU - 1213 dfs求联通块or并查集
思路:给定一个无向图,判断有几个联通块. AC代码 #include <cstdio> #include <cmath> #include <algorithm> ...
- 油田 (Oil Deposits UVA - 572)
题目描述: 原题:https://vjudge.net/problem/UVA-572 题目思路: 1.图的DFS遍历 2.二重循环找到相邻的八个格子 AC代码: #include <iostr ...
随机推荐
- 【Postgres】dump数据库备份与还原
备份 pg_dump.exe -h localhost -p 5432 -U postgres -F plain -v -f C:\Backup.sql db1 2> C:\Backup.log ...
- ios的单元測试OCUnit以及更新了之后的XCTestCase
1.像一般创建项目的步骤一样.创建一个用于測试的项目或者打开一个待測试的项目. (oc是5.0之前所使用的測试,如今用的是XCtestCase,默认会创建一个主的測试类.曾经版本号可能非常多步骤省去) ...
- 5 -- Hibernate的基本用法 --1 ORM和Hibernate
目前流行的编程语言,如Java.C#等,它们都是面向对象的编程语言,而目前铸就的数据库产品,例如Oracle.DB2等,依然是关系数据库等.编程语言和底层数据库的发展不协调,催生出了ORM框架.ORM ...
- SpringMVC -- 梗概--源码--壹--数据传递
附:实体类 Class : User package com.c61.entity; import java.text.SimpleDateFormat; import java.util.Date; ...
- 8 -- 深入使用Spring -- 0...
要点梗概: 利用后处理器扩展Spring容器 Bean后处理器和容器后处理器 Spring3.0 的“零配置” 支持 Spring的资源访问策略 在ApplicationContext中使用资源 AO ...
- gem install cocoapods ERROR: While executing gem ... (Gem::FilePermissionError)
在cocoapods 执行 sudo gem install cocoapods 的时候出现 While executing gem ... (Gem::FilePermissionError) ...
- springJdbc in 查询,Spring namedParameterJdbcTemplate in查询
springJdbc in 查询,Spring namedParameterJdbcTemplate in查询, SpringJdbc命名参数in查询,namedParameterJdbcTempla ...
- 【代码审计】iZhanCMS_v2.1 代码执行漏洞分析
0x00 环境准备 iZhanCMS官网:http://www.izhancms.com 网站源码版本:爱站CMS(zend6.0) V2.1 程序源码下载:http://www.izhancms ...
- “std”: 具有该名称的命名空间不存在
当只用using namesp std 时,会报 error C2871: “std”: 具有该名称的命名空间不存在. 包含一个含有std的头文件就不会报错了,比如<iostream>.& ...
- poj_1042 贪心算法
poj 1042 gone fishing 题目要求: 由有n个湖, 按照顺序排列,一个人从第一个湖向最后一个湖行进(方向只能从湖0到湖n-1),途中可以在湖中钓鱼.在每个湖中钓鱼时,开始的5分钟内可 ...