Advertisement
Josif_tepe

Untitled

Dec 18th, 2021
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.45 KB | None | 0 0
  1. #include<iostream>
  2. #include <queue>
  3. #include <vector>
  4. using namespace std;
  5.  
  6. int n,m,kolku;
  7. int dr[1005] = {-1, 0, +1, 0};
  8. int dc[1005] = {0, +1, 0, -1};
  9. int vis[1005][1005];
  10. int s[10];
  11. int cnt[20];
  12. char mat[1005][1005];
  13. //koj polinja gi ima koj vo sekoj moment
  14. queue<int>Q[20];
  15.  
  16. int main()
  17. {
  18.     ios_base::sync_with_stdio(false);
  19.     cin>>n>>m>>kolku;
  20.     for(int i=0;i<kolku;i++)
  21.     {
  22.         cin>>s[i];
  23.     }
  24.     for(int i=0;i<n;i++)
  25.     {
  26.         for(int j=0;j<m;j++)
  27.         {
  28.             cin>>mat[i][j];
  29.             vis[i][j]=false;
  30.         }
  31.     }
  32.     for(int i=0;i<n;i++)
  33.     {
  34.         for(int j=0;j<m;j++)
  35.         {
  36.             if(isdigit(mat[i][j]))
  37.             {
  38.                 int x=mat[i][j]-'0';
  39.                 Q[x-1].push(i);
  40.                 Q[x-1].push(j);
  41.                 Q[x-1].push(s[x-1]);
  42.                 cnt[x-1]++;
  43.                 vis[i][j] = true;
  44.             }
  45.         }
  46.     }
  47.     while(true)
  48.     {
  49.        bool ok=false;
  50.        for(int i=0;i<kolku;i++)
  51.        {
  52.            if(!Q[i].empty())
  53.            {
  54.                ok=true;
  55.                break;
  56.            }
  57.        }
  58.         if(ok==false)
  59.         {
  60.             break;
  61.         }
  62.         for(int i=0;i<kolku;i++)
  63.         {
  64.             vector<pair<int,int>>v;
  65.  
  66.             while(!Q[i].empty())
  67.             {
  68.                 int r=Q[i].front();
  69.                 Q[i].pop();
  70.                 int c=Q[i].front();
  71.                 Q[i].pop();
  72.                 int d=Q[i].front();
  73.                 Q[i].pop();
  74. //                cout << i + 1 << " " << r + 1 << " " <<  c + 1 << " " << d <<endl;
  75.                 for(int x = 0;x<4;x++)
  76.                 {
  77.                     int nr= r+dr[x];
  78.                     int nc= c+dc[x];
  79.                     if(nr<n && nr>=0 && nc<m && nc>=0 && vis[nr][nc]==false && mat[nr][nc]=='.' && d - 1 >= 0)
  80.                     {
  81.                         Q[i].push(nr);
  82.                         Q[i].push(nc);
  83.                         Q[i].push(d-1);
  84.                         v.push_back({nr,nc});
  85.                         vis[nr][nc]=true;
  86.                         cnt[i]++;
  87.                     }
  88.                 }
  89.             }
  90.             for(int j=0;j<v.size();j++)
  91.             {
  92.                 Q[i].push(v[j].first);
  93.                 Q[i].push(v[j].second);
  94.                 Q[i].push(s[i]);
  95.             }
  96.         }
  97.     }
  98.     for(int i=0;i<kolku;i++)
  99.     {
  100.         cout<<cnt[i]<<" ";
  101.     }
  102.     return 0;
  103. }
  104.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement