# UVA 572 Search for a first-knowledge BFS DFS

Source: Internet
Author: User

Topic Type: Search

Sample input:

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

Sample output:

`0122`

Analysis:

This problem can be said to be one of the most basic problems in the search. To find out how many pieces are connected together, so, in order to enumerate, to encounter @ when the search, with deep search, wide search all line, the purpose is to connect the @ are marked as visited.

Below are the codes for DFS (deep search) and BFS (wide search)

DFS 1: Recursion

`#include <iostream> #include <cstring> #include <cstdlib>using namespace Std;int m,n,idx[105][105];  Char pic[105][105];void dfs (int r,int c,int ID) {    if (r<0 | | r>=m | | c<0 | | c>= n)   return; if (Idx[r][c] >0 | |  PIC[R][C]! = ' @ ') return; Idx[r][c] = id;for (int dr = -1;dr <= 1;dr++) {for (int dc = -1;DC <= 1;dc++) {if (dr! = 0 | | DC! = 0) dfs (r+dr,c+dc,id);}}} int main () {int I,j,cnt;while (cin>>m>>n,m&&n) {for (i=0;i<m;i++) {cin>>pic[i];} CNT = 0;memset (idx,0,sizeof (IDX));        for (i=0;i<m;i++)        {for (j=0;j<n;j++) {               if (idx[i][j]==0 && pic[i][j] = = ' @ ')   dfs (i,j,++cnt);}        }cout<<cnt<<endl;} return 0;}`
This method may have a stack overflow when the amount of data is large
DFS 2: Stacks

`#include <iostream> #include <cstring> #include <cstdlib> #include <stack>using namespace std; int M,n,idx[105][105];char pic[105][105];struct Node {int x;int y;}; stack<node> st;void dfs (int r,int c) {//if (r<0 | | r>=m | | c<0 | | c>= n) return;/if (Idx[r][c] & gt;0 | | PIC[R][C]! = ' @ ') return, while (!st.empty ()) St.pop ();    Node temp;temp.x = R;TEMP.Y = C;st.push (temp); Idx[r][c] = 1;while (!st.empty ()) {temp = St.top (); St.pop (); for (int dr. = -1;dr <= 1;dr++) {for (int dc = -1;DC <= 1;dc++) {int DRR = Dr+temp.x;int DCC = dc+temp.y;if ((d R! = 0 | | DC! = 0) && drr>=0 && drr<m && dcc>=0 && dcc<n && PIC[DRR][DCC] = = ' @ ' &&!IDX[DRR][DCC])//dfs (r+dr,c+dc,id); {Node T;IDX[DRR][DCC] =1;t.x = Drr;t.y = Dcc;st.push (t);}}}} int main () {int I,j,cnt;while (cin>>m>>n,m&&n) {for (i=0;i<m;i++) {cin>>pic[i];}        CNT = 0;memset (idx,0,sizeof (IDX)); for (i=0;i<m;i++) {for (j=0;j<n;j++) {if (idx[i][j]==0 && pic[i][j] = = ' @ ') {cnt++;   DFS (I,J); }}}cout<<cnt<<endl;} return 0;}`

BFS: (Queue Implementation) available STL can also array

`#include <iostream> #include <cstring> #include <cstdlib> #include <stack> #include <queue >using namespace Std;int M,n,idx[105][105];char pic[105][105];struct Node {int X;int y;};/ /stack<node> St;//queue<node> St;  Node que[105];void dfs (int r,int c) {//if (r<0 | | r>=m | | c<0 | | c>= n) return;/if (Idx[r][c] >0 | | PIC[R][C]! = ' @ ') return;//while (!st.empty ()) St.pop (); que[0].x = R;que[0].y = C;idx[r][c] = 1;int front=0;int rear=1;wh Ile (Front < rear) {Node temp=que[front++];for (int dr = -1;dr <= 1;dr++) {for (int dc = -1;DC <= 1;dc++) {in T DRR = dr+temp.x;int DCC = dc+temp.y;if ((dr! = 0 | | DC! = 0) && drr>=0 && drr<m && dcc>= 0 && dcc<n && pic[drr][dcc] = = ' @ ' &&!IDX[DRR][DCC]) {IDX[DRR][DCC] =1;que[rear].x = drr;que[r Ear].y = dcc;rear++;}}}} int main () {int I,j,cnt;while (cin>>m>>n,m&&n) {for (i=0;i<m;i++) {cin>>pic[i];} CNT = 0;memset (idx,0,sizeof (IDX)); for (i=0;i<m;i++) {for (j=0;j<n;j++) {if (idx[i][j]==0 && pic[i][j] = = ' @ ') {cnt+   +;   DFS (I,J); }}}cout<<cnt<<endl;} return 0;}`

UVA 572 Search for a first-knowledge BFS DFS

Related Keywords:

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.