当前位置:网站首页>ACM winter vacation training 4
ACM winter vacation training 4
2022-07-28 10:28:00 【MJ's Manon space】
DFS( Depth-first search )
Depth first search is a method that is often used in the early stage of crawler development . Its purpose is to achieve the leaf nodes of the searched structure ( That is, those that do not contain any hyperchains HTML file ) . In a HTML In file , When a hyperlink is selected , Linked HTML The file will perform a depth first search , That is, you must search a single chain completely before searching the rest of the hyperlink results . Depth first search HTML The hyperlink on the file can't go any further , Then go back to a HTML file , Continue to select the HTML Other hyperlinks in the file . When there are no more hyperchains to choose from , Indicates that the search is over .
The basic idea
The method of depth first traversal graph is , From some vertex in the picture v set out :
(1) Visit vertex v;
(2) Successively v Of the adjacent points that are not accessed , Depth first traversal of the graph ; Until the graph neutralizes v Vertices that have paths are all accessed ;
(3) If there are still vertices in the graph that are not accessed , From a vertex that has not been visited , Do depth first traversal again , Until all the vertices in the graph have been visited . Of course , When people have just mastered depth first search, they often use it to go through the maze . In fact, we have other ways , That's breadth first search (BFS).
Code example
int ans,sx,sy,W,H;
int dx[4]={
0,0,1,-1};
int dy[4]={
1,-1,0,0};
void dfs(int x,int y){
for(int i=0;i<=3;i++){
int tx=x+dx[i],ty=y+dy[i];
if(a[tx][ty]=='#'){
a[tx][ty]='.';
dfs(tx,ty);
}
}
}
Title Example
There is a rectangular room, covered with square tiles. Each tile is colored either red or black. A man is standing on a black tile. From a tile, he can move to one of four adjacent tiles. But he can’t move on red tiles, he can move only on black tiles.
Write a program to count the number of black tiles which he can reach by repeating the moves described above.
input
The input consists of multiple data sets. A data set starts with a line containing two positive integers W and H; W and H are the numbers of tiles in the x- and y- directions, respectively. W and H are not more than 20.
There are H more lines in the data set, each of which includes W characters. Each character represents the color of a tile as follows.
‘.’ - a black tile
‘#’ - a red tile
‘@’ - a man on a black tile(appears exactly once in a data set)
output
For each data set, your program should output a line which contains the number of tiles he can reach from the initial tile (including itself).
Sample Input
6 9
…#.
…#
…
…
…
…
…
#@…#
.#…#.
11 9
.#…
.#.#######.
.#.#…#.
.#.#.###.#.
.#.#…@#.#.
.#.#####.#.
.#…#.
.#########.
…
11 6
…#…#…#…
…#…#…#…
…#…#…###
…#…#…#@.
…#…#…#…
…#…#…#…
7 7
…#.#…
…#.#…
###.###
…@…
###.###
…#.#…
…#.#…
0 0
Sample Output
45
59
6
13
Code example
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <string>
#include <cmath>
using namespace std;
int w,h,sx,sy,ans;
char map[21][21];// Element container
int dx[4]={
0,0,1,-1};// Move up, down, left and right
int dy[4]={
1,-1,0,0};
bool v[21][21];// Check whether the cell has been detected
void dfs(int x,int y)//DFS Depth first search algorithm
{
for(int i=0;i<=3;i++)
{
int tx=x+dx[i],ty=y+dy[i];// Up, down, left and right coordinate positions
if(tx>=1&&tx<=h&&ty>=1&&ty<=w&&map[tx][ty]!='#'&&v[tx][ty]==0)// Determine whether it is the target element
{
ans++;// Increase in the number of results
v[tx][ty]=1;// Tag target cell has been detected
dfs(tx,ty);// Iterate through the remaining positions dfs
}
}
}
int main()// The main function
{
while(cin>>w>>h&&w!=0&&h!=0)// Input w,h value
{
ans=0;// initialization
for(int i=1;i<=h;i++)
for(int j=1;j<=w;j++)
{
cin>>map[i][j];
if(map[i][j]=='@')// Find the person's location
{
sx=i;
sy=j;
}
}
memset(v,0,sizeof(v));// initialization
v[sx][sy]=1;// Tag target cell has been detected
dfs(sx,sy);// quote dfs lookup
cout<<ans+1<<endl;// Output
}
return 0;
}
边栏推荐
- Get to know SuperMap idesktop for the first time
- 胡润发布2020中国芯片设计10强民营企业:华为海思竟然没有上榜!
- India plans to ban China Telecom equipment! Can we really do without Huawei and ZTE?
- 12. Double pointer -- merge two ordered linked lists
- Context values traps and how to avoid or mitigate these traps in go
- PL/SQL server语法详解
- string matching
- pt-kill 查询中包含中文字符 导致工具失效的排查
- 9. Delete nodes in the linked list
- 10 minute quick start EVs [play Huawei cloud]
猜你喜欢

7. Dichotomy -- find a set of repeated or ordered but rotating arrays

C语言 二级指针详解及示例代码

利用正则表达式从文件路径中匹配文件名

SQL Server 2016 learning record - Data Definition

多线程与高并发(三)—— 源码解析 AQS 原理

CentOS7下安装mysql5.7

最短路专题

Typora tutorial

14. Double pointer - the container that holds the most water

Get to know SuperMap idesktop for the first time
随机推荐
(1) Summary of machine learning concepts
JVM principle
集群为什么需要root权限
指令系统超全知识点详解
2021-10-13arx
Double pointer technique
用K-means聚类分类不同行业的关税模型
在mt6735中添加新的开机logo与开\关机动画
Huawei takes a 10% stake in fullerene technology, a graphene material manufacturer
【栈的应用】--- 中缀表达式转后缀表达式
Elk real time log analysis platform
中芯国际科创板IPO顺利过会,市值有望突破2000亿!
Leetcode -- minimum number of rotation array
ZTE: 5nm 5g base station chip is being introduced!
C语言 输入带空格的字符串
机器学习--手写英文字母3--工程特点
10. The penultimate node in the linked list
SQL Server 2016学习记录 --- 连接查询
Detailed explanation of super complete knowledge points of instruction system
Uni app advanced life cycle