zoukankan      html  css  js  c++  java
  • ZOJ

    题目链接

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3865

    思路

    一个迷宫题 但是每次的操作数和普通的迷宫题不一样

    0.按下按钮 即向当前方向盘的方向前进一格
    1.往左移动方向盘
    2.往右移动方向盘
    3.不动

    然后记录时间

    易知,可以从四个方向到达终点,最后从这四个方向找最小值就是答案

    当然要判断 不能通达的结果

    AC代码

    #include <cstdio>
    #include <cstring>
    #include <ctype.h>
    #include <cstdlib>
    #include <cmath>
    #include <climits>
    #include <ctime>
    #include <iostream>
    #include <algorithm>
    #include <deque>
    #include <vector>
    #include <queue>
    #include <string>
    #include <map>
    #include <stack>
    #include <set>
    #include <numeric>
    #include <sstream>
    #include <iomanip>
    #include <limits>
    
    #define CLR(a) memset(a, 0, sizeof(a))
    #define pb push_back
    
    using namespace std;
    typedef long long ll;
    typedef long double ld;
    typedef unsigned long long ull;
    typedef pair <int, int> pii;
    typedef pair <ll, ll> pll;
    typedef pair<string, int> psi;
    typedef pair<string, string> pss;
    
    const double PI = acos(-1);
    const double E = exp(1);
    const double eps = 1e-30;
    
    const int INF = 0x3f3f3f3f;
    const int maxn = 5e4 + 5;
    const int MOD = 1e9 + 7;
    
    char dis[4][4] =
    {
        'l', 'r', 'u', 'd',
        'd', 'l', 'r', 'u',
        'u', 'd', 'l', 'r',
        'r', 'u', 'd', 'l',
    };
    
    map <char, int> M;
    
    int Move[4][2]
    {
        -1, 0, // up
        1, 0, //down
        0,-1, // left
        0, 1, //right
    };
    
    char G[15][15];
    
    int n, m, p;
    
    int dp[12][12][4];
    int ans[12][12][4];
    
    struct Node
    {
        int x, y, d;
    }tmp;
    
    queue <Node> q;
    
    int sx, sy, ex, ey;
    
    bool ok(int x, int y, int d)
    {
        if (x < 0 || x >= n || y < 0 || y >= m)
            return false;
        if (G[x][y] == '*' || dp[x][y][d] != -1)
            return false;
        return true;
    }
    
    void bfs()
    {
        while (!q.empty())
        {
            int x = q.front().x;
            int y = q.front().y;
            int d = q.front().d;
            q.pop();
            if (x == ex && y == ey && ans[x][y][d] == -1)
                ans[x][y][d] = dp[x][y][d];
            int nx = x + Move[M[dis[dp[x][y][d] / p % 4][d]]][0];
            int ny = y + Move[M[dis[dp[x][y][d] / p % 4][d]]][1];
            // press botton
            if (ok(nx, ny, d))
            {
                dp[nx][ny][d] = dp[x][y][d] + 1;
                tmp.x = nx;
                tmp.y = ny;
                tmp.d = d;
                q.push(tmp);
            }
    
            // move left
            int nd = d - 1;
            if (nd < 0)
                nd = 3;
            if (ok(x, y, nd))
            {
                dp[x][y][nd] = dp[x][y][d] + 1;
                tmp.x = x;
                tmp.y = y;
                tmp.d = nd;
                q.push(tmp);
            }
    
            // move right
            nd = d + 1;
            if (nd > 3)
                nd = 0;
            if (ok(x, y, nd))
            {
                dp[x][y][nd] = dp[x][y][d] + 1;
                tmp.x = x;
                tmp.y = y;
                tmp.d = nd;
                q.push(tmp);
            }
    
            // stay
            if (dp[x][y][d] <= 200)
            {
                dp[x][y][d]++;
                tmp.x = x;
                tmp.y = y;
                tmp.d = d;
                q.push(tmp);
            }
        }
    }
    
    void init()
    {
        M['u'] = 0;
        M['d'] = 1;
        M['l'] = 2;
        M['r'] = 3;
    }
    
    int main()
    {
        init();
        int T;
        cin >> T;
        while (T--)
        {
            memset(dp, -1, sizeof(dp));
            memset(ans, -1, sizeof(ans));
            scanf("%d%d%d", &n, &m, &p);
            int x = -1, y = -1;
            for (int i = 0; i < n; i++)
            {
                scanf("%s", &G[i]);
                for (int j = 0; j < m; j++)
                {
                    if (G[i][j] == '@')
                    {
                        sx = i;
                        sy = j;
                    }
                    else if (G[i][j] == '$')
                    {
                        ex = i;
                        ey = j;
                    }
                }
            }
            dp[sx][sy][0] = 0;
            tmp.x = sx;
            tmp.y = sy;
            tmp.d = 0;
            q.push(tmp);
            bfs();
            int Ans = INF;
            for (int i = 0; i < 4; i++)
            {
                if (ans[ex][ey][i] != -1)
                    Ans = min(Ans, ans[ex][ey][i]);
            }
            if (Ans != INF)
                printf("%d
    ", Ans);
            else
                printf("YouBadbad
    ");
        }
    }
  • 相关阅读:
    Poj 1742 Coins(多重背包)
    Poj 2350 Above Average(精度控制)
    求二进制数中1的个数
    Poj 1659 Distance on Chessboard(国际象棋的走子规则)
    Poj 2411 Mondriaan's Dream(压缩矩阵DP)
    Poj 2136 Vertical Histogram(打印垂直直方图)
    Poj 1401 Factorial(计算N!尾数0的个数——质因数分解)
    poj 2390 Bank Interest(计算本利和)
    Poj 2533 Longest Ordered Subsequence(LIS)
    Poj 1887 Testing the CATCHER(LIS)
  • 原文地址:https://www.cnblogs.com/Dup4/p/9433142.html
Copyright © 2011-2022 走看看