From e181b086b7356ccbb4841c218d50e4dacd2c97ae Mon Sep 17 00:00:00 2001
From: Bruno Freitas Tissei <bft15@inf.ufpr.br>
Date: Tue, 7 May 2019 21:23:31 -0300
Subject: [PATCH] Small fixes

Signed-off-by: Bruno Freitas Tissei <bft15@inf.ufpr.br>
---
 algorithms/graph/bfs.cpp         |  5 +-
 contests/Cadernaveis/URI1850.cpp | 80 +++++++++++++-------------------
 2 files changed, 34 insertions(+), 51 deletions(-)

diff --git a/algorithms/graph/bfs.cpp b/algorithms/graph/bfs.cpp
index 6736fa4..55c0ec8 100644
--- a/algorithms/graph/bfs.cpp
+++ b/algorithms/graph/bfs.cpp
@@ -27,11 +27,12 @@ struct BFS {
 
     while (!Q.empty()) {
       int v = Q.front(); Q.pop();
-      vis[v] = true;
 
       for (auto i : graph[v])
-        if (!vis[i])
+        if (!vis[i]) {
+          vis[i] = true;
           Q.push(i);
+        }
     }
   }
 };
diff --git a/contests/Cadernaveis/URI1850.cpp b/contests/Cadernaveis/URI1850.cpp
index e169ec3..ae800d4 100644
--- a/contests/Cadernaveis/URI1850.cpp
+++ b/contests/Cadernaveis/URI1850.cpp
@@ -18,83 +18,65 @@ using namespace std;
 
 typedef long long ll;
 typedef pair<int,int> ii;
-typedef pair<ll, int> lli;
-typedef pair<ii, lli> elem;
+typedef pair<ii,ii> elem;
 
-ll cont[110][110];
 int dx[] = {1, -1, 0, 0};
 int dy[] = {0, 0, 1, -1};
+int cont[110][110][1 << 8];
 
 int main() {
   ios::sync_with_stdio(0);
   cin.tie(0);
 
+  ii arya;
   string x;
-  vector<string> v;
-
-  int n, m;
-  ii arya, exit;
-  for (int lin = 0; cin >> x; ++lin) {
-    v.pb(x);
-    for (int i = 0; i < x.size(); ++i) {
-      if (x[i] == '@')
-        arya = {lin, i};
-      else if (x[i] == '*')
-        exit = {lin, i};
-    }
+  vector<string> mat;
 
-    n = lin;
-    m = x.size();
+  for (int i = 0; cin >> x; ++i) {
+    mat.pb(x);
+    for (int j = 0; j < x.size(); ++j)
+      if (x[j] == '@') 
+        arya = {i, j};
   }
-  n++;
 
-  mset(cont, -1);
+  int n = mat.size(), m = mat[0].size();
 
+  mset(cont, 0);
   queue<elem> Q;
-  Q.push({arya, {0LL, 0}});
-  int ans = -1;
+  Q.push({arya, {0, 0}});
+  cont[arya.fi][arya.se][0] = 1;
 
   while (!Q.empty()) {
     elem curr = Q.front(); Q.pop();
-    cont[curr.fi.fi][curr.fi.se] = curr.se.fi;
-
-    //for (int i = 0; i < n; ++i) {
-    //  for (int j = 0; j < m; ++j)
-    //    cout << cont[i][j] << " ";
-    //  cout << ende;
-    //}
-    //cout << ende;
-
-
-    if (curr.fi == exit) {
-      ans = curr.se.se;
-      break;
-    }
+    cont[curr.fi.fi][curr.fi.se][curr.se.fi] = 1;
+    
+    if (mat[curr.fi.fi][curr.fi.se] == '*')
+      return cout << curr.se.se << ende, 0;
 
     for (int i = 0; i < 4; ++i) {
       int x = curr.fi.fi + dx[i];
       int y = curr.fi.se + dy[i];
 
-      if (x >= 0 && x < n && y >= 0 && y < m && (cont[x][y] == -1 || __builtin_popcountll(cont[x][y]) < __builtin_popcountll(curr.se.fi)) && v[x][y] != '#') {
-        ll msk = curr.se.fi;
+      if (x >= 0 && x < n && y >= 0 && y < m && 
+          !cont[x][y][curr.se.fi] && mat[x][y] != '#') {
+        int msk = curr.se.fi;
 
-        if (v[x][y] >= 'a' && v[x][y] <= 'g') {
-          msk |= (1 << (v[x][y] - 'a'));
-        }
-
-        if (v[x][y] >= 'A' && v[x][y] <= 'G') {
-          if (msk & (1 << (v[x][y] - 'A')))
+        if (mat[x][y] >= 'A' && mat[x][y] <= 'G') {
+          if (msk & (1 << (mat[x][y] - 'A'))) {
+            //cont[x][y][msk] = 1;
             Q.push({{x, y}, {msk, curr.se.se + 1}});
-        } else
+          }
+        } else {
+          if (mat[x][y] >= 'a' && mat[x][y] <= 'g')
+            msk |= (1 << (mat[x][y] - 'a'));
+
+          //cont[x][y][msk] = 1;
           Q.push({{x, y}, {msk, curr.se.se + 1}});
+        }
       }
     }
   }
 
-  if (ans == -1)
-    cout << "--" << ende;
-  else
-    cout << ans << ende;
-
+  cout << "--" << ende;
   return 0;
 }
-- 
GitLab