• poj1324Holedox Moving搜索


      刚开始想 stl水过去 ,一直超时。坑的是 这题贪吃蛇,头不能向尾部移动,后来手跑了样例才发现。。

    二进制位压判重,然后就普通的bfs 搞。对于那些30ms 过的很ym。。

    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <climits>
    #include <string>
    #include <iostream>
    #include <map>
    #include <cstdlib>
    #include <list>
    #include <set>
    #include <queue>
    #include <stack>
    #include<math.h>
    using namespace std;
    int dx[] = { -1, 1, 0, 0 };
    int dy[] = { 0, 0, -1, 1 };
    int n, m, l;
    bool vis[21][21][1 << 14];
    int dis[40][40];
    int Map[40][40];
    const int maxn = 1111111;
    int xxx[maxn]; int yyy[maxn]; int st[maxn];
    int q[maxn];
    
    int gao(int state, int x, int y, int x2, int y2)
    {
        int xiaojiji;
        if (x2 >= 1 && x2 <= n&&y2 >= 1 && y2 <= m&&!Map[x2][y2]) xiaojiji = 1; else return 0;
        int x1 = x; int y1 = y;
        for (int i = 0; i < l - 1; i++){
            int pos = i * 2; int a = state&(1 << pos); int b = state&(1 << (pos + 1));
            if (a) a = 1; if (b) b = 1;
            int t = b * 2 + a;
            x1 = x1 + dx[t]; y1 = y1 + dy[t];
            if (x1 == x2&&y1 == y2) return 0;
        }
        return 1;
    }
    int gao1(int state, int x, int y, int x2, int y2)
    {
        int gg = state;
        gg &= ~(1 << ((l - 1) * 2 - 1));
        gg &= ~(1 << ((l - 1) * 2 - 2));
        gg <<= 2;
        if (x - x2 == 1) gg |= 1;
        if (y - y2 == -1) gg |= 2;
        if (y - y2 == 1) gg |= 1, gg |= 2;
        return gg;
    }
    int gaomaoxian(int x, int y, int state)
    {
        memset(vis, 0, sizeof(vis));
        memset(dis, 0, sizeof(dis));
        vis[x][y][state] = 1;
        int l = 0, r = 0; int ans = 0;
        xxx[ans] = x; yyy[ans] = y; st[ans] = state;
        q[r++] = ans++;
        while (l<r){
            int cur = q[l++]; int xx = xxx[cur]; int yy = yyy[cur]; int state1 = st[cur];
            if (xx == 1 && yy == 1) return dis[xx][yy];
            for (int i = 0; i < 4; i++){
                int x1 = xx + dx[i]; int y1 = yy + dy[i];
                if (!gao(state1, xx, yy, x1, y1)) continue;
                int state2 = gao1(state1, xx, yy, x1, y1);
                if (vis[x1][y1][state2]) continue;
                xxx[ans] = x1; yyy[ans] = y1; st[ans] = state2;
                q[r++] = ans++;
                vis[x1][y1][state2] = 1;
                dis[x1][y1] = dis[xx][yy] + 1;
            }
        }
        return -1;
    }
    int main()
    {
        int x, y, a, b, state, k;
        int Icase = 0;
        while (scanf("%d%d%d", &n, &m, &l), n || m || l){
            scanf("%d%d", &x, &y);
            int a1 = x; int b1 = y; state = 0;
            for (int i = 0; i < l - 1; i++){
                scanf("%d%d", &a, &b);
                int pos = i * 2;
                if (a - a1 == 1) state |= (1 << pos);
                if (b - b1 == -1) state |= (1 << (pos + 1));
                if (b - b1 == 1) state |= (1 << pos), state |= (1 << (pos + 1));
                a1 = a; b1 = b;
            }
            cin >> k;
            memset(Map, 0, sizeof(Map));
            for (int i = 0; i<k; i++){
                scanf("%d%d", &a, &b);
                Map[a][b] = 1;
            }
            printf("Case %d: ", ++Icase);
            printf("%d
    ", gaomaoxian(x, y, state));
        }
        return 0;
    
    }
  • 相关阅读:
    最小生成树与Prim算法
    图的存储——链式前向星
    *转载 Tarjan有向图详解
    图的连通性算法-Kosaraju
    最小花费8597
    PDF提取图片(错误纠正)
    字符串删除指定符号(不限位置)
    python迭代有限制,突破限制
    storm资源冲突
    storm单节点问题(转载)
  • 原文地址:https://www.cnblogs.com/yigexigua/p/3942376.html
Copyright © 2020-2023  润新知