poj 1574 The Triangle Game
Address: http://acm.pku.edu.cn/judgeoonline/problem? Id = 1574
Topic analysis:
Give you six triangles, and give you the weight of each side of the triangle, ask if you can put the triangle together into a hexagon, if you can, output the minimum contour circumference.
As long as you look at the picture in the title again, you can understand the title clearly.
Specific analysis:
In fact, it's not difficult. We use a six [] array to record the contour perimeter of the hexagon. If the number matching the previous edge in the current triangle is j, then the number of the contour it contributes is (j + 1)%3, and the edge to be matched becomes (J + 2)% 3.
AC Code:
#include<cstdio> #include<cstring> #include<algorithm> #include<iostream> #include<cstdlib> #include<cmath> #include<queue> #include<vector> #include<stack> using namespace std; int Tri[8][3],six[8],vis[8]; int ans,First; void DFS(int pos, int index ,int cnt) { //cout << "Ok" << endl; if(cnt == 6) { int sum = 0; for(int i = 0 ;i < cnt ;i++) { sum += six[i]; //cout << six[i] << " "; } //cout << sum << endl; if(sum > ans) ans = sum; return ; } for(int i = 0 ;i < 6 ;i++ ) { for(int j = 0 ;j < 3 ;j++) if(!vis[i] && Tri[i][j] == Tri[pos][index]) { if(cnt < 5) { vis[i] = 1; six[cnt ] = Tri[i][(j + 1)%3]; /* if( cnt == 1 ) printf("i is %d ,j is %d ,x is %d val is %d\n", i ,j ,(j+1)%3, six[cnt + 1]); */ DFS(i, (j + 2)%3, cnt + 1); vis[i] = 0; } else if(cnt == 5) { if(Tri[i][(j + 2) % 3 ] == First) { vis[i] = 1; six[cnt ] = Tri[i][(j + 1) %3]; DFS(i,(j + 2)%3, cnt + 1); vis[i] = 0; } } } } return ; } int main() { char ch; while(1) { ans = -1; memset(vis, 0, sizeof(vis)); for(int i = 0 ;i < 6 ;i++) cin >> Tri[i][0] >> Tri[i][1] >> Tri[i][2]; cin >> ch; for(int i = 0 ;i < 6 ;i++) { for(int j = 0 ;j < 3 ;j++) if(!vis[i] ) { // cout << "Ok1" << endl; First = Tri[i][j]; vis[i] = 1; six[0] = Tri[i][(j + 1) %3]; DFS(i,(j + 2)%3 ,1); vis[i] = 0; } } //cout << "OK" << endl; if(ans == -1) cout << "none" << endl; else cout << ans << endl; if(ch == '$') break; } return 0; }