1105D - Kilani and the Game - CodeForces Solution


dfs and similar graphs implementation shortest paths *1900

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
#define F first
#define S second
#define endl "\n";
#define shekoo ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);

using namespace std;
typedef long long ll;
typedef pair<int, int> pi;
typedef pair<ll, ll> pll;

const int N = 1e3+5, M = 12;
char arr[N][N];
int expansion[M], ans[M], n, m, p, cnt[M];
queue<pair<pi, int>> g[M];
int d[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};


bool valid(int x, int y){
    return x < n and x >= 0 and y < m and y >= 0 and arr[x][y] == '.';
}

void bfs(int playernum){
    cnt[playernum]++;
    while(!g[playernum].empty()){
        int x = g[playernum].front().F.F, y = g[playernum].front().F.S, step = g[playernum].front().S;
        if(step == (expansion[playernum]*cnt[playernum])) return;
        for(int i=0; i<4; i++){
            int xx = x + d[i][0], yy = y + d[i][1];
            if(valid(xx, yy)){
                arr[xx][yy] = char('0'+playernum);
                g[playernum].push({{xx, yy}, step+1});
            }
        }
        g[playernum].pop();
    }
}

void solve(){
    for(int i=1; i<=p; i++) bfs(i);
    bool f = 0;
    for(int i=1; i<=p; i++){
        if(!g[i].empty()){
            f = 1;
            break;
        }
    }
    if(f) solve();
    else{
        for(int i=0; i<n; i++){
            for(int j=0; j<m; j++){
//                cout << arr[i][j];
                if(arr[i][j] == '.' or arr[i][j] == '#') continue;
                ans[arr[i][j]-'0']++;
            }
//            cout << endl
        }
        for(int i=1; i<=p; i++) cout << ans[i] << " ";
    }
}

int main() {
    shekoo
    cin >> n >> m >> p;
    for(int i=1; i<=p; i++) cin >> expansion[i];
    for(int i=0; i<n; i++){
        for(int j=0; j<m; j++){
            cin >> arr[i][j];
            if(arr[i][j] == '.' or arr[i][j] == '#') continue;
            g[(arr[i][j]-'0')].push({{i, j}, 0});
        }
    }
    solve();
    return 0;
}


Comments

Submit
0 Comments
More Questions

67B - Restoration of the Permutation
1734A - Select Three Sticks
1734B - Bright Nice Brilliant
357B - Flag Day
937A - Olympiad
1075A - The King's Race
1734C - Removing Smallest Multiples
1004C - Sonya and Robots
922A - Cloning Toys
817A - Treasure Hunt
1136B - Nastya Is Playing Computer Games
1388A - Captain Flint and Crew Recruitment
592B - The Monster and the Squirrel
1081A - Definite Game
721C - Journey
1400A - String Similarity
1734E - Rectangular Congruence
1312D - Count the Arrays
424C - Magic Formulas
1730C - Minimum Notation
1730B - Meeting on the Line
1730A - Planets
302B - Eugeny and Play List
1730D - Prefixes and Suffixes
1515C - Phoenix and Towers
998A - Balloons
1734F - Zeros and Ones
1144B - Parity Alternated Deletions
92B - Binary Number
1144C - Two Shuffled Sequences