24h購物| | PChome| 登入
2012-12-17 14:05:37| 人氣782| 回應0 | 上一篇 | 下一篇

[UVA][math] 580 - Critical Mass

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


  Critical Mass 

During the early stages of the Manhattan Project, the dangers of the new radioctive materials were not widely known. Vast new factory cities were built to manufacture uranium and plu- tonium in bulk. Compounds and solutions of these substances were accumulating in metal barrels, glass bottles and cardboard box piles on the cement floors of store rooms. Workers did not know that the substances they were handling could result in sickness, or worse, an explosion. The officials who new the danger assumed that they could ensure safety by never assembling any amount close to the critical mass estimated by the physicists. But mistakes were made. The workers, ignorant of the the dangers, often did not track these materials care- fully, and in some cases, too much material was stored together - an accident was waiting to happen.


Fortunately, the dangers were taken seriously by a few knowledgeable physicists. They drew up guidelines for how to store the materials to eliminate the danger of critical mass accumulations. The system for handling uranium was simple. Each uranium cube was marked ``U''. It was to be stacked with lead cubes (marked ``L'') interspersed. No more than two uranium cubes could be next to each other on a stack. With this simple system, a potential for the uranium reaching critical mass (three stacked next to each other) was avoided. The second constraint is that no more than thirty cubes can be stacked on top of each other, since the height of the storage room can only accommodate that many.


One of the physicists was still not completely satisfied with this solution. He felt that a worker, not paying attention or not trained with the new system, could easily cause a chain reaction. He posed the question: consider a worker stacking the radioactive cubes and non radioactive cubes at random on top of each other to a height of n cubes; how many possible combinations are there for a disaster to happen?

For example, say the stack is of size 3. There is one way for the stack to reach critical mass - if all three cubes are radioactive.


1: UUU


However, if the size of the stack is 4, then there are three ways:


1: UUUL

2: LUUU

3: UUUU

Input 

The input is a list of integers on separate lines. Each integer corresponds to the size of the stack and is always greater than 0. The input is terminated with a integer value of 0.

Output 

For each stack, compute the total number of dangerous combinations where each cube position in the linear stack can either be ``L'' for lead, or ``U'' for uranium. Output your answer as a single integer on a line by itself.

Sample Input 

4
5
0

Sample Output 

3
8


看到網路上一些很麻煩的寫法,看不是很懂,以下程式碼附上一些解說。
希望你看得懂。




#include <stdio.h>

int main() {
    int dp[50][3] = {}, i, n;
    dp[1][0] = 2;
    dp[2][0] = 4;
    dp[3][0] = 4;// safe end - L
    dp[3][1] = 2;// safe end - 1U
    dp[3][2] = 1;// safe end - 2U
    for(i = 3; i < 30; i++) {
        dp[i+1][0] += dp[i][0]; // -L + L
        dp[i+1][1] += dp[i][0]; // -L + U
        dp[i+1][2] += dp[i][1]; // -1U + 1U
        dp[i+1][0] += dp[i][1]; // -1U + L
        dp[i+1][0] += dp[i][2]; // -2U + L
    }
    while(scanf("%d", &n) == 1 && n) {
        printf("%d\n", (1<<n)-dp[n][0]-dp[n][1]-dp[n][2]);
    }
    return 0;
}

台長: Morris
人氣(782) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 不分類 | 個人分類: UVA |
此分類下一篇:[UVA][greedy] 11292 - Dragon of Loowater
此分類上一篇:[UVA][因數] 12573 - Sohel Sir's Assignment

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