codeforces Restore Cube(暴力枚举)
生活随笔
收集整理的這篇文章主要介紹了
codeforces Restore Cube(暴力枚举)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1 /*
2 題意:給出立方體的每個頂點的坐標(是由源坐標三個數某幾個數被交換之后得到的!),
3 問是否可以還原出一個立方體的坐標,注意這一句話:
4 The numbers in the i-th output line must be a permutation of the numbers in i-th input line!
5
6 思路:
7 我們只要對輸入的每一行數據進行枚舉每一個排列, 然后檢查時候能構成立方體就好了!
8 檢查立方體:找到最小的邊長的長度 l, 統計邊長為l, sqrt(2)*l, sqrt(3)*l的邊長
9 條數,如果恰好分別為12, 12, 4那么就是立方體了...
10 */
11 #include<iostream>
12 #include<cstdio>
13 #include<cstring>
14 #include<algorithm>
15
16 using namespace std;
17
18 typedef long long LL;
19
20 LL num[8][3], d[70];
21
22 LL dis(int i, int j){
23 return (num[i][0]-num[j][0])*(num[i][0]-num[j][0]) +
24 (num[i][1]-num[j][1])*(num[i][1]-num[j][1]) +
25 (num[i][2]-num[j][2])*(num[i][2]-num[j][2]);
26 }
27
28
29 void out(){
30 for(int i=0; i<8; ++i){
31 printf("%lld", num[i][0]);
32 for(int j=1; j<3; ++j)
33 printf(" %lld", num[i][j]);
34 printf("\n");
35 }
36 }
37
38 int vis[8][8];
39
40 bool check(){
41 int cnt=0;
42 int cnt1=0, cnt2=0, cnt3=0;
43 LL L=(1LL)<<60;
44 memset(vis, 0, sizeof(vis));
45 for(int i=0; i<8; ++i)
46 for(int j=0; j<8; ++j)
47 if(i!=j && !vis[i][j] && !vis[j][i]){
48 d[cnt++]=dis(i, j);
49 vis[i][j]=vis[j][i]=1;
50 if(L>d[cnt-1])
51 L=d[cnt-1];
52 }
53 if(L==0) return false;
54 for(int i=0; i<cnt; ++i)
55 if(d[i] == L) cnt1++;
56 else if(d[i] == 2*L) cnt2++;
57 else if(d[i] == 3*L) cnt3++;
58 if(cnt1==12 && cnt2==12 && cnt3==4) return true;
59 return false;
60 }
61
62 bool dfs(int cur){
63 if(cur>=8) return false;
64 sort(num[cur], num[cur]+3);//排序
65 do{
66 if(check()){
67 printf("YES\n");
68 out();
69 return true;
70 }
71 if(dfs(cur+1)) return true;//得到當前的全排列之后,繼續向下尋找
72 }while(next_permutation(num[cur], num[cur]+3));//枚舉這一個行的每一個全排列
73 return false;
74 }
75
76 int main(){
77 for(int i=0; i<8; ++i)
78 for(int j=0; j<3; ++j)
79 scanf("%lld", &num[i][j]);
80 if(!dfs(0))
81 printf("NO\n");
82 return 0;
83 }
?
轉載于:https://www.cnblogs.com/hujunzheng/p/3966599.html
總結
以上是生活随笔為你收集整理的codeforces Restore Cube(暴力枚举)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 一串铃小南瓜是打侧枝留主枝,还是留主枝,
- 下一篇: 啤酒为什么不用塑料瓶装?