我是靠谱客的博主 鲤鱼花瓣,最近开发中收集的这篇文章主要介绍UVa 810 - A Dicey Problem,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

#include <bits/stdc++.h>
using namespace std;
const int maxn = 20;
struct Die {
    int x, y, tp, ft;
    Die() {}
    Die(int _x, int _y, int _tp, int _ft):
        x(_x), y(_y), tp(_tp), ft(_ft){}
};
int dis[maxn][maxn][7][7];
int maze[maxn][maxn];
int top_front[maxn][maxn];
int r,c,beg,ed,btp,bft;
int dir[4][2] = {{1,0},{-1,0},{0,1},{0,-1}};//下上右左
Die father[maxn][maxn][7][7];
void prepare() {
    top_front[1][5] = 3, top_front[1][3] = 2, top_front[1][2] = 4, top_front[1][4] = 5;
    top_front[2][1] = 3, top_front[2][3] = 6, top_front[2][6] = 4, top_front[2][4] = 1;
    top_front[3][1] = 5, top_front[3][5] = 6, top_front[3][6] = 2, top_front[3][2] = 1;
    top_front[4][1] = 2, top_front[4][2] = 6, top_front[4][6] = 5, top_front[4][5] = 1;
    top_front[5][1] = 4, top_front[5][4] = 6, top_front[5][6] = 3, top_front[5][3] = 1;
    top_front[6][2] = 3, top_front[6][3] = 5, top_front[6][5] = 4, top_front[6][4] = 2;
}

bool read() {
    string str;
    cin >> str;
    if(str=="END") return false;
    cout << str;
    memset(maze, 0, sizeof(maze));
    memset(dis, -1, sizeof(dis));
    memset(father, 0, sizeof(father));
    cin >> r >> c >> beg >> ed >> btp >> bft;
    for(int i = 1; i <= r; i++) {
        for(int j = 1; j <= c; j++)
            cin >> maze[i][j];
    }
    return true;
}

Die rotate(Die cur,int i) {
    Die next = cur;
    next.x += dir[i][0], next.y += dir[i][1];
    if(i==0) {//上
        next.ft = 7 - next.ft; swap(next.ft, next.tp); }
    else if(i==1) {//下
        next.tp = 7 - next.tp; swap(next.ft, next.tp); }
    else if(i==2)//右
        next.tp = top_front[cur.tp][cur.ft];
    else //左
        next.tp = 7-top_front[cur.tp][cur.ft];
    return next;
}

bool inside(Die next) {
    return next.x>0 && next.x<=r && next.y>0 && next.y<=c;
}

bool feasible(Die next, Die cur) {
    if(maze[next.x][next.y]==-1) return true;
    else if(maze[next.x][next.y]==cur.tp) return true;
    return false;
}

void print(Die u) {
    vector<Die> vec;
    while(true) {
        vec.push_back(u);
        if(dis[u.x][u.y][u.tp][u.ft]==0) break;
        u = father[u.x][u.y][u.tp][u.ft];
    }
    int cnt = 0;
    bool flag = false, f = false;
    for(int i = (int)vec.size()-1; i >= 0; i--) {
        if(cnt++%9==0) {
            if(!f) {
                cout << endl << "  ";
                f = true;
            }
            else cout << "," << endl << "  ";
            flag = false;
        }
        if(!flag) {
            cout << "(" << vec[i].x << "," << vec[i].y << ")";
            flag = true;
        }
        else cout << ",(" << vec[i].x << "," << vec[i].y << ")";
    }
    cout << ",(" << beg << "," << ed << ")";
    cout << endl;
}

void bfs() {
    queue<Die> queue1;
    queue1.push(Die(beg,ed,btp,bft));
    dis[beg][ed][btp][bft] = 0;
    while(!queue1.empty()) {
        Die cur = queue1.front(); queue1.pop();
        for(int i = 0; i < 4; i++) {
            Die next = rotate(cur, i);
            cout << next.x << " " << next.y << " " << next.tp << " " << next.ft << endl;
            if(!inside(next) || !feasible(next, cur)) continue;
            if(next.x==beg && next.y==ed) {
                print(cur);
                return;
            }
            if(dis[next.x][next.y][next.tp][next.ft]<0) {
                queue1.push(next);
                dis[next.x][next.y][next.tp][next.ft] = dis[cur.x][cur.y][cur.tp][cur.ft]+1; 
                father[next.x][next.y][next.tp][next.ft] = cur;
            }
        }
    }
    cout << "n  No Solution Possible"<<endl;
    return;
}

int main() {
    freopen("i.txt","r",stdin);;
    freopen("o.txt","w",stdout); 
    prepare();
    while(read()) {
        bfs();
    }
    return 0;
}

 

最后

以上就是鲤鱼花瓣为你收集整理的UVa 810 - A Dicey Problem的全部内容,希望文章能够帮你解决UVa 810 - A Dicey Problem所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(37)

评论列表共有 0 条评论

立即
投稿
返回
顶部