zoukankan      html  css  js  c++  java
  • ZOJ 0188

    Property Distribution

    题目看不懂,不过看输入输出结果猜出来了,查看所以块得到数量,DFS搜索下就可以了。

     1 #include <iostream>
     2 #include <stdio.h>
     3 #include <string.h>
     4 using namespace std;
     5 char a[110][110];
     6 int dx[] = {1, 0, -1, 0}, dy[] = {0, -1, 0, 1},  h, w, ans;
     7 bool vis[110][110];
     8 
     9 void dfs(int x, int y, char c){
    10     vis[x][y] = true;
    11     for(int i = 0; i < 4; i ++){
    12         int xx = x+dx[i], yy = y + dy[i];
    13         if(0 <= xx && xx < h && 0 <= yy && yy < w && a[xx][yy] == c && !vis[xx][yy]){
    14             dfs(xx,yy,c);
    15         }
    16     }
    17 }
    18 
    19 int main(){
    20     while(cin>>h>>w){
    21         if(h==0&w==0) break;
    22         memset(a,0,sizeof(a));
    23         memset(vis,false,sizeof(vis));
    24         ans = 0;
    25         // for(int i = 0; i < h; i ++) gets(a[i]);
    26         for(int i = 0; i < h; i ++)
    27         for(int j = 0; j < w; j ++)
    28         cin>>a[i][j];
    29         for(int i = 0; i < h; i ++){
    30             for(int j = 0; j < w; j ++){
    31                 if(!vis[i][j]){
    32                     ans++;
    33                     dfs(i,j,a[i][j]);
    34                 }
    35             }
    36         }
    37         cout << ans << endl;
    38     }
    39     return 0;
    40 }
  • 相关阅读:
    meanshift聚类的实现
    birch聚类算法
    DBSCAN聚类算法的实现
    discrete adaboost的C++实现
    kd-tree的实现
    红黑树的实现——插入
    24位位图转8位灰度图
    将RGB数据写入BMP位图文件
    splay树的实现
    AVL树的实现
  • 原文地址:https://www.cnblogs.com/xingkongyihao/p/6909039.html
Copyright © 2011-2022 走看看