Showing posts with label BFS. Show all posts
Showing posts with label BFS. Show all posts

Monday, March 4, 2024

[Toph] D. Mushroom Kingdom

 

Problem Link    : D. Mushroom Kingdom
Category        : Bitmask, Bfs
Contest         : Intra AIUB Programming Contest 2024 (Senior)

#include "bits/stdc++.h"
using namespace std;
#define int     long long int
#define endl    '\n'
 
using pii = pair<int, int>;
 
int dr[] = {1, -1, 0, 0}; // 4 Direction
int dc[] = {0, 0, 1, -1};
 
void task() {
    int n;
    cin >> n;
    vector<char> chars;
    vector<vector<char>> grid(n + 1, vector<char>(n + 1));
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
            cin >> grid[i][j];
            chars.push_back(grid[i][j]);
        }
    }
    pii src;
    cin >> src.first >> src.second;
    pii des;
    cin >> des.first >> des.second;
    sort(chars.begin(), chars.end());
    chars.erase(unique(chars.begin(), chars.end()), chars.end());
    int ans = 10;
    for (int mask = 0; mask < (1 << (int)chars.size()); mask++) {
        set<int> validChars;
        for (int i = 0; i < chars.size(); i++) {
            if (mask >> i & 1) {
                validChars.insert(chars[i]);
            }
        }
        vector<vector<bool>> visited(n + 1, vector<bool>(n + 1));
        queue<pii> pq;
        if (validChars.count(grid[src.first][src.second])) {
            pq.push(src);
            visited[src.first][src.second] = 1;
        }
        while (pq.size()) {
            pii cur = pq.front(); pq.pop();
            for (int k = 0; k < 4; k++) {
                int x = cur.first + dr[k];
                int y = cur.second + dc[k];
                if (x >= 1 and x <= n and y >= 1 and y <= n and visited[x][y] == 0 and validChars.count(grid[x][y])) {
                    pq.push(pii(x, y));
                    visited[x][y] = 1;
                }
            }
        }
        if (visited[des.first][des.second]) {
            ans = min(ans, (int)__builtin_popcount(mask));
        }
    }
    cout << ans << endl;
}
 
signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr); cout.tie(nullptr);
    cout.precision(12);
 
    bool FILEIO = true; string FILE = "in.txt";
    if (FILEIO and fopen(FILE.c_str(), "r")) {
        freopen(FILE.c_str(), "r", stdin);
    }
 
    int tc;
    cin >> tc;
    for (int tcase = 1; tcase <= tc; tcase++) {
        task();
    }
}
 

Saturday, February 2, 2019

[UVA] 10507 - Waking up brain

Author            : Dipu Kumar Mohanto 
                    CSE, Batch - 6
                    BRUR.
Problem Statement : 10507 - Waking up brain
Source            : UVA Online Judge
Category          : Graph Theory
Algorithm         : bfs
Verdict           : Accepted

  1. #include "bits/stdc++.h"  
  2.   
  3. using namespace std;  
  4.   
  5. #define vi                vector <int>  
  6. #define em                emplace_back  
  7.   
  8. static const int maxn = 30;  
  9.   
  10. vi graph[maxn];  
  11. int on[maxn], dis[maxn];  
  12. int tnode, tedge;  
  13. char s[5];  
  14. set <int> used[maxn];  
  15.   
  16. void init()  
  17. {  
  18.     for (int i = 0; i < maxn; i++)  
  19.     {  
  20.         graph[i].clear();  
  21.         on[i] = 0;  
  22.         used[i].clear();  
  23.     }  
  24. }  
  25.   
  26. int toint(char ch)  
  27. {  
  28.     return ch - 'A' + 1;  
  29. }  
  30.   
  31. int bfs(int x, int y, int z)  
  32. {  
  33.     on[x] = on[y] = on[z] = 1;  
  34.     queue <int> PQ;  
  35.     PQ.emplace(x);  
  36.     PQ.emplace(y);  
  37.     PQ.emplace(z);  
  38.     dis[x] = dis[y] = dis[z] = 0;  
  39.     int maxdis = 0;  
  40.     int vis = 3;  
  41.     while (!PQ.empty())  
  42.     {  
  43.         int u = PQ.front(); PQ.pop();  
  44.         for (int v : graph[u])  
  45.         {  
  46.             if (on[v]) continue;  
  47.             used[v].emplace(u);  
  48.             if (used[v].size() == 3)  
  49.             {  
  50.                 dis[v] = dis[u] + 1;  
  51.                 if (dis[v] > maxdis) maxdis = dis[v];  
  52.                 on[v] = 1;  
  53.                 vis++;  
  54.                 PQ.emplace(v);  
  55.             }  
  56.         }  
  57.     }  
  58.     if (vis < tnode) return -1;  
  59.     return maxdis;  
  60. }  
  61.   
  62. int main()  
  63. {  
  64.     while (scanf("%d", &tnode) == 1)  
  65.     {  
  66.         init();  
  67.         scanf("%d", &tedge);  
  68.         scanf("%s", s);  
  69.         int x = toint(s[0]);  
  70.         int y = toint(s[1]);  
  71.         int z = toint(s[2]);  
  72.         for (int e = 1; e <= tedge; e++)  
  73.         {  
  74.             scanf("%s", s);  
  75.             int u = toint(s[0]);  
  76.             int v = toint(s[1]);  
  77.             graph[u].em(v);  
  78.             graph[v].em(u);  
  79.         }  
  80.         int year = bfs(x, y, z);  
  81.         if (year == -1) printf("THIS BRAIN NEVER WAKES UP\n");  
  82.         else printf("WAKE UP IN, %d, YEARS\n", year);  
  83.     }  
  84. }  

