【HDU - 1241】Oil Deposits (连通块问题 属于求大海中的岛屿个数 类似问题)
題干:
?
The GeoSurvComp geologic survey company is responsible for detecting underground oil deposits. GeoSurvComp works with one large rectangular region of land at a time, and creates a grid that divides the land into numerous square plots. It then analyzes each plot separately, using sensing equipment to determine whether or not the plot contains oil. A plot containing oil is called a pocket. If two pockets are adjacent, then they are part of the same oil deposit. Oil deposits can be quite large and may contain numerous pockets. Your job is to determine how many different oil deposits are contained in a grid.?
InputThe input file contains one or more grids. Each grid begins with a line containing m and n, the number of rows and columns in the grid, separated by a single space. If m = 0 it signals the end of the input; otherwise 1 <= m <= 100 and 1 <= n <= 100. Following this are m lines of n characters each (not counting the end-of-line characters). Each character corresponds to one plot, and is either `*', representing the absence of oil, or `@', representing an oil pocket.?
OutputFor each grid, output the number of distinct oil deposits. Two different pockets are part of the same oil deposit if they are adjacent horizontally, vertically, or diagonally. An oil deposit will not contain more than 100 pockets.?
Sample Input
Sample Output
0 1 2 2?
解題報告:
?
? ? dfs水題,但是做題效率并不好。自己想想為什么。
錯誤代碼:(是我想多了、、、)(想法是如果周圍)
#include<bits/stdc++.h>using namespace std;char maze[105][105]; bool bk[105][105]; int main() {int cnt=1;int n,m;while(cin>>m>>n) {memset(bk,0,sizeof(bk) ) ;memset(maze,0,sizeof(maze) );?if(m==0) break;cnt=0;for(int i = 1; i<=m; i++)?scanf("%s",maze[i]+1);for(int i = 1; i<=m; i++) {for(int j = 1; j<=n; j++) {if(maze[i][j]=='*') continue;if(bk[i][j]==1) continue;if( !bk[i][j+1] && !bk[i+1][j+1] && !bk[i+1][j] && !bk[i+1][j-1] && !bk[i][j-1]&& !bk[i-1][j-1]&& !bk[i-1][j]&& !bk[i-1][j+1]){cnt++;if(maze[i][j+1]=='@') bk[i][j+1]=1;if(maze[i+1][j+1]=='@') bk[i+1][j+1]=1;if(maze[i+1][j]=='@') bk[i+1][j]=1;if(maze[i+1][j-1]=='@') bk[i+1][j-1]=1;if(maze[i][j-1]=='@') bk[i][j-1]=1;if(maze[i-1][j-1]=='@') bk[i-1][j-1]=1;if(maze[i-1][j]=='@') bk[i-1][j]=1;if(maze[i-1][j+1]=='@') bk[i-1][j+1]=1;}bk[i][j]=1;//別放到那個括號里面啊!!!?}}printf("%d\n",cnt);}return 0 ; } int cnt=1;int n,m;while(cin>>m>>n) {memset(bk,0,sizeof(bk) ) ;memset(maze,0,sizeof(maze) );?if(m==0) break;cnt=0;for(int i = 1; i<=m; i++)?scanf("%s",maze[i]+1);for(int i = 1; i<=m; i++) {for(int j = 1; j<=n; j++) {if(maze[i][j]=='*') continue;if(bk[i][j]==1) continue;if( !bk[i][j+1] && !bk[i+1][j+1] && !bk[i+1][j] && !bk[i+1][j-1] && !bk[i][j-1]&& !bk[i-1][j-1]&& !bk[i-1][j]&& !bk[i-1][j+1]){cnt++;if(maze[i][j+1]=='@') bk[i][j+1]=1;if(maze[i+1][j+1]=='@') bk[i+1][j+1]=1;if(maze[i+1][j]=='@') bk[i+1][j]=1;if(maze[i+1][j-1]=='@') bk[i+1][j-1]=1;if(maze[i][j-1]=='@') bk[i][j-1]=1;if(maze[i-1][j-1]=='@') bk[i-1][j-1]=1;if(maze[i-1][j]=='@') bk[i-1][j]=1;if(maze[i-1][j+1]=='@') bk[i-1][j+1]=1;}bk[i][j]=1;//別放到那個括號里面啊!!!?}}printf("%d\n",cnt);}return 0 ; }給一個樣例:
***#
*#*#
**#*
就不立了。
?
ac代碼:
#include<bits/stdc++.h>using namespace std;char maze[105][105]; bool bk[105][105]; int n,m; void dfs(int x,int y) {//屬于先深入再判斷型 。一般寫深搜都是for一個方向數組,然后先判斷,再進入型。 這種題用前者會減少代碼量畢竟沒有for循環了得一個一個的if就很麻煩 if(maze[x][y]!='@' || x<1 || y<1 || x>m || y>n) return; maze[x][y] = 'x';//這步很關鍵。 dfs(x-1, y-1); dfs(x-1, y); dfs(x-1, y+1); dfs(x, y-1); dfs(x, y+1); dfs(x+1, y-1); dfs(x+1, y); dfs(x+1, y+1); } int main() {int cnt=1;while(cin>>m>>n) {memset(bk,0,sizeof(bk) ) ;memset(maze,0,sizeof(maze) ); if(m==0) break;cnt=0;for(int i = 1; i<=m; i++) scanf("%s",maze[i]+1);for(int i = 1; i<=m; i++) {for(int j = 1; j<=n; j++) {if(maze[i][j]!='@') continue;//不要寫 maze[i][j]=='*'!!因為你這里面還有'x'出現啊 dfs(i,j); cnt++;}}printf("%d\n",cnt);}return 0 ; }?
總結:
?
? ? 1.雖然是一道水題但是還是有很多問題需要注意的。可能是因為太長時間沒寫深搜了吧,有點小手生。
? ? 2.本題的搜索屬于先深入再判斷型 。一般寫深搜(尤其是地圖問題)都是for一個方向數組,然后先判斷,再進入型。 這種題用前者會減少代碼量畢竟沒有for循環了得一個一個的if就很麻煩 。
? ? 3.不要想當然的用for循環!那樣是肯定有陷阱的!由給出的那個樣例你也可以發現用for遍歷和dfs深搜的最主要區別就是dfs可以跳躍的選擇我們想要的部分,而不是只能老老實實的挨個遍歷。dfs犧牲了空間(遞歸層數深了還有可能棧溢出)和效率(此題并沒有體現出來,因為相當于剪枝十分優雅)的同時也是有自己的優勢所在的!
? ? 4.連通塊問題可否通用?一個點很實用,那就是改原地圖,比如此題中走過的'@'就改為別的字符,這樣避免了開一個新的vis數組了!(或者用著色問題也可以解)
5.連通塊問題也可以用并查集來做!貼一個代碼:
????給定一個由1和0組成的二維字符數組,1代表陸地,0代表水。問被水包圍的連通陸地區域的個數。
class Solution { public:vector<int> pre;int count=0;int numIslands(vector<vector<char>>& grid) {if(grid.size()==0 || grid[0].size()==0)return 0;int row = grid.size();int col = grid[0].size();pre.resize(row*col+1,0);//對pre數組進行初始化for(int i=0;i<row;i++)for(int j=0;j<col;j++){int num = col*i+j;pre[num] = num;}//遍歷圖中的每個點for(int i=0;i<row;i++)for(int j=0;j<col;j++){if(grid[i][j]=='1'){int down=i+1,right=j+1;if(down<row && grid[down][j]=='1')join(col*i+j,col*down+j);if(right<col && grid[i][right]=='1')join(col*i+j,col*i+right);}}//再遍歷一次,計算islands的個數int ans = 0;for(int i=0;i<row;i++)for(int j=0;j<col;j++){int num = col*i+j;if(pre[num] == num && grid[i][j]=='1')ans++;}return ans;}//并,將聯通的點的pre設為同一個值void join(int x,int y){int fx=find(x);int fy=find(y);if(fx != fy)pre[fx] = fy;}//找到a的祖先,并且路徑壓縮int find(int a){if(pre[a] != a)pre[a] = find(pre[a]);return pre[a];} };?
貼一道類似題目 :
?
? ? 藍橋杯山東省賽習題9 全球變暖https://blog.csdn.net/qq_41289920/article/details/81056373
總結
以上是生活随笔為你收集整理的【HDU - 1241】Oil Deposits (连通块问题 属于求大海中的岛屿个数 类似问题)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【 HDU - 5363】Key Set
- 下一篇: 四年磨一剑!国产手机处理器新黑马杀出来了