Connected Blocks dfs搜索题
来源:互联网 发布:淘宝网店运营推广方案 编辑:程序博客网 时间:2024/06/08 16:14
Problem Description
We have a map consist of n * m points. In the map, there are two type points. One is ‘@’, another is ‘#’.
Then you need to count the connected blocks in the map.
But we have two way to count the blocks,so you need to count twice.
The first way, if two points ‘@’ which are adjacent to each other, they are in the same block.
The second way, if two points ‘@’ which are adjacent or diagonal to each other, they are in the same block.
Input
The first line contains an integer T(T<=20), then T cases follow.
In each case:
The first line contains 2 integer: n (1<=n<=100) and m(1 <= m <= 100), represent the rows and columns.
Then n line contains m characters. Each character represent a point in the map.
Output
For each case,output one line contains two integers, means the number of blocks in two ways.
Sample Input
2
2 3
@##
#@@
4 4
##@#
###@
@#@#
@###
Sample Output
2 1
4 2
Hint
In the first example,By the first way, the first block is (1, 1), the second block is (2, 2) and (2, 3).By the
second way, (1, 1), (2, 2) and (2, 3) are in the same block.
So the answer is 2 1.
// Copyright (c) 2017 , Shawbert. Shaw// All rights reserved.//// Filename: 数@//// Description: dfs//// Version: 1.0// Created: 2017/12/18/20点21分// Compiler: g++//// Author: 孙毓霄 Shaw#include <stdio.h>#include <stdlib.h>#include <string.h>#define maxn 103int vis[maxn][maxn];char mat[maxn][maxn];int n,m;int dx1[4] = {0,0,-1,1}, dy1[4] = {1,-1,0,0};int dx2[8] = {1,1,-1,-1,0,0,-1,1},dy2[8] = {-1,1,1,-1,1,-1,0,0};void dfs1(int i,int j){ if(i < 0 || i >= n || j < 0 || j >= m) return ; if(vis[i][j] || mat[i][j] == '#') return ; vis[i][j] = 1; int direct; for(direct = 0; direct < 4; direct++) { dfs1(i + dx1[direct],j + dy1[direct]); }}void dfs2(int i,int j){ if(i < 0 || i >= n || j < 0 || j >= m) return ; if(vis[i][j] || mat[i][j] == '#') return ; vis[i][j] = 1; int direct; for(direct = 0; direct < 8; direct++) { dfs2(i + dx2[direct],j + dy2[direct]); }}int main(){ int i,j; int cnt1,cnt2; int t; scanf("%d",&t); while(t--) { cnt1 = 0,cnt2 = 0; scanf("%d%d",&n,&m); for(i = 0; i < n; i++) { scanf("%s",mat[i]); } memset(vis,0,sizeof(vis)); for(i = 0; i < n; i++) { for(j = 0; j < m; j++) { if(mat[i][j] == '@' && vis[i][j] == 0) { cnt1 ++; dfs1(i,j); } } } memset(vis,0,sizeof(vis)); for(i = 0; i < n; i++) { for(j = 0; j < m; j++) { if(mat[i][j] == '@' && vis[i][j] == 0) { cnt2 ++; dfs2(i,j); } } } printf("%d %d\n",cnt1,cnt2); } return 0;}
- Connected Blocks dfs搜索题
- DFS搜索基础题
- Connected Cells in a Grid(dfs)
- Plato's Blocks(DFS+模拟)
- 搜索 dfs
- dfs搜索
- DFS搜索
- 搜索dfs
- (简单搜索)Blocks (P2363)
- HDU 1312(搜索题,DFS)
- HDU 1241(搜索题,DFS)
- HDU 1010(搜索题,DFS+剪枝)
- HDU 1016(搜索题,DFS)
- 模板题-----DFS深度搜索 HDU2952
- 棋盘问题+POJ+dfs搜索题
- hdu2102 (dfs)搜索水题
- poj1010——dfs搜索题
- poj1011——剪枝dfs搜索题
- jqueryUI的下载以及引用
- JMS(三)--ActiveMQ简单的HelloWorld实例
- 安装完YUI Compressor后 sublime ctrl+b控制台不输出
- TensorFlow实战(三)CNN
- Mysql优化之limit
- Connected Blocks dfs搜索题
- 初识Tomcat
- 对SQL语句的基础知识总结一(DML的增删改查)
- 剑指offer关于链表的一些操作(java)
- leetcode 583. Delete Operation for Two Strings 最长公共子串 + DP动态规划
- Echarts柱状图与折线图的基本使用
- 343434
- 栈及其应用
- angularjs商品列表(增删改查)