572-oil deposits |
11158 |
58.77% |
5511 |
95.30% |
Question link:
Http://uva.onlinejudge.org/index.php? Option = com_onlinejudge & Itemid = 8 & category = 105 & page = show_problem & problem = 513
Question type: Search
Sample input:
1 1*3 5*@*@***@***@*@*1 8@@****@*5 5****@*@@*@*@**@@@@*@@@**@0 0
Sample output:
0122
Analysis:
This question is one of the most basic questions in search. You need to find out how many pieces are connected together. Therefore, enumeration is performed in sequence. When @ is encountered, search is performed. You can perform deep search and extensive search, the purpose is to mark all connected @ as accessed.
The following code uses DFS (Deep Search) and BFS (wide 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;}
Code 2:
The above is a recursive DFS, but if the data size is large, the recursive method may pose a risk of stack overflow!
The following describes how to simulate the stack instead of writing the DFS recursively.
#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;}
-- The meaning of life is to give it meaning.
Original
Http://blog.csdn.net/shuangde800
,
D_double