zoukankan      html  css  js  c++  java
  • 【HDOJ】1198 Farm Irrigation

    其实就是并查集,写麻烦了,同样的代码第一次提交wa了,第二次就过了。

      1 #include <stdio.h>
      2 #include <string.h>
      3 
      4 #define MAXNUM 55
      5 #define UP    0
      6 #define RIGHT 1
      7 #define DOWN  2
      8 #define LEFT  3
      9 
     10 char buf[MAXNUM][MAXNUM];
     11 int bin[MAXNUM*MAXNUM];
     12 char visit[MAXNUM*MAXNUM][MAXNUM*MAXNUM];
     13 
     14 int direct[4][2] = {{-1,0},{0,1},{1,0},{0,-1}};
     15                     // A        B        C          D          E
     16 int pipes[11][4] = {{1,0,0,1},{1,1,0,0},{0,0,1,1},{0,1,1,0}, {1,0,1,0},
     17                     // F        G        H          I          J
     18                     {0,1,0,1},{1,1,0,1},{1,0,1,1},{0,1,1,1}, {1,1,1,0}, {1,1,1,1}};
     19 
     20 int find(int x) {
     21     int r = x;
     22 
     23     while (r != bin[r])
     24         r = bin[r];
     25 
     26     return r;
     27 }
     28 
     29 void merge(int x, int y) {
     30     int fx, fy;
     31 
     32     fx = find(x);
     33     fy = find(y);
     34 
     35     if (fx != fy)
     36         bin[fx] = fy;
     37 }
     38 
     39 int main() {
     40     int n, m;
     41     int i, j, k, x, y, p, q, is, id, ns, nd;
     42 
     43     while (scanf("%d %d%*c", &n, &m) != EOF) {
     44         if (m<0 || n<0)
     45             break;
     46         for (i=0; i<n; ++i)
     47             scanf("%s", buf[i]);
     48         memset(visit, 0, sizeof(visit));
     49         for (i=0; i<n*m; ++i)
     50             bin[i] = i;
     51         for (i=0; i<n; ++i) {
     52             for (j=0; j<m; ++j) {
     53                 is = buf[i][j] - 'A';
     54                 ns = i*m+j;
     55                 for (k=0; k<4; ++k) {
     56                     x = i + direct[k][0];
     57                     y = j + direct[k][1];
     58                     if (x<0 || x>=n || y<0 || y>=m)
     59                         continue;
     60                     nd = x*m+y;
     61                     if (visit[ns][nd] || visit[nd][ns])
     62                         continue;
     63                     id = buf[x][y] - 'A';
     64                     switch (k) {
     65                     case UP:
     66                         p = pipes[is][UP];
     67                         q = pipes[id][DOWN];
     68                         break;
     69                     case RIGHT:
     70                         p = pipes[is][RIGHT];
     71                         q = pipes[id][LEFT];
     72                         break;
     73                     case DOWN:
     74                         p = pipes[is][DOWN];
     75                         q = pipes[id][UP];
     76                         break;
     77                     case LEFT:
     78                         p = pipes[is][LEFT];
     79                         q = pipes[id][RIGHT];
     80                         break;
     81                     default: ;
     82                     }
     83                     if (p && q)
     84                         merge(ns, nd);
     85                     visit[ns][nd] = 1;
     86                     visit[nd][ns] = 1;
     87                 }
     88             }
     89         }
     90         k = 0;
     91 
     92         for (i=0; i<n*m; ++i) {
     93             if (i == bin[i])
     94                 ++k;
     95         }
     96         printf("%d
    ", k);
     97     }
     98 
     99     return 0;
    100 }
  • 相关阅读:
    Spring bean作用域
    软件类说明文档排版建议
    fit_line_contour_xld拟合直线的五种算法的准确度比较
    .Net优秀开源(5)SqlSugar
    .NET[C#]中实现实体对象深拷贝(克隆/复制)的几种方法
    spring框架学习(14)AOP(中)
    .Net优秀开源(4)Castle.Core
    .Net优秀开源(3)Dapper
    .Net优秀开源(2)Autofac
    .Net优秀开源(1)
  • 原文地址:https://www.cnblogs.com/bombe1013/p/3700072.html
Copyright © 2011-2022 走看看