zoukankan      html  css  js  c++  java
  • [Princeton Algorithm I] Programming Assignment 1: Percolation(带权并查集)

    题目链接:http://coursera.cs.princeton.edu/algs4/assignments/percolation.html

    题意:比较繁琐,就是说从第1行到第n行有没有一个连通块能连起来。复习考研码力变弱了太多,这题调了2h。带权并查集,行号越小权值越高就行了。

    使用方法:windows下编译运行,按住一个键不放。

      1 #include <stdio.h>
      2 #include <string.h>
      3 #include <stdlib.h>
      4 #include <conio.h>
      5 #include <time.h>
      6 
      7 const int dx[5] = {0, 0, 1, -1};
      8 const int dy[5] = {1, -1, 0, 0};
      9 
     10 static struct RandomGen {
     11     RandomGen() { srand((int)time(NULL)); }
     12     static int getRandom() { return (int)rand()*(int)rand()+(int)rand(); }
     13 }RandomGen;
     14 
     15 typedef struct Percolation {
     16     int* pre;
     17     int** _G;
     18     int n, m;
     19 
     20     Percolation() {}
     21     Percolation(int _n, int _m) : n(_n), m(_m) {
     22         int _size = _n * _m;
     23         pre = (int*)malloc(_size*sizeof(int));
     24         _G = (int**)malloc(_n*sizeof(int*));
     25         for(int i = 0; i < _size; i++) {
     26             pre[i] = i;
     27         }
     28         for(int i = 0; i < n; i++) {
     29             _G[i] = (int*)malloc(_m*sizeof(int));
     30             for(int j = 0; j < m; j++) {
     31                 _G[i][j] = 0;
     32             }
     33         }
     34     }
     35 
     36     int find(int x) {
     37         return x == pre[x] ? x : pre[x] = find(pre[x]);
     38     }
     39 
     40     void unite(int x, int y) {
     41         x = find(x), y = find(y);
     42         if(x > y) pre[x] = y;
     43         else pre[y] = x;
     44     }
     45 
     46     inline int getId(int x, int y) { return x * m + y; }
     47     inline int ok(int x, int y) { return x >= 0 && y >= 0 && x < n && y < m; }
     48 
     49     inline void update(int x, int y) {
     50         _G[x][y] = 1;
     51         for(int i = 0; i < 4; i++) {
     52             int xx = x + dx[i], yy = y + dy[i];
     53             if(!ok(xx, yy) || !_G[xx][yy]) continue;
     54             unite(getId(x, y), getId(xx, yy));
     55         }
     56     }
     57 }Percolation;
     58 
     59 typedef struct PrintControl {
     60     Percolation* perc;
     61 
     62     PrintControl() {}
     63     PrintControl(Percolation* _p) : perc(_p) {}
     64     
     65     inline char sign(int x, int y) {
     66         int id = perc->getId(x, y);
     67         if(perc->_G[x][y]) {
     68             if(perc->find(id) / perc->m == 0) return 'O';
     69             else return '.';
     70         }
     71         else return 'X';
     72     }
     73 
     74     void Print() {
     75         system("cls");
     76         for(int i = 0; i < perc->n; i++) {
     77             for(int j = 0; j < perc->m; j++) {
     78                 printf("%c", sign(i, j));
     79             }
     80             printf("
    ");
     81         }
     82         printf("
    ");
     83     }
     84 }PrintControl;
     85 
     86 typedef struct Simulator {
     87     int n, m, initSize;
     88     Percolation perc;
     89     PrintControl pc;
     90 
     91     Simulator() {}
     92     Simulator(int _n, int _m, int _i) : n(_n), m(_m), initSize(_i) {
     93         perc = Percolation(n, m);
     94         pc = PrintControl(&perc);
     95         while(_i--) update();
     96     }
     97 
     98     void update() {
     99         int x = RandomGen::getRandom() % n,
    100               y = RandomGen::getRandom() % m;
    101         perc.update(x, y);
    102     }
    103     
    104     void Semaphore() {
    105         update();
    106         pc.Print();
    107         getch();
    108     }
    109 }Simulator;
    110 
    111 signed main() {
    112     // freopen("out", "w", stdout);
    113     Simulator sim(20, 20, 5);
    114     sim.pc.Print();
    115     while(1) {
    116         sim.Semaphore();
    117     }
    118     return 0;
    119 }
  • 相关阅读:
    ubuntu配置bridge网桥
    openstack 手动安装版 功能测试
    BC一周年B
    重构摘要11_处理概括关系
    深入浅出Redis(二)高级特性:事务
    补:小玩文件1-统计文本文件里的字符个数
    【C】字符串,字符和字节(C与指针第9章)
    怎样对报表的參数控件赋值
    POJ-2240 -Arbitrage(Bellman)
    ExtJS学习-----------Ext.Object,ExtJS对javascript中的Object的扩展
  • 原文地址:https://www.cnblogs.com/kirai/p/8127525.html
Copyright © 2011-2022 走看看