close

原文

中文

 

解題方法

將骰子剖面,再用相對位置來看就比較好處理。向北轉和向南轉請看示意圖,向東轉和向西轉只要將中間四個數字往右或往左移動1格就可以了。

色子剖面圖.png

 

C++

#include <iostream>
#include <string>
using namespace std;
 
int main()
{
    int n;
    string act;
    
    while(cin>>n&&n!=0)//輸入動作次數 
    {
    	int p[3][4]={0},t;
    	
    	p[1][0]=1;//設定好初始值 
    	p[1][1]=4;
    	p[1][2]=6;
    	p[1][3]=3;
    	p[0][0]=2;
    	p[2][0]=5;
    	
    	while(n--)
    	{
    		cin>>act;
    		if(act=="north")//向北轉 
			{
				t=p[0][0];
				p[0][0]=p[1][0];
				p[1][0]=p[2][0];
				p[2][0]=p[1][2];
				p[1][2]=t;
			}
			else if(act=="east")//東 
			{
				t=p[1][0];
				p[1][0]=p[1][3];
				p[1][3]=p[1][2];
				p[1][2]=p[1][1];
				p[1][1]=t;
			}
			else if(act=="south")//南 
			{
				t=p[2][0];
				p[2][0]=p[1][0];
				p[1][0]=p[0][0];
				p[0][0]=p[1][2];
				p[1][2]=t;
			}
			else if(act=="west")//西 
			{
				t=p[1][0];
				p[1][0]=p[1][1];
				p[1][1]=p[1][2];
				p[1][2]=p[1][3];
				p[1][3]=t;
			}
			
		}
		cout<<p[1][0]<<endl;
	}
    return 0;
}
 
arrow
arrow
    文章標籤
    UVa一星題 Die Game
    全站熱搜
    創作者介紹
    創作者 豪CO 的頭像
    豪CO

    程式道路,必為豐富

    豪CO 發表在 痞客邦 留言(0) 人氣()