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
    ");
        }
    }
  • 相关阅读:
    下载最新Silverlight 5 Beta客户端
    oracle数据库导入导出命令!
    使用SQL Server 2008提供的表分区向导
    Microsoft Visual Studio 2010 旗舰版下载地址
    用C#创建Windows服务(Windows Services)
    Socket通信:服务端发送安全策略到flash(c#)
    Microsoft Silverlight 4 Tools for Visual Studio 2010 下载地址
    Flex打印
    .NET中三种数据类型转换的区别:(type), type.Parse, Convert类
    JQUERY 常用方法大全
  • 原文地址:https://www.cnblogs.com/Dup4/p/9433142.html
Copyright © 2011-2022 走看看