Submission #941081

# Submission time Handle Problem Language Result Execution time Memory
941081 2024-03-08T06:35:31 Z KaleemRazaSyed Portals (BOI14_portals) C++17
20 / 100
28 ms 69980 KB
#include<iostream>
#include<vector>
#include<queue>
 
using namespace std;
 
const int N = 1e3 + 5;
char grid[N][N];
int left_[N][N], right_[N][N], up[N][N], down[N][N];

vector<int> G[N * N], port[N * N];
int dist[N * N];
int r, c, m;
int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, 1, -1};
 
bool valid(int x, int y)
{
  return (x > 0 && y > 0 && x <= r && y <= c && grid[x][y] != '#');
}
 
 
void bfs(int v, int u)
{
  for(int i = 0; i < N * N; i++)
    dist[i] = N * N + 10;
 
  queue<int> q;
  vector<int> portals;
  q.push(v);
  dist[v] = 0;
  while(q.size())
    {
      int w = q.front();
      q.pop();
      bool hold = false;

      for(int i : port[w])
	{
	  portals.push_back(i);
	  hold |= (w == i);
	}
      
      for(int k : G[w])
	{ 
	  if(dist[w] + 1 < dist[k])
	    {
	      dist[k] = dist[w] + 1;
	      q.push(k);
	    }
	}

      if(hold)
	{
	  for(int i : portals)
	    if(dist[w] + 1 < dist[i])
	      {
		dist[i] = dist[w] + 1;
		q.push(i);
	      }
	  portals.clear();
	}
    }

  //for(int i = 1; i <= r; i++)
  //for(int j = 1; j <= c; j++)
  //  cout << dist[i * m + j] << " \n"[j == c];
  
  cout << dist[u] << endl;
}
 
int main()
{
  cin >> r >> c;
  for(int i = 1; i <= r; i++)
    for(int j = 1; j <= c; j++)
      cin >> grid[i][j];
  
  m = c + 2;
  for(int i = 1; i <= r; i ++)
    for(int j = 1; j <= c; j ++)
      {
	if(grid[i][j] == '#') continue;
	int ij = i * m + j;
	for(int n = 0; n < 4; n++)
	  {
	    int ni = i + dx[n], nj = j + dy[n];
	    int nij = ni * m + nj;
	    if(valid(ni, nj))
	      G[ij].push_back(nij);
	  }
      }

  for(int i = 1; i <= r; i ++)
    {
      int p = 0;
      for(int j = 1; j <= c; j++)
	{
	  if(grid[i][j] == '#') p = j;
	  left_[i][j] = p;
	}

      int n = c + 1;
      for(int j = c; j >= 1; j--)
	{
	  if(grid[i][j] == '#') n = j;
	  right_[i][j] = n;
	}
    }
  

  for(int j = 1; j <= c; j ++)
    {
      int p = 0;
      for(int i = 1; i <= r; i ++)
	{
	  if(grid[i][j] == '#') p = i;
	  up[i][j] = p;
	}

      int n = r + 1;
      for(int i = r; i >= 1; i--)
	{
	  if(grid[i][j] == '#') n = i;
	  down[i][j] = n;
	}
    }

  for(int i = 1; i <= r; i ++)
    for(int j = 1; j <= c; j++)
      {
        if(grid[i][j] == '#') continue;

	int idx = i * m + j;
	
	int idx1 = i * m + left_[i][j] + 1;
	int idx2 = i * m + right_[i][j] - 1;
	int idx3 = (up[i][j] + 1) * m + j;
	int idx4 = (down[i][j] - 1) * m + j;
	
	port[idx].push_back(idx1);
	port[idx].push_back(idx2);
	port[idx].push_back(idx3);
	port[idx].push_back(idx4);
      }
	
  
  int v, u;
  for(int i = 1; i <= r; i++)
    for(int j = 1; j <= c; j++)
      if(grid[i][j] == 'S')
	v = i * m + j;
      else if(grid[i][j] == 'C')
	u = i * m + j;
  
  bfs(v, u);
  return 0;
}

Compilation message

portals.cpp: In function 'int main()':
portals.cpp:156:6: warning: 'u' may be used uninitialized in this function [-Wmaybe-uninitialized]
  156 |   bfs(v, u);
      |   ~~~^~~~~~
portals.cpp:156:6: warning: 'v' may be used uninitialized in this function [-Wmaybe-uninitialized]
# Verdict Execution time Memory Grader output
1 Correct 22 ms 59988 KB Output is correct
2 Correct 16 ms 60124 KB Output is correct
3 Correct 13 ms 59900 KB Output is correct
4 Correct 13 ms 59996 KB Output is correct
5 Correct 14 ms 59996 KB Output is correct
6 Correct 13 ms 59996 KB Output is correct
7 Correct 13 ms 59996 KB Output is correct
8 Correct 14 ms 59996 KB Output is correct
9 Correct 13 ms 59992 KB Output is correct
10 Incorrect 14 ms 59996 KB Output isn't correct
11 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 13 ms 59996 KB Output is correct
2 Correct 15 ms 59992 KB Output is correct
3 Correct 13 ms 59996 KB Output is correct
4 Correct 14 ms 59996 KB Output is correct
5 Correct 14 ms 59996 KB Output is correct
6 Correct 13 ms 59896 KB Output is correct
7 Correct 13 ms 59996 KB Output is correct
8 Correct 13 ms 60108 KB Output is correct
9 Correct 15 ms 62348 KB Output is correct
10 Incorrect 15 ms 62300 KB Output isn't correct
11 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 15 ms 59932 KB Output is correct
2 Correct 14 ms 59992 KB Output is correct
3 Correct 13 ms 59936 KB Output is correct
4 Correct 13 ms 59996 KB Output is correct
5 Correct 21 ms 69920 KB Output is correct
6 Correct 22 ms 69720 KB Output is correct
7 Correct 28 ms 69980 KB Output is correct
8 Correct 22 ms 69980 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 59992 KB Output is correct
2 Correct 13 ms 59996 KB Output is correct
3 Correct 13 ms 59996 KB Output is correct
4 Correct 13 ms 59996 KB Output is correct
5 Correct 13 ms 59996 KB Output is correct
6 Correct 13 ms 60116 KB Output is correct
7 Correct 13 ms 59996 KB Output is correct
8 Correct 13 ms 60084 KB Output is correct
9 Correct 16 ms 62300 KB Output is correct
10 Incorrect 14 ms 62300 KB Output isn't correct
11 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 14 ms 59996 KB Output is correct
2 Correct 15 ms 59992 KB Output is correct
3 Correct 14 ms 59992 KB Output is correct
4 Correct 17 ms 59996 KB Output is correct
5 Correct 14 ms 59992 KB Output is correct
6 Correct 15 ms 59992 KB Output is correct
7 Correct 13 ms 60000 KB Output is correct
8 Correct 14 ms 59996 KB Output is correct
9 Correct 14 ms 62296 KB Output is correct
10 Incorrect 15 ms 62296 KB Output isn't correct
11 Halted 0 ms 0 KB -