AcWing - 95 - 费解的开关 = bfs

https://www.acwing.com/problem/content/97/

看了一下感觉可以暴力做,踩了一些坑终于过了。

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;

int cur;

int set_ij(int u, int i, int j) {
    int cur = u;
    cur ^= 1 << (i * 5 + j);
    if(i - 1 >= 0)
        cur ^= 1 << ((i - 1) * 5 + j);
    if(j - 1 >= 0)
        cur ^= 1 << (i * 5 + j - 1);
    if(i + 1 < 5)
        cur ^= 1 << ((i + 1) * 5 + j);
    if(j + 1 < 5)
        cur ^= 1 << (i * 5 + j + 1);
    return cur;
}

char vis[1 << 25];

int Q[1 << 20], front, back;

void bfs1() {
    vis[cur] = 0;
    front = 1;
    back = 0;
    Q[++back] = cur;
    while(front <= back) {
        int u = Q[front++];
        if(vis[u] < 2) {
            for(int i = 0; i < 5; ++i) {
                for(int j = 0; j < 5; ++j) {
                    int tmp = set_ij(u, i, j);
                    if(vis[tmp] == -1) {
                        vis[tmp] = vis[u] + 1;
                        Q[++back] = tmp;
                    }
                }
            }
        }
    }
}

char vis2[1 << 25];

int suc;
void bfs2() {
    vis2[cur] = 0;
    front = 1;
    back = 0;
    Q[++back] = cur;
    while(front <= back) {
        int u = Q[front++];
        if(vis2[u] < 4) {
            for(int i = 0; i < 5; ++i) {
                for(int j = 0; j < 5; ++j) {
                    int tmp = set_ij(u, i, j);
                    if(vis2[tmp] == -1) {
                        vis2[tmp] = vis2[u] + 1;
                        Q[++back] = tmp;
                    }
                }
            }
        }
    }
}


int main() {
#ifdef Yinku
    freopen("Yinku.in", "r", stdin);
#endif // Yinku

    for(int i = 0; i < 5; ++i) {
        for(int j = 0; j < 5; ++j) {
            cur |= 1 << (i * 5 + j);
        }
    }
    memset(vis2, -1, sizeof(vis2));
    bfs2();

    int T;
    scanf("%d", &T);

    memset(vis, -1, sizeof(vis));
    while(T--) {
        cur = 0;
        for(int i = 0; i < 5; ++i) {
            for(int j = 0; j < 5; ++j) {
                int tmp;
                scanf("%1d", &tmp);
                if(tmp)
                    cur |= 1 << (i * 5 + j);
            }
        }
        bfs1();
        suc = 1e9;
        front = 1;
        while(front <= back) {
            int u = Q[front++];
            if(vis2[u] != -1)
                suc = min(suc, vis[u] + vis2[u]);
            vis[u] = -1;
        }
        if(suc == 1e9)
            suc = -1;
        printf("%d\n", suc);
    }
}

事实证明预处理可以预处理5步甚至6步,输出队列的back就知道了,back*25就是预处理的复杂度。考虑到重复状态特别多,所以预处理的步数可以上升。

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;

int cur;

int set_ij(int u, int i, int j) {
    int cur = u;
    cur ^= 1 << (i * 5 + j);
    if(i - 1 >= 0)
        cur ^= 1 << ((i - 1) * 5 + j);
    if(j - 1 >= 0)
        cur ^= 1 << (i * 5 + j - 1);
    if(i + 1 < 5)
        cur ^= 1 << ((i + 1) * 5 + j);
    if(j + 1 < 5)
        cur ^= 1 << (i * 5 + j + 1);
    return cur;
}

int Q[1 << 20], front, back;

char vis2[1 << 25];

int suc;
void bfs2() {
    vis2[cur] = 0;
    front = 1;
    back = 0;
    Q[++back] = cur;
    while(front <= back) {
        int u = Q[front++];
        if(vis2[u] < 6) {
            for(int i = 0; i < 5; ++i) {
                for(int j = 0; j < 5; ++j) {
                    int tmp = set_ij(u, i, j);
                    if(vis2[tmp] == -1) {
                        vis2[tmp] = vis2[u] + 1;
                        Q[++back] = tmp;
                    }
                }
            }
        }
    }
}


int main() {
#ifdef Yinku
    freopen("Yinku.in", "r", stdin);
#endif // Yinku

    for(int i = 0; i < 5; ++i) {
        for(int j = 0; j < 5; ++j) {
            cur |= 1 << (i * 5 + j);
        }
    }
    memset(vis2, -1, sizeof(vis2));
    bfs2();

    int T;
    scanf("%d", &T);
    while(T--) {
        cur = 0;
        for(int i = 0; i < 5; ++i) {
            for(int j = 0; j < 5; ++j) {
                int tmp;
                scanf("%1d", &tmp);
                if(tmp)
                    cur |= 1 << (i * 5 + j);
            }
        }
        suc = vis2[cur];
        printf("%d\n", suc);
    }
}

AcWing - 95 - 费解的开关 = bfs

上一篇:WX通讯路打裙V(twingo-liu)


下一篇:MdispSelectWindow (Mil 函数描述)