首页 > 其他 > 详细

八连块

时间:2020-02-25 18:23:52      阅读:72      评论:0      收藏:0      [点我收藏+]
 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 
 4 const int MAX = 100;
 5 char arr[MAX][MAX];
 6 int n,m;
 7 
 8 void init();
 9 void solve();
10 void dfs(int x,int y);
11 
12 void init(){
13     cin>>n>>m;//输入行数列数 
14     for(int i=0;i<n;i++){
15         for(int j=0;j<m;j++){
16             cin>>arr[i][j];//输入“w 和 . ” 
17         }
18     }    
19     solve();
20 } 
21 
22 void solve(){
23     int result = 0;
24     for(int i=0;i<n;i++){
25         for(int j=0;j<m;j++){
26             if(arr[i][j] == w||arr[i][j] == W){
27                 dfs(i,j);//搜索(i,j)周围的 w 
28                 result++;
29             }
30         }
31     }
32     cout<<"八连块的数量为"<<result<<endl; 
33 }
34 
35 void dfs(int x,int y){
36     arr[x][y] = .;
37     //遍历(x,y)周围的八个位置,搜索 w 
38     for(int dx=-1;dx<=1;dx++){
39         for(int dy=-1;dy<=1;dy++){
40             int nx=x+dx;
41             int ny=y+dy;
42             if(nx>=0&&nx<=n&&ny>=0&&ny<=m&&(arr[nx][ny]==w||arr[nx][ny] == W)){
43                 dfs(nx,ny);
44             }
45         }
46     }
47     
48 }
49 
50 int main(){
51     init();
52     return 0;
53 }

 

八连块

原文:https://www.cnblogs.com/CodingLife-190505/p/12362675.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!