拼多多笔试 第二题 六种情况枚举一下
我们知道 骰子你
1.左旋转1下 等于右旋转3下
2.左旋转2下 等于左旋转1下再旋转1下 可以从左旋转1下递归出
3.左旋转3下 等于右旋转一下
4.左旋转4下 等于原地不动
那么24种情况压缩成6种情况
我们只需要处理 左旋 右旋 上旋 下旋 前旋 后旋
可以画个图如下
画了一个简单的坐标轴
我们发现其实就是六种情况
然后可以看图 也可以看代码
代码长是因为很多代码是在复制粘贴~
#include <cstdio>
#include <iostream>
#include <cstring>
#include <map>
#include <set>
#include <vector>
#include <algorithm>
using namespace std;
const int MAX_N = 1025;
int arr[MAX_N][9];
string gao1(string tmp)
{
string ans = "";
ans += tmp[0];
ans+=tmp[1];
ans+=tmp[5];
ans+=tmp[4];
ans+=tmp[2];
ans+=tmp[3];
return ans;
}
string gao2(string tmp)
{
string ans = "";
ans += tmp[0];
ans+=tmp[1];
ans+=tmp[4];
ans+=tmp[5];
ans+=tmp[3];
ans+=tmp[2];
return ans;
}
string gao3(string tmp)
{
string ans = "";
ans += tmp[4];
ans+=tmp[5];
ans+=tmp[2];
ans+=tmp[3];
ans+=tmp[1];
ans+=tmp[0];
return ans;
}
string gao4(string tmp)
{
string ans = "";
ans += tmp[5];
ans+=tmp[4];
ans+=tmp[2];
ans+=tmp[3];
ans+=tmp[0];
ans+=tmp[1];
return ans;
}
string gao5(string tmp)
{
string ans = "";
ans += tmp[2];
ans+=tmp[3];
ans+=tmp[1];
ans+=tmp[0];
ans+=tmp[4];
ans+=tmp[5];
return ans;
}
string gao6(string tmp)
{
string ans = "";
ans += tmp[3];
ans+=tmp[2];
ans+=tmp[0];
ans+=tmp[1];
ans+=tmp[4];
ans+=tmp[5];
return ans;
}
int col[MAX_N];
set<int> st;
map<string ,int> mp;
void dfs(string tmp,int x)
{
if(mp.find(tmp)!=mp.end()) return;
mp[tmp] = x;
string tmp1 = gao1(tmp);
string tmp2 = gao2(tmp);
string tmp3 = gao3(tmp);
string tmp4 = gao4(tmp);
string tmp5 = gao5(tmp);
string tmp6 = gao6(tmp);
if(mp.find(tmp1)==mp.end())
{
dfs(tmp1,x);
}
if(mp.find(tmp2)==mp.end())
{
dfs(tmp2,x);
}
if(mp.find(tmp3)==mp.end())
{
dfs(tmp3,x);
}
if(mp.find(tmp4)==mp.end())
{
dfs(tmp4,x);
}
if(mp.find(tmp5)==mp.end())
{
dfs(tmp5,x);
}
if(mp.find(tmp6)==mp.end())
{
dfs(tmp6,x);
}
return;
}
vector<int> ans_;
map<int,int> MP;
int ans[1025];
int main()
{
int cnt = 0;
int n;
scanf("%d",&n);
for(int i = 1;i<=n;++i)
{
for(int j = 1;j<=6;++j)
{
scanf("%d",&arr[i][j]);
}
}
for(int i = 1;i<=n;++i)
{
string tmp = "";
for(int j = 1;j<=6;++j)
{
tmp += ((arr[i][j]-1)+'1');
}
if(mp.find(tmp)==mp.end()) dfs(tmp,++cnt);
}
for(int i = 1;i<=n;++i)
{
string tmp = "";
for(int j = 1;j<=6;++j)
{
tmp += (arr[i][j]-1)+'1';
}
if(MP.find(mp[tmp])==MP.end()) MP[mp[tmp]] = 1;
else MP[mp[tmp]] = MP[mp[tmp]]+1;
}
for(map<int,int>::iterator it = MP.begin();it!=MP.end();++it)
{
ans_.push_back(it->second);
}
printf("%d\n",ans_.size());
sort(ans_.begin(),ans_.end());
for(int i = ans_.size()-1;i>=0;--i)
{
i==0?printf("%d\n",ans_[i]):printf("%d ",ans_[i]);
}
return 0;
} 

查看15道真题和解析