Advertisement
Hezov

Miting OJI X 2016

Jan 12th, 2025 (edited)
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.69 KB | None | 0 0
  1. #include <fstream>
  2. #include <iomanip>
  3. #include <queue>
  4. #include <cstring>
  5. using namespace std;
  6. ifstream cin("miting.in");
  7. ofstream cout("miting.out");
  8. queue<pair<int,int>> q;
  9. int di[] = {0,0,1,-1}, dj[] = {1,-1,0,0};
  10. char mat[101][101], cuv[11];
  11. int dp[11][11][61][61],visited[61][61];
  12. const int inf = 1e9;
  13. int cerinta, n ,m;
  14. pair<int,int> poz[11];
  15. int index(char c)
  16. {
  17.     return (strchr(cuv,c) - cuv) + 1 ;
  18. }
  19. int main()
  20. {
  21.     cin >> cerinta >> n >> m >> cuv;
  22.     for(int i = 1;i<=n;i++)
  23.         for(int j = 1;j<=m;j++)
  24.             cin >> mat[i][j];
  25.     if(cerinta == 1)
  26.     {
  27.         int xi=inf, yi=inf, xf=-inf, yf=-inf;
  28.         for(int i = 1;i<=n;i++)
  29.             for(int j = 1;j<=m;j++)
  30.                 if(mat[i][j]!= '#' && mat[i][j]!='_')
  31.                 {
  32.                     xi = min(xi,i); yi = min(yi,j);
  33.                     xf = max(xf,i); yf = max(yf,j);
  34.                 }
  35.         cout << (xf - xi + 1) * (yf - yi + 1);
  36.         return 0;
  37.     }
  38.     /// Initializam vectorul dp[][][][] cu valori mari..
  39.     int cuvLen = strlen(cuv);
  40.     for(int i = 1;i<=cuvLen;i++)
  41.         for(int j = 1;j<=cuvLen;j++)
  42.             for(int k = 1;k<=n;k++)
  43.                 for(int l = 1;l<=m;l++)
  44.                     dp[i][j][k][l] = 1e9;
  45.     /// Salvam pozitiile literelor in poz.
  46.     for(int i = 1;i<=n;i++)
  47.         for(int j = 1;j<=m;j++)
  48.             if(mat[i][j] != '#' && mat[i][j]!='_')
  49.                 poz[index(mat[i][j])] = {i,j};
  50.     /// Facem un lee pornind din fiecare litera.
  51.     for(int i = 1;i<=cuvLen;i++)
  52.     {
  53.         q.push(poz[i]);
  54.         int x = poz[i].first, y = poz[i].second;
  55.         dp[i][i][x][y] = 0;
  56.         while(!q.empty())
  57.         {
  58.             x = q.front().first;
  59.             y = q.front().second;
  60.             for(int k = 0; k<4;k++)
  61.             {
  62.                 int cx = x + di[k];
  63.                 int cy = y + dj[k];
  64.                 if(cx < 1 || cy < 1 || cx > n || cy > m)
  65.                     continue;
  66.                 if(mat[cx][cy] == '#' || dp[i][i][cx][cy] <= dp[i][i][x][y]) continue;
  67.                 dp[i][i][cx][cy] = dp[i][i][x][y] + 1;
  68.                 /// Daca elementul este deja in coada nu are rost sa il pun iar.
  69.                 if(!visited[cx][cy])
  70.                     q.push({cx,cy}),visited[cx][cy] = true;
  71.             }
  72.             q.pop();
  73.             visited[x][y] = false;
  74.         }
  75.     }
  76.     /// Afisarea acestor matrici :
  77.     /*for(int i = 1;i<=cuvLen;i++,cout<<'\n')
  78.     {
  79.         cout << "MAT " << i << ":\n";
  80.         for(int j = 1;j<=n;j++,cout<<'\n')
  81.              for(int k = 1;k<=m;k++)
  82.              {
  83.                  if(dp[i][i][j][k] == 1e9)
  84.                     cout <<setw(2) << -1 << ' ';
  85.                  else cout << setw(2) << dp[i][i][j][k] << ' ';
  86.              }
  87.     }*/
  88.  
  89.     /// Acum completam toate celelalte pozitii din dp.
  90.     for(int len = 2; len <= cuvLen;len++)
  91.     {
  92.         for(int i = 1;i <= cuvLen - len + 1; i++)
  93.         {
  94.             int j = i + len - 1;
  95.             for(int p = i; p < j;p++)
  96.                 for(int L = 1; L <= n; L++)
  97.                     for(int C = 1; C <= m; C++)
  98.                         if(mat[L][C]!='#')
  99.                             dp[i][j][L][C] = min(dp[i][j][L][C], dp[i][p][L][C] + dp[p+1][j][L][C]);
  100.             /// Afisarea matricilor fara a lua in calcul unirea dintre doua litere :
  101.             /*cout << "Matrice inainte de Lee\n";
  102.             cout << "len = " << len << ',' << " i = " << i << " ,j = " << j << '\n' << '\n';
  103.             for(int L = 1; L <= n; L++,cout << '\n')
  104.                 for(int C = 1; C<=m; C++)
  105.                 {
  106.                     if(dp[i][i][L][C] == 1e9)
  107.                         cout <<setw(2) << -1 << ' ';
  108.                     else cout << setw(2) << dp[i][j][L][C] << ' ';
  109.                 }
  110.             cout << '\n';*/
  111.  
  112.             /// Acum efectuam Lee-ul pentru a lua in calcul unirea dintre doua litere.
  113.             for(int L = 1; L <= n; L++)
  114.                 for(int C = 1; C <= m; C++)
  115.                 {
  116.                     if(mat[L][C] == '#')
  117.                         continue;
  118.                     q.push({L,C});
  119.                     while(!q.empty())
  120.                     {
  121.                         int x = q.front().first;
  122.                         int y = q.front().second;
  123.                         for(int k = 0; k<4; k++)
  124.                         {
  125.                             int cx = x + di[k];
  126.                             int cy = y + dj[k];
  127.                             if(cx < 1 || cy < 1 || cx > n || cy > m)
  128.                                 continue;
  129.                             if(mat[cx][cy] == '#' || dp[i][j][cx][cy] <= dp[i][j][x][y] + 1)
  130.                                 continue;
  131.                             dp[i][j][cx][cy] = dp[i][j][x][y] + 1;
  132.                             if(!visited[cx][cy])
  133.                                 q.push({cx,cy}),visited[cx][cy] = true;
  134.                         }
  135.                         q.pop();
  136.                         visited[x][y] = false;
  137.                     }
  138.                 }
  139.             /// Afisarile noilor matrici
  140.             /*cout << "Matrice dupa lee \n" << "len = " << len << ',' << " i = " << i << " ,j = " << j << '\n' << '\n';
  141.             for(int L = 1; L <= n; L++,cout << '\n')
  142.                 for(int C = 1; C<=m; C++)
  143.                 {
  144.                     if(dp[i][i][L][C] == 1e9)
  145.                         cout <<setw(2) << -1 << ' ';
  146.                     else cout << setw(2) << dp[i][j][L][C] << ' ';
  147.                 }
  148.             cout << '\n';*/
  149.  
  150.         }
  151.     }
  152.     /// Afisam raspunsul.
  153.     int sol = 1e9;
  154.     for(int i = 1;i<=n;i++)
  155.         for(int j = 1;j<=m;j++)
  156.             sol = min(sol,dp[1][cuvLen][i][j]);
  157.     cout << sol << '\n';
  158.  
  159.     return 0;
  160. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement