当前位置:网站首页>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;
}
边栏推荐
- 在mt6735中添加新的开机logo与开\关机动画
- 中兴通讯:5nm 5G基站芯片正在技术导入!
- 13. Hash table - the first common node of two linked lists
- 10 minute quick start EVs [play Huawei cloud]
- 管道、管程、管态的区别
- Troubleshooting of tool failure caused by Chinese characters in PT kill query
- Chapter 1: cross end development of small programs of uniapp ----- create a uniapp project
- Bitwise and, or, XOR and other operation methods
- 简介
- Why does the cluster need root permission
猜你喜欢

Sword finger offer

SQL Server 2016 学习记录 --- 嵌套查询

Multithreading and high concurrency (III) -- source code analysis AQS principle

django-celery-redis异步发邮件
![[wechat applet] project practice - lottery application](/img/7b/a0545f077259b3dc971dc246813177.png)
[wechat applet] project practice - lottery application

IDEA创建我的第一个项目

配置树莓派,过程和遇到问题

CentOS7下安装mysql5.7

PHP生成二维码(学习)

Get to know SuperMap idesktop for the first time
随机推荐
Massive data topn problem
Record a parent-child project in idea, modify the name of project and module, and test it personally!
Database security - create login user + configure permissions [notes]
C语言 二级指针详解及示例代码
4.调整数组顺序使奇数位于偶数前面
CentOS7下安装mysql5.7
2. Output one of the repeated numbers in the array
PL/SQL server语法详解
SQL Server 2016 学习记录 --- 数据定义
中兴通讯总裁徐子阳:5nm芯片将在2021年推出
Use of Ogg parameter filter [urgent]
13、哈希表——两个链表第一个公共节点
SQL Server 2016 learning record - Data Definition
管道、管程、管态的区别
Why does the cluster need root permission
Can kingbasees v8r6 JDBC use VIP?
数据库安全 --- 创建登录名 用户+配置权限【笔记】
Sleeping barber problem
【微信小程序】项目实战—抽签应用
ACM寒假集训#5