HDU/HDOJ 1241 Oil Deposits (DFS) Deep Priority Search

Source: Internet
Author: User

This is equivalent to finding the number of connected subgraphs in the graph. The number of connected subgraphs in the graph is eight neighboring areas according to the question (top, bottom, left, right, and four diagonal)

Idea: give priority to depth, search for the eight neighborhoods of each vertex in the graph in sequence, and mark the access. Otherwise, the consequences will be very serious.

AC code: 46 MS 416 K speed is not slow, it may be because of many obstacles in the figure.


[Cpp]
# Include <iostream>
# Include <string>
# Include <cstdio>
# Include <cmath>
# Include <vector>
# Include <algorithm>
# Include <sstream>
# Include <cstdlib>
# Include <fstream>
# Include <queue>
Using namespace std;
Bool visit [110] [110]; // repetitive Access Control
Char maze [110] [110]; // Map
Int dir [8] [2] = {-}, {0,-1}, {-1,-1 }, {-}, {1,-1}, {}; // eight directions
Int sum, m, n, sx, sy;
Bool isbound (int a, int B) {// judge the Boundary
If (a <1 | a> m | B <1 | B> n) return true;
Return false;
}
Void dfs (int sx, int sy) {// perform a deep search for each vertex


For (int I = 0; I <8; I ++)
{
If (maze [sx + dir [I] [0] [sy + dir [I] [1] = '*') continue; // obstacle return
If (isbound (sx + dir [I] [0], sy + dir [I] [1]) continue; // return the Boundary
If (visit [sx + dir [I] [0] [sy + dir [I] [1]) continue; // accessed, return

Visit [sx + dir [I] [0] [sy + dir [I] [1] = 1; // mark accessed to prevent repeated access
Dfs (sx + dir [I] [0], sy + dir [I] [1]);

}
}
Int main ()
{
While (cin> m> n)
{
If (m = 0) break;
Memset (visit, 0, sizeof (visit ));
For (int I = 1; I <= m; I ++ ){
For (int j = 1; j <= n; j ++ ){
Cin> maze [I] [j];
}
}
Sum = 0;
For (int I = 1; I <= m; I ++) {// The conditions for Deep Search for each vertex are:
// 1: not an obstacle; 2: not accessed
For (int j = 1; j <= n; j ++ ){
If (maze [I] [j] = '@'&&! Visit [I] [j]) {visit [I] [j] = 1; dfs (I, j); sum ++ ;}
}
}
Cout <sum <endl;

}
 
Return 0;
 
}
 

# Include <iostream>
# Include <string>
# Include <cstdio>
# Include <cmath>
# Include <vector>
# Include <algorithm>
# Include <sstream>
# Include <cstdlib>
# Include <fstream>
# Include <queue>
Using namespace std;
Bool visit [110] [110]; // repetitive Access Control
Char maze [110] [110]; // Map
Int dir [8] [2] = {-}, {0,-1}, {-1,-1 }, {-}, {1,-1}, {}; // eight directions
Int sum, m, n, sx, sy;
Bool isbound (int a, int B) {// judge the Boundary
If (a <1 | a> m | B <1 | B> n) return true;
Return false;
}
Void dfs (int sx, int sy) {// perform a deep search for each vertex
 
 
For (int I = 0; I <8; I ++)
{
If (maze [sx + dir [I] [0] [sy + dir [I] [1] = '*') continue; // obstacle return
If (isbound (sx + dir [I] [0], sy + dir [I] [1]) continue; // return the Boundary
If (visit [sx + dir [I] [0] [sy + dir [I] [1]) continue; // accessed, return

Visit [sx + dir [I] [0] [sy + dir [I] [1] = 1; // mark accessed to prevent repeated access
Dfs (sx + dir [I] [0], sy + dir [I] [1]);
 
}
}
Int main ()
{
While (cin> m> n)
{
If (m = 0) break;
Memset (visit, 0, sizeof (visit ));
For (int I = 1; I <= m; I ++ ){
For (int j = 1; j <= n; j ++ ){
Cin> maze [I] [j];
}
}
Sum = 0;
For (int I = 1; I <= m; I ++) {// The conditions for Deep Search for each vertex are:
// 1: not an obstacle; 2: not accessed
For (int j = 1; j <= n; j ++ ){
If (maze [I] [j] = '@'&&! Visit [I] [j]) {visit [I] [j] = 1; dfs (I, j); sum ++ ;}
}
}
Cout <sum <endl;

}

Return 0;

}

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.