Sunday, January 13, 2019

[Spoj] STAR3CAS - THE WEIRD STAIRCASE

Author            : Dipu Kumar Mohanto 
                    CSE, Batch - 6
                    BRUR.
Problem Statement : STAR3CAS - THE WEIRD STAIRCASE
Source            : Spoj
Category          : Graph Theory
Algorithm         : BFS
Verdict           : Accepted

  1. #include "bits/stdc++.h"  
  2.   
  3. using namespace std;  
  4.   
  5. #define FI              freopen("in.txt", "r", stdin)  
  6. #define FO              freopen("out.txt", "w", stdout)  
  7. #define FAST            ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL)  
  8.   
  9. #define FOR(i, n)       for (int i = 1; i <= n; i++)  
  10. #define For(i, n)       for (int i = 0; i < n; i++)  
  11. #define ROF(i, n)       for (int i = n; i >= 1; i--)  
  12. #define Rof(i, n)       for (int i = n-1; i >= 0; i--)  
  13. #define FORI(i, n)      for (auto i : n)  
  14.   
  15. #define ll              long long  
  16. #define ull             unsigned long long  
  17. #define vi              vector <int>  
  18. #define vl              vector <ll>  
  19. #define pii             pair <int, int>  
  20. #define pll             pair <ll, ll>  
  21. #define mk              make_pair  
  22. #define ff              first  
  23. #define ss              second  
  24. #define eb              emplace_back  
  25. #define em              emplace  
  26. #define pb              push_back  
  27. #define ppb             pop_back  
  28. #define All(a)          a.begin(), a.end()  
  29. #define memo(a, b)      memset(a, b, sizeof a)  
  30. #define Sort(a)         sort(All(a))  
  31. #define ED(a)           Sort(a), a.erase(unique(All(a)), a.end())  
  32. #define rev(a)          reverse(All(a))  
  33. #define sz(a)           (int)a.size()  
  34. #define max3(a, b, c)   max(a, max(b, c))  
  35. #define min3(a, b, c)   min(a, min(b, c))  
  36. #define maxAll(a)       *max_element(All(a))  
  37. #define minAll(a)       *min_element(All(a))  
  38. #define allUpper(a)     transform(All(a), a.begin(), :: toupper)  
  39. #define allLower(a)     transform(All(a), a.begin(), :: tolower)  
  40. #define endl            '\n'  
  41. #define nl              puts("")  
  42. #define ub              upper_bound  
  43. #define lb              lower_bound  
  44. #define Exp             exp(1.0)  
  45. #define PIE             2*acos(0.0)  
  46. #define Sin(a)          sin(((a)*PIE)/180.0)  
  47. #define EPS             1e-9  
  48.   
  49. #include "ext/pb_ds/assoc_container.hpp"  
  50. #include "ext/pb_ds/tree_policy.hpp"  
  51. using namespace __gnu_pbds;  
  52.   
  53. template <typename T> using orderset = tree <T, null_type, less <T>, rb_tree_tag, tree_order_statistics_node_update>;  
  54.   
  55. #include "ext/rope"  
  56. using namespace __gnu_cxx;  
  57.   
  58. // rope <int> Rope;  
  59.   
  60. // int dr[] = {1, -1, 0, 0}; // 4 Direction  
  61. // int dc[] = {0, 0, 1, -1};  
  62. // int dr[] = {0, 0, 1, -1, 1, 1, -1, -1}; // 8 Direction  
  63. // int dc[] = {1, -1, 0, 0, 1, -1, 1, -1};  
  64. // int dr[] = {-1, 1, -2, -2, -1, 1, 2, 2}; // knight Moves  
  65. // int dc[] = {-2, -2, -1, 1, 2, 2, 1, -1};  
  66.   
  67. #define trace1(x)                           cerr << #x << ": " << x << endl;  
  68. #define trace2(x, y)                        cerr << #x << ": " << x << " | " << #y << ": " << y << endl;  
  69. #define trace3(x, y, z)                     cerr << #x << ": " << x << " | " << #y << ": " << y << " | " << #z << ": " << z << endl;  
  70. #define trace4(a, b, c, d)                  cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << endl;  
  71. #define trace5(a, b, c, d, e)               cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << " | " << #e << ": " << e << endl;  
  72. #define trace6(a, b, c, d, e, f)            cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << " | " << #e << ": " << e << " | " << #f << ": " << f << endl;  
  73.   
  74. inline int setbit(int mask, int pos)        { return mask |= (1 << pos); }  
  75. inline int resetbit(int mask, int pos)      { return mask &= ~(1 << pos); }  
  76. inline int togglebit(int mask, int pos)     { return mask ^= (1 << pos); }  
  77. inline bool checkbit(int mask, int pos)     { return (bool)(mask & (1 << pos)); }  
  78.   
  79. #define popcount(mask)                       __builtin_popcount(mask) // count set bit  
  80. #define popcountLL(mask)                     __builtin_popcountll(mask) // for long long  
  81.   
  82. inline int read()                           { int a; scanf("%d", &a); return a; }  
  83. inline ll readLL()                          { ll a; scanf("%lld", &a); return a; }  
  84. inline double readDD()                      { double a; scanf("%lf", &a); return a; }  
  85.   
  86. template <typename T> string toString(T num) { stringstream ss; ss << num; return ss.str(); }  
  87. int toInt(string s)                          { int num; istringstream iss(s); iss >> num; return num;  }  
  88. ll toLLong(string s)                         { ll num; istringstream iss(s); iss >> num; return num; }  
  89.   
  90. #define inf             123456  
  91. #define mod             1000000007  
  92.   
  93. static const int maxn = 25;  
  94. static const int logn = 18;  
  95.   
  96. int stair[25];  
  97. int N;  
  98.   
  99. vector <int> graph[maxn];  
  100. int dist[25 + 5];  
  101.   
  102. inline int bfs(int src, int des)  
  103. {  
  104.       FOR(i, maxn) dist[i] = 0;  
  105.       queue <int> PQ;  
  106.       PQ.em(src);  
  107.       dist[src] = 0;  
  108.       while (!PQ.empty())  
  109.       {  
  110.             int u = PQ.front(); PQ.pop();  
  111.             for (int v : graph[u])  
  112.             {  
  113.                   if (dist[v]) continue;  
  114.                   dist[v] = dist[u] + 1;  
  115.                   PQ.em(v);  
  116.             }  
  117.       }  
  118.       if (dist[des] == 0) return -1;  
  119.       return dist[des];  
  120. }  
  121.   
  122. int main()  
  123. {  
  124.       //FI;  
  125.       int tc = read();  
  126.       FOR(tcase, tc)  
  127.       {  
  128.             N = read();  
  129.             FOR(i, N)  
  130.             {  
  131.                   stair[i] = read();  
  132.                   graph[i].eb(i+1);  
  133.                   if (stair[i] < 0)  
  134.                   {  
  135.                         int then = i + stair[i];  
  136.                         if (then >= 1) graph[i].eb(then);  
  137.                         continue;  
  138.                   }  
  139.                   int go = i + stair[i];  
  140.                   if (go > (N+1)) continue;  
  141.                   graph[i].eb(go);  
  142.             }  
  143.             printf("%d\n", bfs(1, N+1));  
  144.             For(i, maxn) graph[i].clear();  
  145.       }  
  146. }