zoukankan      html  css  js  c++  java
  • LightOJ 1047-Program C

    Description

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

    You will be given the information of houses. Each house will contain three integers "R G B" (quotes for clarity only), where R, G and Bare the 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 will contain 3 integers "R G B". These integers will lie in the range [1, 1000].

     

    Output

    For each case of input you have to print the case number and the minimal cost.

     

    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

    题目大意:给n座房子刷漆,每座房子可以刷红,绿,蓝色中的任何一种,粉刷每一种颜色都要花费一定的money,但是相邻两座房子的颜色不能相同,

    要求输出最少的花费。用i表示1~n座房子,用j表示三种颜色的花费。

    分析:先输入1~n的房子中每种颜色所需的花费p[i][j],接着从1~n先比较第一座房子中所需花费最少的并标记其颜色,这其中有三种情况,j=1,2,3,

    分别表示第一,二,三种颜色,用一个二维数组dp[i][j]=min(dp[i][j-2],dp[i][j-3])+p[i][j](当j=1时),然后比较第二座房子所需的花费最少,判断其颜色是否与第一座相

    同,不同则直接累加,否则比较出第二少的,再累加。

    代码如下:

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    
    using namespace std;
    int n;
    int dp[25][5],p[25][5];
    int main()
    {
    	int t,i,j,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
    ",++kase,min(dp[n][1],min(dp[n][2],dp[n][3])));
    	}
    	return 0;
    }
  • 相关阅读:
    如何通过jQuery获取一个没有定高度的元素---------的自适应高度(offsetHeight的正确使用方法)
    jQuery插件之----缓冲运动
    jQuery插件学习基础
    javascript随机一个1-9的数字
    jQuery事件之鼠标事件
    枚举类的应用
    ConvertUtils.register(new DateConverter(null), java.util.Date.class)使用
    逆向工程不使用驼峰命名而保持字段中下划线
    mongoDB 的介绍
    eclipse 的版本及下载地址
  • 原文地址:https://www.cnblogs.com/xl1164191281/p/4734850.html
Copyright © 2011-2022 走看看