답안 #465742

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
465742 2021-08-16T17:45:24 Z MohamedAliSaidane 포탈들 (BOI14_portals) C++14
100 / 100
565 ms 25760 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 = 0;
        for(int j = 0; j < c; j ++)
        {
            if(mat[i][j] == '#')
                sof = -1;
            psa[0][i][j] = sof;
            sof ++;
        }
    }

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

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

    for(int j = 0; j < c; j ++)
    {
        sof = 0;
        for(int i = r-1; i >= 0; i --)
        {
            if(mat[i][j] == '#')
                sof = -1;
            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]+1);
            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;
      |         ^~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 460 KB Output is correct
3 Correct 1 ms 588 KB Output is correct
4 Correct 1 ms 460 KB Output is correct
5 Correct 1 ms 588 KB Output is correct
6 Correct 1 ms 460 KB Output is correct
7 Correct 1 ms 588 KB Output is correct
8 Correct 1 ms 580 KB Output is correct
9 Correct 1 ms 460 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 332 KB Output is correct
2 Correct 1 ms 460 KB Output is correct
3 Correct 1 ms 588 KB Output is correct
4 Correct 1 ms 460 KB Output is correct
5 Correct 1 ms 588 KB Output is correct
6 Correct 1 ms 588 KB Output is correct
7 Correct 1 ms 460 KB Output is correct
8 Correct 1 ms 588 KB Output is correct
9 Correct 2 ms 1484 KB Output is correct
10 Correct 2 ms 1604 KB Output is correct
11 Correct 1 ms 1476 KB Output is correct
12 Correct 2 ms 1484 KB Output is correct
13 Correct 2 ms 1484 KB Output is correct
14 Correct 1 ms 456 KB Output is correct
15 Correct 2 ms 1484 KB Output is correct
16 Correct 1 ms 332 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 332 KB Output is correct
2 Correct 1 ms 460 KB Output is correct
3 Correct 1 ms 460 KB Output is correct
4 Correct 1 ms 588 KB Output is correct
5 Correct 11 ms 5308 KB Output is correct
6 Correct 12 ms 5264 KB Output is correct
7 Correct 10 ms 5196 KB Output is correct
8 Correct 11 ms 5268 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 460 KB Output is correct
3 Correct 1 ms 460 KB Output is correct
4 Correct 0 ms 460 KB Output is correct
5 Correct 1 ms 460 KB Output is correct
6 Correct 1 ms 460 KB Output is correct
7 Correct 1 ms 460 KB Output is correct
8 Correct 1 ms 588 KB Output is correct
9 Correct 2 ms 1484 KB Output is correct
10 Correct 2 ms 1484 KB Output is correct
11 Correct 2 ms 1484 KB Output is correct
12 Correct 2 ms 1484 KB Output is correct
13 Correct 2 ms 1484 KB Output is correct
14 Correct 12 ms 5196 KB Output is correct
15 Correct 12 ms 5196 KB Output is correct
16 Correct 10 ms 5196 KB Output is correct
17 Correct 12 ms 5268 KB Output is correct
18 Correct 14 ms 5324 KB Output is correct
19 Correct 4 ms 5196 KB Output is correct
20 Correct 11 ms 5268 KB Output is correct
21 Correct 11 ms 5248 KB Output is correct
22 Correct 11 ms 5196 KB Output is correct
23 Correct 13 ms 5196 KB Output is correct
24 Correct 15 ms 5284 KB Output is correct
25 Correct 0 ms 460 KB Output is correct
26 Correct 1 ms 1484 KB Output is correct
27 Correct 0 ms 332 KB Output is correct
28 Correct 9 ms 5196 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 332 KB Output is correct
2 Correct 1 ms 460 KB Output is correct
3 Correct 1 ms 460 KB Output is correct
4 Correct 1 ms 460 KB Output is correct
5 Correct 1 ms 460 KB Output is correct
6 Correct 1 ms 460 KB Output is correct
7 Correct 1 ms 460 KB Output is correct
8 Correct 1 ms 588 KB Output is correct
9 Correct 2 ms 1484 KB Output is correct
10 Correct 2 ms 1484 KB Output is correct
11 Correct 1 ms 1484 KB Output is correct
12 Correct 2 ms 1484 KB Output is correct
13 Correct 2 ms 1484 KB Output is correct
14 Correct 11 ms 5196 KB Output is correct
15 Correct 13 ms 5280 KB Output is correct
16 Correct 9 ms 5196 KB Output is correct
17 Correct 12 ms 5196 KB Output is correct
18 Correct 14 ms 5324 KB Output is correct
19 Correct 4 ms 5196 KB Output is correct
20 Correct 11 ms 5324 KB Output is correct
21 Correct 11 ms 5196 KB Output is correct
22 Correct 11 ms 5196 KB Output is correct
23 Correct 11 ms 5220 KB Output is correct
24 Correct 332 ms 25028 KB Output is correct
25 Correct 565 ms 25760 KB Output is correct
26 Correct 66 ms 25024 KB Output is correct
27 Correct 242 ms 25316 KB Output is correct
28 Correct 225 ms 24884 KB Output is correct
29 Correct 251 ms 24832 KB Output is correct
30 Correct 234 ms 24824 KB Output is correct
31 Correct 15 ms 5324 KB Output is correct
32 Correct 424 ms 24900 KB Output is correct
33 Correct 1 ms 460 KB Output is correct
34 Correct 1 ms 1484 KB Output is correct
35 Correct 223 ms 24904 KB Output is correct
36 Correct 1 ms 332 KB Output is correct
37 Correct 9 ms 5196 KB Output is correct
38 Correct 200 ms 24900 KB Output is correct
39 Correct 168 ms 24900 KB Output is correct