Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <algorithm>
- #include <set>
- using namespace std;
- typedef long long ll;
- const int N = 107;
- const int K = N*N;
- const int D = 8;
- int dx[D] = { +1, +1, -1, -1, +2, +2, -2, -2 };
- int dy[D] = { +2, -2, +2, -2, +1, -1, +1, -1 };
- char s[N][N];
- int n, m;
- int st[N][N], fsz;
- int ps[N][N];
- struct MST {
- int p[8], pos, cnt;
- int x, y;
- void clear() {
- }
- } mstack[K];
- int mstack_l;
- #define MTOP mstack[mstack_l]
- void ms_push(int qx, int qy) {
- mstack_l++;
- MTOP.cnt = 0;
- MTOP.pos = 0;
- MTOP.x = qx;
- MTOP.y = qy;
- }
- void ms_pop() {
- mstack_l--;
- }
- bool is_opened(int x, int y) {
- if(x < 0 || y < 0 || x >= m || y >= n)
- return false;
- return s[y][x] != 'X' && st[x][y] == 0;
- }
- void try_push(int qx, int qy) {
- if(is_opened(qx, qy)) {
- ms_push(qx, qy);
- ps[qx][qy] = mstack_l;
- st[qx][qy] = 1;
- }
- }
- int main() {
- ios_base::sync_with_stdio(false); cin.tie(0);
- cin >> n >> m;
- for(int i = 0; i < n; ++i) {
- cin >> s[i];
- for(int j = 0; j < m; ++j) {
- if(s[i][j] == 'K') {
- s[i][j] = '.';
- try_push(j, i);
- }
- if(s[i][j] == '.') fsz++;
- }
- }
- while(mstack_l != 0 && mstack_l != fsz) {
- int cx = MTOP.x, cy = MTOP.y;
- int* cp = MTOP.p;
- int& ccnt = MTOP.cnt;
- int& cpos = MTOP.pos;
- if(st[cx][cy] == 1) {
- //cout << "[POINT : " << cx << " ; " << cy << "]" << endl;
- int cpc[8] = { 0 };
- for(int i = 0; i < 8; ++i) {
- if(!is_opened(cx+dx[i], cy+dy[i])) continue;
- cp[ccnt] = i;
- for(int j = 0; j < 8; ++j)
- if(is_opened(cx+dx[i]+dx[j], cy+dy[i]+dy[j]))
- ++cpc[i];
- if(cpc[i] != 0 || mstack_l+1 == fsz) ccnt++;
- }
- sort(cp, cp+ccnt, [&](int a, int b) {
- return cpc[a] < cpc[b];
- });
- /*
- for(int i = 0; i < ccnt; ++i) {
- cout << " [WAY : " << cx+dx[cp[i]] << " ; " << cy+dy[cp[i]] << "] => " << cpc[cp[i]] << endl;
- }
- */
- st[cx][cy] = 2;
- }
- if(st[cx][cy] == 2) {
- if(cpos == ccnt) {
- st[cx][cy] = 0;
- ms_pop();
- continue;
- }
- int nx = cx+dx[cp[cpos]];
- int ny = cy+dy[cp[cpos]];
- try_push(nx, ny);
- cpos++;
- continue;
- }
- }
- for(int i = 0; i < n; ++i) {
- for(int j = 0; j < m; ++j) {
- printf("%3d ", ps[j][i]);
- // cout << ps[j][i] << ' ';
- }
- cout << endl;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement