题目描述:https://uva.onlinejudge.org/external/8/810.pdf

把一个骰子放在一个M x N的地图上,让他按照规定滚动,求滚回原点的最短路径。


思路:  记忆化搜索(我这里用的dfs深度优先搜索)


难点:

如何推导骰子的状态

我没有用打表,而是先用build_dice()初始化二维数组dice, 这样,当我们有骰子的顶面top,正面face, 就可以通过dice[top][face] 获得对应的右面,由此,就可以实现骰子的旋转。rotate()函数实现骰子的旋转,给它top, face, 和旋转的方向(前,后,左,右), 它会返回旋转后骰子的top, face.

          

记忆化搜索要多一个状态记录骰子的状态

说白了就是记录访问状态的数组vis要多两个维度分别记录top, face,因为top, face一旦固定,骰子就固定了

Note: 输出的格式也要稍微注意一下,锁进、末尾的逗号,换行要处理好。

#include <iostream>
#include <cstdio>
#include <vector>
#include <utility>
#include <cstring>
 
using namespace std;
const int MAXN = + ;
int b[MAXN][MAXN], R, C, sx, sy, st, sf, len;
int dx[] = {, -, , }, dy[] = {, , -, }, dice[][];   
bool vis[MAXN][MAXN][][], ok, first;
vector<pair<int, int> > path;
 
void build_dice() {             //dice[top][face] = right
    dice[][] = ;
    for (int i = ; i < ; i++) {
        for (int j = ; j < ; j++) {
            if (i == j) continue;
            if (dice[i][ - j]) dice[i][j] = - dice[i][ - j];
            if (dice[i][j]) {
                dice[j][i] = - dice[i][j];
                dice[i][dice[i][j]] = - j;
            }
        }
    }
}
 
pair<int, int> rotate(int top, int face, int di) {
    pair<int, int> n_tf;
    if (di == ) {
        n_tf.second = face;
        n_tf.first = - dice[top][face];
    }
    else if (di == ) {
        n_tf.first = face;
        n_tf.second = - top;
    }
    else if (di == ) {
        n_tf.second = face;
        n_tf.first = dice[top][face];
    }
    else {
        n_tf.second = top;
        n_tf.first = - face;
    }
    return n_tf;
}
 
bool inside(int x, int y) {
    return x > && y > && x <= R && y <= C;
}
 
void dfs(int x, int y, int t, int f) {
    path.push_back(make_pair(x, y));
    if (x == sx && y == sy)
        if (first) first = ;
        else { ok = ; return;}
    vis[x][y][t][f] = ;
     
    for (int i = ; i < ; i++) {
        pair<int, int> nextp = rotate(t, f, i);
        int nx = x + dx[i], ny = y + dy[i];
        int nt = nextp.first, nf = nextp.second;
        if (inside(nx, ny) && (!vis[nx][ny][nt][nf] || (nx == sx && ny == sy)) && (t == b[nx][ny] || b[nx][ny] == -)) {
            dfs(nx, ny, nt, nf);
        }
        if (ok) return;
    }
    vis[x][y][t][f] = ;
    path.pop_back();
}
 
int main() {
    build_dice();
    char name[];
    while (scanf("%s", name) == && strcmp(name, "END")) {
        scanf("%d%d%d%d%d%d", &R, &C, &sx, &sy, &st, &sf);
        memset(vis, , sizeof(vis));
        memset(b, , sizeof(b));
        path.clear(); ok = ; first = ;
        for (int i = ; i <= R; i++)
            for (int j = ; j <= C; j++)
                scanf("%d", &b[i][j]);
        dfs(sx, sy, st, sf);
        len = path.size();
        printf("%s", name);
        if (ok)
            for (int i = ; i < len; i++) {
                if (i % == ) {
                    if (i >= ) printf(",");
                    printf("\n  (%d,%d)", path[i].first, path[i].second);
                }
                else printf(",(%d,%d)", path[i].first, path[i].second);
            }
        else printf("\n  No Solution Possible");
        printf("\n");
    }
    return ;
}

