Lightoj 1047-program C

Source: Internet
Author: User

Description

The people of Mohammadpur has decided to paint each of the their houses red, green, or blue. They ' ve also decided that no neighboring houses would be painted the same color. The neighbors of House i is houses i-1 and i+1. The first and last houses is not neighbors.

You'll be given the information of houses. Each house would contain three integers "R G B" (quotes for clarity only), where R, G and Bis t He costs of painting the corresponding house red, green, and blue, respectively. Return the minimal total cost required to perform the work.

Input

Input starts with an integer T (≤100), denoting the number of test cases.

Each case begins with a blank line and an integer n (1≤n≤20) denoting the number of houses. Each of the next n lines would contain 3 integers "R G B". These integers'll lie in the range [1, +].

Output

For each case of input, the case number and the minimal cost are the to print.

Sample Input

2

4

13 23 12

77 36 64

44 89 76

31 78 45

3

26 40 83

49 60 57

13 89 99

Sample Output

Case 1:137

Case 2:96

The main topic: to paint the N house, each house can be brushed red, green, blue in any one, paint each color to spend a certain amount of money, but the two adjacent houses cannot be the same color,

Requires minimal cost of output. Use I to represent the 1~n House, with J for the cost of three different colors.

Analysis: First enter the 1~n house in the cost of each color p[i][j], and then from 1~n first house to compare the minimum cost and mark its color, which there are three cases, j=1,2,3,

The first, two, three colors, respectively, with a two-dimensional array dp[i][j]=min (dp[i][j-2],dp[i][j-3]) +p[i][j] (when j=1), and then the second house to compare the cost of the least, to determine whether its color and the first phase

The same, the difference is directly accumulated, otherwise the second less, and then accumulate.

The code is as follows:

#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>UsingNamespace Std;int n;int DP[25][5],p[25][5];IntMain(){int T, IJ, Kase=0;scanf("%d", &t);While(t--){Memset(DP,0,sizeof(DP));scanf("%d", &n);For(I=1; I<=n; I++)For(j=1; j<=3; j++)scanf("%d", &p[I][j]);For(I=1; I<=n; I++)For(j=1; j<=3; j++){If(j==1) DP[I][j]=Min(DP[I-1][2],dp[I-1][3]) +p[I][j];If(j==2) DP[I][j]=Min(DP[I-1][1],dp[I-1][3]) +p[I][j];If(j==3) DP[I][j]=Min(DP[I-1][1],dp[I-1][2]) +p[I][j];}printf("Case%d:%d\ n", ++kase,min(DP[n] [1],min( DP[n] [2],DP [n] [3]));} return 0;}               

Lightoj 1047-program C

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.