ZOJ 1208 Roll the Die! (模拟)

于 2011年02月9日 发布在 未分类 跳到评论

ZOJ 1208 Roll the Die!

还是筛子在平面坐标上滚动的问题,初始时候色子在(0,0)的位置,每组输入会给出一个top和front值,代表在初始点时候色子的上面和前面的数字,另外还会给出一系列的操作,问最终状态时候的坐标和此时的top,front值~

记得在去年暑假的时候,我们印了一份zoj的分类,照着上面的题目一个一个刷,后来cw说这上面的题目非常难,不是你们能力所能及的。当时模拟题分类的第一个就是他,我读懂了题,当时印象是非常麻烦,之后就放弃这个了,但脑子里始终有个印象:有个关于色子的很难的题我没做。早上A了3059,就是上一篇题解,关于色子的滚动,然后就想到了这题,AC掉之后非常高兴,今天下雪喽~

我用了宏定义,把方向用宏表示出来,理解起来轻松了许多:


#include<iostream>
#include<string>
#include<cstring>
#include<cstdio>
#include<cstdlib>
#include<queue>
#define ZUO 3
#define YOU 0
#define QIAN 2
#define HOU 1
using namespace std;
int dir[4][2] = {1,0,0,1,0,-1,-1,0};//4个方向
char s[300];
int dui[7] = {0,6,5,4,3,2,1};
int ok[7][7] = {{0},
    {0,0,4,2,5,3,0},
    {0,3,0,6,1,0,4},
    {0,5,1,0,0,6,2},
    {0,2,6,0,0,1,5},
    {0,4,0,1,6,0,3},
    {0,0,3,5,2,4,0}
};//top&front,这个表的意义就是当top=i,front=j时候left的值是ok[i][j]

struct node
{
    int top,front;
    int left,right;
    int back,bottom;
    int x,y;
}t;
node roll( node t,int i)//以第i的方向,对t进行滚动
{
    int m;
    t.x += dir[i][0];
    t.y += dir[i][1];
    if( i == QIAN )
    {
        m = t.front;
        t.front = t.top;
        t.top = t.back;
        t.back = t.bottom;
        t.bottom = m;
    }
    if( i == YOU )
    {
        m = t.right;
        t.right = t.top;
        t.top = t.left;
        t.left = t.bottom;
        t.bottom = m;
    }
    if( i == ZUO )
    {
        m = t.right;
        t.right = t.bottom;
        t.bottom = t.left;
        t.left = t.top;
        t.top = m;
    }
    if( i == HOU )
    {
        m = t.bottom;
        t.bottom = t.back;
        t.back = t.top;
        t.top = t.front;
        t.front = m;
    }
    return t;
}
int main(void)
{
    //freopen("C:\\Users\\thinkpad\\Desktop\\out.txt","w",stdout);
    int a,b;
    char c;
    printf("Problem 2 by team x\n");
    while( scanf("%d%d",&a,&b) != EOF )
    {
        printf("\n");
        if( !ok[a][b] )
        {
            gets(s);
            printf("Invalid initial orientation:");
            printf("     top = %d  front = %d\n",a,b);
            continue;
        }
        printf("Initial orientation:");
        printf("             top = %d  front = %d\n",a,b);
        printf("Moves:");
        t.top = a;
        t.front = b;
        t.bottom = dui[ t.top ];
        t.back = dui[ t.front ];
        t.left = ok[ t.top ][ t.front ];
        t.right = dui[ t.left ];
        t.x = t.y = 0;
        while( 1 )
        {
            if( (c=getchar()) == '\n' ) break;
            c = getchar();
            printf(" %c",c);
            switch(c)
            {
                case 'E':t = roll(t,YOU);break;
                case 'N':t = roll(t,HOU);break;
                case 'W':t = roll(t,ZUO);break;
                case 'S':t = roll(t,QIAN);break;
                default :break;
            }
        }
        printf("\nFinal orientation and position:");
        printf("  top = %d  front = %d  x =%4d  y =%4d\n",
                t.top,t.front,t.x,t.y);
    }
    printf("End of problem 2 by team x\n");
    return 0;
}

本文共有 5条评论 | 沙发:文章评论

  1. 小媛说道:

    :wink: 你别加题目链接了 每次都点错 = =。。

  2. 墨画的留白说道:

    我把我主题升到3.0了额,这下咱俩的看着有点像了。。。

留下评论!

:wink: :twisted: :roll: :oops: :mrgreen: :lol: :idea: :evil: :cry: :arrow: :?: :-| :-x :-o :-P :-D :-? :) :( :!: 8-O 8)