zoukankan      html  css  js  c++  java
  • uva804 四分树ac

    //70ms 在输出格式卡了挺久

    #include <cstdio>
    #include <cmath>
    #include <cstring>
    const int maxn = 70;
    char gra[maxn][maxn];
    int count, arr[1 << 13];
    int n;
    int allthesame(int r, int c, int w)//返回1全黑,-1全白,0既有黑也有白
    {
     char col = gra[r][c];
     for (int ir = r;ir < r + w;ir++)
      for (int ic = c;ic < c + w;ic++)
       if (gra[ir][ic] != col)return 0;//灰色
     if (col == '0')return -1;//白色
     return 1;//黑色
    }
    void dfs(int r, int c, int w, int road)
    {
     int ret_value = allthesame(r, c, w);
     if (ret_value == 1) { arr[count++] = road; return; }
     if (ret_value == -1)return;
     int depth = 0, t = road;
     while (t)
     {
      t /= 5;depth++;
     }
     t = (int)pow(5, depth);
     dfs(r, c, w / 2, road + t);
     dfs(r, c + w / 2, w / 2, road + t * 2);
     dfs(r + w / 2, c, w / 2, road + t * 3);
     dfs(r + w / 2, c + w / 2, w / 2, road + t * 4);
    }
    bool read()
    {
     count = 0;
     scanf("%d", &n);
     if (!n)return false;
     if (n > 0)
     {
      for (int i = 0;i < n;i++)
       scanf("%s", gra[i]);
     }
     else {
      int t;
      while (scanf("%d", &t) == 1 && t != -1)
       arr[count++] = t;
     }
     return true;
    }
    void bubblesort(int arr[], int count)
    {
     for (int i = 0;i < count - 1;i++)
     {
      for (int j = 0;j < count-1;j++)
      {
       if (arr[j] > arr[j + 1])
       {
        int t = arr[j];
        arr[j] = arr[j + 1];
        arr[j + 1] = t;
       }
      }
     }
    }
    void print()
    {
     if (n > 0)
     {
      bubblesort(arr, count);
      //if(count)printf("%d", arr[0]);
      for (int i = 0;i < count;i++)
      {
       printf("%d", arr[i]);
       if ((i + 1) % 12 == 0||i==count-1)printf("
    ");//这波输出格式很给力
       else printf(" ");
      }
      //if(count)printf("
    ");
      printf("Total number of black nodes = %d
    ", count);
     }
     else {
      for (int i = 0;i < -n;i++)
      {
       printf("%s
    ", gra[i]);
       /*for (int j = 0;j<-n;j++)
        printf("%c", gra[i][j]);
       printf("
    ");*/
      }
     
     }
    }
    const int dr[] = { 0,0,1,1 };
    const int dc[] = { 0,1,0,1 };
    void draw(int u)
    {
     int r = 0, c = 0, w = -n;
     while (u)
     {
      w /= 2;
      r += dr[u % 5 - 1] * w;
      c += dc[u % 5 - 1] * w;
      u /= 5;
     }
     for (int i = r;i < r + w;i++)
      for (int j = c;j < c + w;j++)
       gra[i][j] = '*';
    }
    void Draw()
    {
     memset(gra, '.', sizeof(gra));
     for (int i = 0;i < -n;i++)
      gra[i][-n] = '';
     for (int i = 0;i < count;i++)
     {
      draw(arr[i]);
     }
    }
    int main()
    {
     int kase = 0;
     while (read())
     {
      if(kase)printf("
    ");
      printf("Image %d
    ", ++kase);
      if (n > 0)dfs(0, 0, n, 0);
      else Draw();
      print();
      
     }
     return 0;
    }
  • 相关阅读:
    Python—字符编码转换、函数基本操作
    零散知识点
    Python—集合的操作、文件的操作
    Python—字典的操作
    Python—字符串的操作
    Spring基础—— 在 Spring Config 中使用外部属性文件
    Spring基础—— Bean 的作用域
    Spring基础——在 IOC 容器中 Bean 之间的关系
    Spring基础——在 Spring Config 文件中基于 XML 的 Bean 的自动装配
    Spring基础——一个简单的例子
  • 原文地址:https://www.cnblogs.com/schsb/p/7901162.html
Copyright © 2011-2022 走看看