码迷,mamicode.com
首页 > 其他好文 > 详细

【HDOJ】1198 Farm Irrigation

时间:2014-07-22 23:13:53      阅读:310      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   java   color   io   

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

mamicode.com,码迷
  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\n", k);
 97     }
 98 
 99     return 0;
100 }
mamicode.com,码迷

 

【HDOJ】1198 Farm Irrigation,码迷,mamicode.com

【HDOJ】1198 Farm Irrigation

标签:style   blog   http   java   color   io   

原文地址:http://www.cnblogs.com/bombe1013/p/3700072.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!