一、题目(CF 598D)

输入一个n x m的字符矩阵,求从某个空点出发,能碰到多少面墙壁,总共询问k次。(3 ≤m,n ≤1000,1 ≤ k ≤ min(nm,100 000))

二、解题思路

用DFS找连通分量:从每个“.”格子出发,递归遍历与之相邻的“*”格子,且写上相同的联通分量(即代码中的blocks数组),同时统计该联通分量边界墙壁面数。由于存在多次查询,我们用blocks[i][j]记录格子(i,j)所在的联通分量标号,用res[i]表示联通分量i的边界墙壁面数。

三、代码实现

 #include<stdio.h>
#include<iostream>
#include<string.h>
#include<stdbool.h>
using namespace std; const int maxn = + ;
const int maxm = + ;
const int maxk = + ;
const int maxx = + ;
int n, m;
char maze[maxn][maxm];
bool vis[maxn][maxm];
int blocks[maxn][maxm];
int res[maxx];
int dx[] = { -,,, }, dy[] = { ,,,- };
int flag = ; void dfs(int x, int y, int flag,int& sum) //sum传引用
{
vis[x][y] = true;
blocks[x][y] = flag;
for (int i = ; i < ; i++)
{
int xx = x + dx[i]; int yy = y + dy[i];
if (xx >= && xx < n && yy >= && yy < m && (!vis[xx][yy]))
{
if (maze[xx][yy] == '*')
sum++;
else
dfs(xx, yy, flag,sum);
}
}
}
int main()
{
int k;
scanf("%d%d%d", &n, &m, &k);
memset(vis, , sizeof(vis)); for (int i = ; i < n; i++)
for (int j = ; j < m; j++)
cin >> maze[i][j]; for (int i = ; i < n; i++)
for (int j = ; j < m; j++)
{
if (maze[i][j] == '.' && (!vis[i][j])) {
int sum = ;
dfs(i, j, ++flag,sum);
res[flag] = sum;
}
} while (k--)
{
int sx, sy;
scanf("%d%d", &sx, &sy);
printf("%d\n", res[blocks[sx - ][sy - ]]);
}
return ;
}

用dfs遍历联通块(优化)的更多相关文章

  1. 利用DFS求联通块个数

    /*572 - Oil Deposits ---DFS求联通块个数:从每个@出发遍历它周围的@.每次访问一个格子就给它一个联通编号,在访问之前,先检查他是否 ---已有编号,从而避免了一个格子重复访问 ...

  2. 用dfs求联通块(UVa572)

    一.题目 输入一个m行n列的字符矩阵,统计字符“@”组成多少个八连块.如果两个字符所在的格子相邻(横.竖.或者对角线方向),就说它们属于同一个八连块. 二.解题思路 和前面的二叉树遍历类似,图也有DF ...

  3. HDU - 1213 dfs求联通块or并查集

    思路:给定一个无向图,判断有几个联通块. AC代码 #include <cstdio> #include <cmath> #include <algorithm> ...

  4. 【紫书】Oil Deposits UVA - 572 dfs求联通块

    题意:给你一个地图,求联通块的数量. 题解: for(所有还未标记的‘@’点) 边dfs边在vis数组标记id,直到不能继续dfs. 输出id及可: ac代码: #define _CRT_SECURE ...

  5. 中矿新生赛 H 璐神看岛屿【BFS/DFS求联通块/连通块区域在边界则此连通块无效】

    时间限制:C/C++ 1秒,其他语言2秒空间限制:C/C++ 32768K,其他语言65536K64bit IO Format: %lld 题目描述 璐神现在有张n*m大小的地图,地图上标明了陆地(用 ...

  6. K - Ancient Messages(dfs求联通块)

    K - Ancient Messages Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Subm ...

  7. POJ 1562 Oil Deposits (并查集 OR DFS求联通块)

    Oil Deposits Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 14628   Accepted: 7972 Des ...

  8. Educational Codeforces Round 1D 【DFS求联通块】

    http://blog.csdn.net/snowy_smile/article/details/49924965 D. Igor In the Museum time limit per test ...

  9. POJ 3620 Avoid The Lakes【DFS找联通块】

    Avoid The Lakes Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6826   Accepted: 3637 D ...

随机推荐

  1. Flutter实战视频-移动电商-50.持久化_shared_preferences

    50.持久化_shared_preferences 当app关掉了.再进去的时候 ,购物车的内容还是存在. sqflite提供这个来操作SQLite数据库 flutter提供三种持久化的工具 今天要学 ...

  2. android系统的源代码获取(亲测可用)

    1.在线阅读各版本源代码: http://androidxref.com/ 2.下载到本地: http://blog.csdn.net/yin1031468524/article/details/55 ...

  3. HDU - 1241 POJ - 1562 Oil Deposits DFS FloodFill漫水填充法求连通块问题

    Oil Deposits The GeoSurvComp geologic survey company is responsible for detecting underground oil de ...

  4. c#类—成员函数和封装及构造函数、析构函数、静态成员

    C# 类(Class) 当您定义一个类时,您定义了一个数据类型的蓝图.这实际上并没有定义任何的数据,但它定义了类的名称意味着什么,也就是说,类的对象由什么组成及在这个对象上可执行什么操作.对象是类的实 ...

  5. 用Go语言异常机制模拟TryCatch异常捕捉

    有的同学看到Go和TryCatch一起出现,心里可能会说,难道Go语言升级了,加入了try...catch语句.哈哈,其实Go语言从创建之初就没打算加入try...catch语句,因为创建Go的那帮大 ...

  6. Unity5自动命名Assetbundle并打包

    http://www.shihuanjue.com/?p=57 using UnityEngine; using System.Collections; using UnityEditor; usin ...

  7. NGUI研究院之UISprite和UITexture浅谈

    NGUI的三大组件,UILabel.UISprite.UITexture,它们三个同时都继承UIWidget.先回到一个很郁闷的话题上,到底是优化DrawCall还是优化内存. UISprite : ...

  8. Springboot整合elasticSearch的官方API实例

    前言:在上一篇博客中,我介绍了从零开始安装ElasticSearch,es是可以理解为一个操作数据的中间件,可以把它作为数据的存储仓库来对待,它具备强大的吞吐能力和计算能力,其基于Lucene服务器开 ...

  9. 调用Web API将文件上传到服务器的方法(.Net Core)

    最近遇到一个将Excel通过Web API存到服务器的问题,其中涉及到Excel的读取.调用API.Web  API怎么进行接收. 一. Excel的读取.调用API Excel读取以及调用API的代 ...

  10. 抛出异常-throws和throw

    package com.mpp.test; import java.util.Scanner; public class TryDemoFour { public static void main(S ...