UVa 572:oil Deposits Search Topic

Source: Internet
Author: User
Tags bool printf stdin

Topic Link:

Http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=105&page=show_ problem&problem=513

Topic Type: Search

Sample input:

1 1
*
3 5
*@*@*
**@**
*@*@*
1
8 @@****@*
5 5
****@
*@@*@ *@**@
@@@*@ @@**@
0 0

Sample output:

0
1
2
2

Analysis:

This topic can be said to be one of the most basic topics in search. To find out how many blocks are connected together, so, in turn, enumerate, when you encounter the @ time to search, with deep search, wide search all line, the purpose is to connect the @ are marked as visited.

The following is a code that uses DFS (deep search) and BFS (extensive search).

Code 1:DFS

#include <iostream> #include <cstdio> #include <cstring> using namespace std;  
int m,n;  
int dir[8][2] = {{ -1,0},{-1,1},{0,1},{1,1}, {1,0},{1,-1},{0,-1},{-1,-1}};  
BOOL vis[105][105];  
     
Char map[105][105];  
        void Dfs (int x, int y) {for (int i=0; i<8; ++i) {int dx=x+dir[i][0], dy=y+dir[i][1]; if (dx>=0 && dx<m && dy>=0 && dy<n && map[dx][dy]== ' @ ' &&!vis[dx][dy  
            ] {Vis[dx][dy] = true;  
        DFS (dx, dy);  
int main () {#ifdef local freopen ("Input.txt", "R", stdin);  
    #endif int i,j; while (scanf ("%d%d%*c", &m,&n) && m && N) {for (i=0; i<m; ++i) gets (map[i  
        ]);  
        memset (Vis, 0, sizeof (VIS));  
        int cnt=0; For (i=0 i<m; ++i) {for (j=0; j<n; ++j) {if (map[i][j]== ' @ ' &&!vis[i][j]) { 
                    ++cnt;  
                DFS (I, j);  
    } printf ("%d\n", CNT);  
return 0; }

This article URL address: http://www.bianceng.cn/Programming/sjjg/201410/45526.htm

Code 2:

The above is a recursive way to DFS, but if the amount of data is large, recursive method is likely to overflow the risk!

Here's how to simulate stacks instead of recursively writing DFS

#include <iostream> #include <cstdio> #include <cstring> #include <stack> using namespace std  
;  
int m,n;  
int dir[8][2] = {{ -1,0},{-1,1},{0,1},{1,1}, {1,0},{1,-1},{0,-1},{-1,-1}};  
BOOL vis[105][105];  
Char map[105][105];  
struct node{int x,y;  
     
stack<node>st;  
    void Dfs (int x, int y) {while (!st.empty ()) St.pop ();  
    Node temp;  
    temp.x = x, temp.y = y;  
    St.push (temp);  
        while (!st.empty ()) {temp = St.top ();  
        St.pop ();  
            for (int i=0; i<8; ++i) {int dx=temp.x+dir[i][0], dy=temp.y+dir[i][1]; if (dx>=0 && dx<m && dy>=0 && dy<n && map[dx][dy]== ' @ ' &&!vis[dx][dy  
                ] {Vis[dx][dy] = true;  
                Node T;  
                t.x = dx, t.y = dy;  
            St.push (t); int main () {#ifdef local freopen ("Input.txt", "R", stdin);  
    #endif int i,j; while (scanf ("%d%d%*c", &m,&n) && m && N) {for (i=0; i<m; ++i) gets (map[i  
        ]);  
        memset (Vis, 0, sizeof (VIS));  
        int cnt=0;  
                    For (i=0 i<m; ++i) {for (j=0; j<n; ++j) {if (map[i][j]== ' @ ' &&!vis[i][j]) {  
                    ++cnt;  
                DFS (I, j);  
    } printf ("%d\n", CNT);  
return 0; }

Code 3:BFS

#include <iostream> #include <cstdio> #include <cstring> using namespace std;  
int m,n;  
int dir[8][2] = {{ -1,0},{-1,1},{0,1},{1,1}, {1,0},{1,-1},{0,-1},{-1,-1}};  
BOOL vis[105][105];  
Char map[105][105];  
struct node{int x,y;  
     
Node que[10000];  
    void BFs (int x,int y) {int front=0, rear=1;  
    que[0].x = x;  
    Que[0].y = y;  
        while (front < rear) {Node t=que[front++];  
            for (int i=0; i<8; ++i) {int dx=t.x+dir[i][0], dy=t.y+dir[i][1]; if (dx>=0 && dx<m && dy>=0 && dy<n && map[dx][dy]== ' @ ' &&!vis[dx][dy  
                ] {Vis[dx][dy] = true;  
                Node temp;  
                temp.x = dx, temp.y = dy;  
            que[rear++] = temp;  
int main () {#ifdef local freopen ("Input.txt", "R", stdin);  
    #endif int i,j; while (scanf ("%d%d%*c"),&m,&n) && m && N) {for (i=0; i<m; ++i) gets (map[i));  
        memset (Vis, 0, sizeof (VIS));  
        int cnt=0;  
                    For (i=0 i<m; ++i) {for (j=0; j<n; ++j) {if (map[i][j]== ' @ ' &&!vis[i][j]) {  
                    ++cnt;  
                BFS (i, j);  
    } printf ("%d\n", CNT);  
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.