24h購物| | PChome| 登入
2013-07-02 10:52:01| 人氣339| 回應0 | 上一篇 | 下一篇

[UVA] 418 - Molecules

推薦 0 收藏 0 轉貼0 訂閱站台


 Molecules 

In this abstraction from a molecular engineering problem associated with developing a synthetic fuel, we are given four, equal-length, molecular chains that are to form a super molecule. In the simplified two-dimensional model used here, the super molecule is formed as an interlocking rectangular arrangement of the four given molecular chain strands. The interlocking feature is the sharing of a common molecule between pairs of chains.

To illustrate, suppose we have the four, length-twelve, molecular chains:

                      O I M D I H E I A F N L
                      C H J D B J M H P J K D
                      L C B J O J G I E K B O
                      K A I N L H L O L B E J

These can be placed in the interlocking arrangements:

                            O           L
                            I           C
                            M           B
                      C H J D B J M H P J K D
                            I           O
                            H           J
                            E           G
                            I           I
                            A           E
                            F           K
                      K A I N L H L O L B E J
                            L           O

-OR-

                                    O     C
                                    I     H
                                    M     J
                                    D     D
                      L C B J O J G I E K B O
                                    H     J
                                    E     M
                                K A I N L H L O L B E J
                                    A     P
                                    F     J
                                    N     K
                                    L     D

In this problem, we have some constraints on the arrangements being sought:

1)
Any of the four chains can be placed in any of the super molecule's four, general, horizontal or vertical slots, as in the illustrations above.
2)
If a chain is placed in one of the two horizontal slots, it must keep the same left-to-right orientation it had in the original chain listing. That is, it can't be flipped end-for-end.
3)
If a chain is placed in one of the two vertical slots, its left-to-right orientation in the original chain listing must match its top-to-bottom orientation in the slot. It can't be flipped end-for-end from this orientation.
4)
The enclosed rectangular region at the center of the super molecule must have as large an area as possible, and the area cannot be zero. (The large-area constraint arises from a fuel-volatility criterion for the arrangement. The non-zero area constraint arises because neither the vertically nor the horizontally oriented chains can lie immediately next to each other without producing side-effects we're not considering.)

The area is measured as the count of vacant character positions within the enclosed rectangle of the super molecule. The area counts of the two super molecules illustrated above are thirty (30) and four (4).

5)
The fore and aft tails of each chain extending beyond the super molecule's central interlocked rectangle must have a minimum length of one chain element. That is, none of the four original chains can have either its first or its last element as part of the interlocking-rectangle boundary.

Input

The input consists of a series of data sets. Each data set consists of four molecular chains of 12 fixed elements each. These 12 elements are given as contiguous capital letters. The molecule designators within the chains will be restricted to the sixteen letters, A...P. The first letter of a chain will appear as the first character on an input line.

The first molecule designator within the first chain of a data set will be the letter "Q" to indicate the end of data.

Output

A line with a single integer is to be emitted for each input data set encountered. This integer is the maximum area enclosed by any legitimate arrangement of the four chains.

Use the output value zero (0) to indicate that no legitimate super molecule could be formed for a givendata set.

The first digit of an output value should be the first character on a line.

Sample Input

CDBADCBBEFEF
DACCBADAFEAB
EFBDCAADBDCD
ABCDABCDABCD
DACCBADAFEAB
EFBDCAADBDCD
ABCDABCDABCD
CDBADCBBEFEF
ABABABABABAB
CDCDCDCDCDCD
EEEEEEEEEEEE
FFFFFFFFFFFF
ABAAAAAAAABA
CBCCCCCCCCBC
DBDDDDDDDDBD
EBEEEEEEEEBE
ABBBBBBBBBBA
ACCCCCCCCCCA
ADDDDDDDDDDA
AEEEEEEEEEEA
BBBABBBABBBB
CCACCCACCCCC
DDDDADDADDDD
EEAEEAEEEEEE
Q

Sample Output

48
48
0
64
0
6

題目要求抓出兩個當作水平、另外兩個當作垂直,求中間面積最大為何。
而且每個字串要留尾端。

因此窮舉是 O(4! * 12*12*12*12*12*12)
要窮舉接的位置。


#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
int ret = 0, used[4], A[4];
char s[4][200];
void bruteforce() {
int i, j, k, p, q, r;
for(i = 1; i < 11; i++) {
for(j = 1; j < 11; j++) {
if(s[A[0]][i] == s[A[1]][j]) {
for(k = i+1; k < 11; k++) {
int w = k - i;
for(p = 1; p < 11; p++) {
if(s[A[0]][k] == s[A[2]][p]) {
for(q = j+1; q < 11; q++) {
int h = q - j;
for(r = 1; r < 11; r++) {
if(s[A[3]][r] == s[A[1]][q]) {
if(r+w < 11 && p+h < 11) {
if(s[A[3]][r+w] == s[A[2]][p+h])
ret = max(ret, (w-1)*(h-1));
}
}
}
}
}
}
}
}
}
}
}
void dfs(int idx) {
if(idx == 4) {
bruteforce();
return;
}
int i;
for(i = 0; i < 4; i++) {
if(used[i] == 0) {
used[i] = 1;
A[idx] = i;
dfs(idx+1);
used[i] = 0;
}
}
}
int main() {
while(scanf("%s %s %s %s", s[0], s[1], s[2], s[3]) == 4) {
ret = 0;
dfs(0);
printf("%d\n", ret);
}
return 0;
}

台長: Morris
人氣(339) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 不分類 | 個人分類: UVA |
此分類下一篇:[UVA][LIS] 10051 - Tower of Cubes
此分類上一篇:[UVA] 434 - Matty's Blocks

是 (若未登入"個人新聞台帳號"則看不到回覆唷!)
* 請輸入識別碼:
請輸入圖片中算式的結果(可能為0) 
(有*為必填)
TOP
詳全文