A Dicey Problem 骰子难题(Uva 810)的更多相关文章

  1. UVA 810 - A Dicey Problem(BFS)

    UVA 810 - A Dicey Problem 题目链接 题意:一个骰子,给你顶面和前面.在一个起点,每次能移动到周围4格,为-1,或顶面和该位置数字一样,那么问题来了,骰子能不能走一圈回到原地, ...

  2. poj 1872 A Dicey Problem WA的代码,望各位指教!!!

    A Dicey Problem Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 832   Accepted: 278 Des ...

  3. UVA-810 A Dicey Problem (BFS)

    题目大意:滚骰子游戏,骰子的上面的点数跟方格中的数相同时或格子中的数是-1时能把格子滚过去,找一条从起点滚到起点的路径. 题目大意:简单BFS,状态转移时细心一些即可. 代码如下; # include ...

  4. poj1872A Dicey Problem

    Home Problems Status Contest     284:28:39 307:00:00   Overview Problem Status Rank A B C D E F G H ...

  5. Uva - 810 - A Dicey Problem

    根据状态进行bfs,手动打表维护骰子滚动. AC代码: #include <iostream> #include <cstdio> #include <cstdlib&g ...

  6. UVA 810 A Dicey Promblem 筛子难题 (暴力BFS+状态处理)

    读懂题意以后还很容易做的, 和AbbottsRevenge类似加一个维度,筛子的形态,可以用上方的点数u和前面的点数f来表示,相对的面点数之和为7,可以预先存储u和f的对应右边的点数,点数转化就很容易 ...

  7. UVA 11991 Easy Problem from Rujia Liu?【STL】

    题目链接: option=com_onlinejudge&Itemid=8&page=show_problem&problem=3142">https://uv ...

  8. UVa 11375 - Matches

    http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...

  9. UVA 567 Risk【floyd】

    题目链接: option=com_onlinejudge&Itemid=8&page=show_problem&problem=508">https://uva ...

随机推荐

  1. StarUML启动时候出现"System Error. Code:1722. RPC服务器不可用."错误的解决办法

    StarUML是用得很顺手的UML工具,但是启动时候每次都会出现"System Error. Code:1722. RPC服务器不可用."错误. 一般来说这个应该是某个Window ...

  2. “jni.h”: No such file or directory

    VS2010解决方案: 进入 “包含目录“ 方式: 右键项目属性页-> 配置属性->VC++目录->包含目录 在”包含目录“中编辑 添加以下路径: C:\Program Files\ ...

  3. 优化有标量子查询的SQL

    数据库环境:SQL SERVER 2008R2 今天在数据库中抓出一条比较耗费资源的SQL,只返回904条数据,居然跑了40多分钟.SQL及对应的数据量如下图: SELECT saft04.cur_y ...

  4. MyEclipse中配置自己的JRE和tomcat

    MyEclipse中配置自己的JRE:windows>Preference>java>Installed JREs>Add>Stantard VM>next> ...

  5. C#.Net网页加载等待效果漂亮并且简单

    最近网页加载数据比较多,点击后给用户就是白板很不友好,想了很久找了些资料,在网页加载中显示等待画面给客户,页面加载完成自动隐藏等待效果. 在网页后台cs代码:    protected void Pa ...

  6. Delphi 停靠技术的应用

    一.基础知识介绍 1.VCL组件的基础知识 在TWinControl类中有一个DockSite属性(boolean),它的作用是是否允许别的控件停靠在它的上面:在TControl类中有一个DragKi ...

  7. 【python】只执行普通除法:添加 from __future__ import division

    from __future__ import division 注意future前后是两个下划线

  8. Linux Kernel Makefile Test

    一.本文说明 本文为linux内核Makefile整体分析的续篇,是依据Linux内核Makefile体系的主要内容编写一个简要的测试工程.Linux内核Makefile体系就好像一只“大鸟”,而这篇 ...

  9. 一个C++程序员学习C#语言

    感悟:C++是一门语法非常严谨的语言,只是指针就很难掌握,这其中肯定要经历很多折腾,特别是自学者. 折腾了一年半的C++,在即将毕业之际,对Unity3d游戏开发感兴趣,先是用cocos2dx开发了个 ...

  10. [BZOJ 2594] [Wc2006]水管局长数据加强版 【LCT】

    题目链接:BZOJ - 2594 题目分析 这道题如果没有删边的操作,那么就是 NOIP2013 货车运输,求两点之间的一条路径,使得边权最大的边的边权尽量小. 那么,这条路径就是最小生成树上这两点之 ...