Description
Input
Output
Sample Input
Sample Output
代码:
#include<iostream> #include<string.h> #include<stdlib.h> #include<stdio.h> char map[105][105]; int m,n; using namespace std; void DFS(int i,int j) { if(map[i][j]==‘@‘) { map[i][j]=‘*‘; if(i-1>0&&map[i-1][j]==‘@‘) DFS(i-1,j); if(i+1<=m&&map[i+1][j]==‘@‘) DFS(i+1,j); if(j-1>0&&map[i][j-1]==‘@‘) DFS(i,j-1); if(j+1<=n&&map[i][j+1]==‘@‘) DFS(i,j+1); if(i-1>0&&j-1>0&&map[i-1][j-1]==‘@‘) DFS(i-1,j-1); if(i-1>0&&j+1<=n&&map[i-1][j+1]==‘@‘) DFS(i-1,j+1); if(i+1<=m&&j-1>0&&map[i+1][j-1]==‘@‘) DFS(i+1,j-1); if(i+1<=m&&j+1<=n&&map[i+1][j+1]==‘@‘) DFS(i+1,j+1); } } int main() { int i,j,count; while(scanf("%d%d",&m,&n)!=EOF) { count=0; if(m==0) break; for(i=1;i<=m;++i) { for(j=1;j<=n;++j) { cin>>map[i][j]; } } for(i=1;i<=m;++i) { for(j=1;j<=n;++j) { if(map[i][j]==‘@‘) { count++; DFS(i,j); } } } cout<<count<<endl; } return 0; }
//Oil Deposits #include <iostream> #include <stdio.h> using namespace std; char map[101][101]; int n, m, sum; void dfs(int i, int j) { //若该点不可行或越界,返回 if(map[i][j]!=‘@‘ || i<0 || j<0 || i>=m || j>=n) return; else //否则,标记该点为不可行,再往8个方向深搜 { map[i][j] = ‘!‘; dfs(i-1, j-1); dfs(i-1, j); dfs(i-1, j+1); dfs(i, j-1); dfs(i, j+1); dfs(i+1, j-1); dfs(i+1, j); dfs(i+1, j+1); } } int main() { int i, j; while(cin>>m>>n) { if(m==0 || n==0) break; sum = 0; for(i = 0; i < m; i++) for(j = 0; j < n; j++) cin>>map[i][j]; for(i = 0; i < m; i++) { for(j = 0; j < n; j++) { if(map[i][j] == ‘@‘) { dfs(i, j); sum++; } } } cout<<sum<<endl; } return 0; }
- Oil Deposits 深搜,就是所谓的dfs,布布扣,bubuko.com
原文:http://www.cnblogs.com/lipching/p/3868340.html