Submission #465740

# Submission time Handle Problem Language Result Execution time Memory
465740 2021-08-16T17:38:26 Z MohamedAliSaidane Portals (BOI14_portals) C++14
0 / 100
1 ms 588 KB
#include <bits/stdc++.h>
using namespace std;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef long long ll;
typedef pair<ll,ll> pll;
typedef tuple<int,int,int> ti;
typedef unsigned long long ull;
typedef long double ld;
typedef vector<ll> vll;
typedef pair<ld,ld> pld;
#define pb push_back
#define popb pop_back()
#define pf push_front
#define popf pop_front
#define ff first
#define ss second
#define MOD (int)(1e9  + 7)
#define INF (ll) (1e18)
#define all(v) (v).begin(),(v).end()
ll gcd(ll a , ll b) {return b ? gcd(b , a % b) : a ;}
ll lcm(ll a , ll b) {return (a * b) / gcd(a , b);}
ld pointdist(ld x, ld y, ld point) { return ((x-point)*(y-point))/abs(x-y); }
//ld dist(ld x, ld y, ld a, ld b){ return sqrt((x-a)*(x-a) + (y-b)*(y-b)); }
const int nx[4] = {0, 0, 1, -1}, ny[4] = {1, -1, 0, 0}; //East, West, South, North+
////////////******SOLUTION******\\\\\\\\\\\

const int MAX_R = 1004;
char mat[MAX_R][MAX_R];
int r, c;
pii start;
pii arv;
int dist[MAX_R][MAX_R];
int near[MAX_R][MAX_R];
int psa[4][MAX_R][MAX_R];

void build()
{
    int sof = 1;
    for(int i = 0; i < r; i ++)
    {
        sof = 1;
        for(int j = 0; j < c; j ++)
        {
            if(mat[i][j] == '#')
                sof = 0;
            psa[0][i][j] = sof;
            sof ++;
        }
    }

    for(int i = 0; i < r; i ++)
    {
        sof = 1;
        for(int j = c-1; j >= 0; j --)
        {
            if(mat[i][j] == '#')
                sof = 0;
            psa[1][i][j] = sof;
            sof ++;
        }
    }

    for(int j = 0; j < c; j ++)
    {
        sof = 1;
        for(int i = 0; i < r; i ++)
        {
            if(mat[i][j] == '#')
                sof = 0;
            psa[2][i][j] = sof;
            sof ++;
        }
    }

    for(int j = 0; j < c; j ++)
    {
        sof = 1;
        for(int i = r-1; i >= 0; i --)
        {
            if(mat[i][j] == '#')
                sof = 0;
            psa[3][i][j] = sof;
            sof ++;
        }
    }
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    cin >> r >> c;
    for(int i = 0; i <r; i ++)
    {
        for(int j = 0; j < c; j ++)
        {
            cin >> mat[i][j];
            if(mat[i][j] == 'S')
                start = {i,j};
            if(mat[i][j] == 'C')
            arv = {i, j};
            dist[i][j] = MOD;
            near[i][j] = MOD;
        }
    }
    build();
    set<pair<int,pii>> pq;
    pq.insert({0,start});
    int ans = MOD;
    while(!pq.empty())
    {
        pair<int,pii> u = *(pq.begin());
        pq.erase(pq.begin());
        int d = u.ff;
        pii coord = u.ss;
        int x = u.ss.ff;
        int y = u.ss.ss;
        //cout << x << ' ' << y << ' ' << d  << '\n' << flush;
        if(x == arv.ff && y == arv.ss)
        {
            cout << d;
            return 0;
        }
        dist[x][y] = d;
        bool wall = false;
        vector<pii> wls;
        int near = MOD;
        for(int i=  0; i < 4; i ++)
        {
            int newx = nx[i] + x;
            int newy = ny[i] + y;
            if(newx >= 0 && newx < r && newy >= 0 && newy < c
               && dist[newx][newy] > d + 1 && mat[newx][newy] != '#')
            {
                pq.insert({d+1,{newx,newy}});
                dist[newx][newy] = d + 1;
            }
            near = min(near,psa[i][x][y]);
            wls.pb({x+nx[i]*psa[i][x][y],y+ny[i]*psa[i][x][y]});
        }
        for(auto e: wls)
        {
            int a = e.ff;
            int b = e.ss;
            if(dist[a][b] > d +near)
            pq.insert({d+near,e});
            dist[a][b] = min(dist[a][b],d+near);
        }
    }
    //cout << "fin sans res\n";
    return 0;
}
/*
TEST CASE:
3 7
S##...#
.......
.#...C#

Identify problem diagram: Brute force, Greedy, Dynamic Programming, Divide and Conquer
Reformulate the problem into something more theoretical
!!!!! IMPLICIT GRAPH ??????
!!!!! PAY ATTENTION TO THE CONSTRAINTS: DP nD ? BF ? BITMASKING ?
!!!!! SOLVE THE SUBTASKS FIRST: IT'S TOTALLY OK NOT TO SOLVE THE PROBLEM ENTIRELY
Search for multiple approaches: select the seemingly optimal one
Remember that you're the king of CP
Change your approach
Imagine Corner cases before submitting
Don't spend too much time on the problem: move on !
*/

Compilation message

portals.cpp:26:1: warning: multi-line comment [-Wcomment]
   26 | ////////////******SOLUTION******\\\\\\\\\\\
      | ^
portals.cpp: In function 'int main()':
portals.cpp:116:13: warning: variable 'coord' set but not used [-Wunused-but-set-variable]
  116 |         pii coord = u.ss;
      |             ^~~~~
portals.cpp:126:14: warning: unused variable 'wall' [-Wunused-variable]
  126 |         bool wall = false;
      |              ^~~~
portals.cpp:110:9: warning: unused variable 'ans' [-Wunused-variable]
  110 |     int ans = MOD;
      |         ^~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 584 KB Output is correct
3 Incorrect 1 ms 588 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 332 KB Output is correct
2 Correct 1 ms 588 KB Output is correct
3 Incorrect 1 ms 580 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 588 KB Output is correct
3 Incorrect 1 ms 588 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 332 KB Output is correct
2 Correct 1 ms 588 KB Output is correct
3 Incorrect 1 ms 588 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 332 KB Output is correct
2 Correct 0 ms 588 KB Output is correct
3 Incorrect 1 ms 588 KB Output isn't correct
4 Halted 0 ms 0 KB -