Advertisement
UNoobAle

UVA 776

Jan 20th, 2019
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.94 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std; typedef long long ll; typedef unsigned long long ull; const int mod = 1e9 + 7, N = 1e5 + 1, inf = INT_MAX;
  3. #define fi first
  4. #define se second
  5. #define pb push_back
  6. #define mp make_pair
  7. #define ep emplace_back
  8. //#define int ll
  9.  
  10. int n = 1, m = 0, cur, idx = 0, max_digit[111], ans[111][111];
  11. char x, a[111][111];
  12. bool visited[111][111];
  13. string s;
  14.  
  15. int cnt(int x)
  16. {
  17.     int ans = 0;
  18.     while (x)
  19.     {
  20.         x /= 10;
  21.         ans++;
  22.     }
  23.     return ans;
  24. }
  25.  
  26. void dfs(int i, int j)
  27. {
  28.     if (visited[i][j])
  29.     {
  30.         return;
  31.     }
  32.     if (i == 0 || j == 0 || i > n || j > m)
  33.     {
  34.         return;
  35.     }
  36.     if (a[i][j] != cur)
  37.     {
  38.         return;
  39.     }
  40.     ans[i][j] = idx;
  41.     visited[i][j] = 1;
  42.     dfs(i, j + 1);
  43.     dfs(i + 1, j);
  44.     dfs(i - 1, j);
  45.     dfs(i, j - 1);
  46.     dfs(i - 1, j + 1);
  47.     dfs(i + 1, j - 1);
  48.     dfs(i + 1, j + 1);
  49.     dfs(i - 1, j - 1);
  50. }
  51.  
  52. signed main()
  53. {
  54.     ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  55.     while (getline(cin, s) && s != "")
  56.     {
  57.         idx = 0;
  58.         memset(visited, 0, sizeof visited);
  59.         memset(max_digit, 0, sizeof max_digit);
  60.         n = 1, m = 0;
  61.         stringstream z(s);
  62.         while (z >> x)
  63.         {
  64.             a[n][++m] = x;
  65.         }
  66.         while (getline(cin, s) && s != "" && s != "%")
  67.         {
  68.             stringstream z(s);
  69.             n++;
  70.             int pos = 0;
  71.             while (z >> x)
  72.             {
  73.                 a[n][++pos] = x;
  74.             }
  75.         }
  76.         for (int i = 1; i <= n; i++)
  77.         {
  78.             for (int j = 1; j <= m; j++)
  79.             {
  80.                 if (!visited[i][j])
  81.                 {
  82.                     ++idx;
  83.                     cur = a[i][j];
  84.                     dfs(i, j);
  85.                 }
  86.             }
  87.         }
  88.         for (int j = 1; j <= m; j++)
  89.         {
  90.             for (int i = 1; i <= n; i++)
  91.             {
  92.                 if (i == 1) max_digit[j] = cnt(ans[i][j]);
  93.                 else max_digit[j] = max(max_digit[j], cnt(ans[i][j]));
  94.             }
  95.         }
  96.         for (int i = 1; i <= n; i++)
  97.         {
  98.             for (int j = 1; j <= m; j++)
  99.             {
  100.                 for (int k = 1; k <= max_digit[j] - cnt(ans[i][j]); k++)
  101.                 {
  102.                     cout << ' ';
  103.                 }
  104.                 cout << ans[i][j];
  105.                 if (j < m) cout << ' ';
  106.             }
  107.             cout << endl;
  108.         }
  109.         cout << "%\n";
  110.     }
  111.     return 0;
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement