利用std::set和遍历骰子的24种可能(人面向骰子)

筛子如下图,有六个

rotate.h

#include <string>
void swap(char& a, char& b, char& c, char& d){
   char temp=a;
   a=b;
   b=c;
   c=d;
   d=temp;
}

string rotate(string dice,int rotation){
   // rotation:top 1,back 2,right 3,front 4,left 5, down 6
   switch(rotation){
   case 1://roll front
   {
       swap(dice.at(4-1),dice.at(1-1),dice.at(2-1),dice.at(6-1));
       break;
   }
   case 2://roll back
   {
       swap(dice.at(6-1),dice.at(2-1),dice.at(1-1),dice.at(4-1));
       break;
   }
   case 3://roll left
   {
       swap(dice.at(1-1),dice.at(3-1),dice.at(6-1),dice.at(5-1));
       break;
   }
   case 4://roll right
   {
       swap(dice.at(5-1),dice.at(6-1),dice.at(3-1),dice.at(1-1));
       break;
   }
   case 5://rotate left
   {
       swap(dice.at(2-1),dice.at(3-1),dice.at(4-1),dice.at(5-1));
       break;
   }
   case 6://rotate right
   {
       swap(dice.at(5-1),dice.at(4-1),dice.at(3-1),dice.at(2-1));
       break;
   }
   default:break;
   }
   return dice;
}

printdice.hh

#ifndef PRINTDICE_HH
#define PRINTDICE_HH
#include <string>
#include <set>
void printdices(std::set<string> & dices,string dice){
   if(dices.size()==(unsigned)24) return;
   //cout<<"size of the set:"<<dices.size()<<endl;
   std::set<string>::iterator it;
   bool isexist=false;
   for(it=dices.begin();it!=dices.end();++it){
       std::string temp=*it;
       if(temp.compare(dice)==0){
           isexist=true;
       }
   }
   if(isexist==false){
       cout<<dice<<endl;
       dices.insert(dice);
   }else{
       return;
   }
   for(int i=1;i<=6;++i){
       //cout<<"i:"<<i<<endl;
       string temp=rotate(dice,i);
       printdices(dices,temp);
   }
}
#endif // PRINTDICE_HH



main.cc

#include <iostream>
using namespace std;
#include "complex.hh"
#include "rotate.hh"
#include "printdice.hh"

int main()
{
    //program2
    string dice2="145326";
    cout<<"start: "<<dice2<<endl;
    cout<<"roll front: ";
    cout<<rotate(dice2,1)<<endl;
    cout<<"turn left: ";
    cout<<rotate(dice2,5)<<endl;
    //program3
    string dice3="145326";
    std::set<string> set_dices;
    printdices(set_dices,dice3);

    return 0;
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值