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\n",++kase,min(dp[n][1],min(dp[n][2],dp[n][3])));
	}
	return 0;
}
时间: 2024-11-18 01:37:14

LightOJ 1047-Program C的相关文章

lightOJ 1047 Neighbor House (DP)

题目链接:lightOJ 1047 Neighbor House 题意:有N做房子,每个房子涂3种颜色各有一个花费,相邻的房子颜色不能一样,给N个房子涂颜色,问完成这个任务的最小花费. dp[i][j] 表示涂到第i个房子涂j颜色的最小花费. 状态转移方程:dp[i][k]=min(dp[i][k],dp[i-1][j]+p[i].c[k]); AC代码: #include <stdio.h> #include <string.h> #include <algorithm&g

Neighbor House LightOJ - 1047

Neighbor House LightOJ - 1047 1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 using namespace std; 5 int T,TT,n; 6 int a[22][22],ans[22][22]; 7 int main() 8 { 9 int i,j,k; 10 scanf("%d",&T); 11 for(TT=1;TT<

LightOJ 1047 Neighbor House (DP 数字三角形变形)

1047 - Neighbor House PDF (English) Statistics Forum Time Limit: 0.5 second(s) Memory Limit: 32 MB The people of Mohammadpur have decided to paint each oftheir houses red, green, or blue. They've also decided that no two neighboringhouses will be pai

LightOJ 1047 - Neighbor House 【DP】

题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1047 题意:求(p[i][j])上下相邻的 j 不能相同的数塔的最小和. 解法:看代码! 代码: #include <stdio.h> #include <iostream> #include <string.h> #include <algorithm> #include <bitset> #include <math

LightOJ 1141 Program E

Description In this problem, you are given an integer number s. You can transform any integer number A to another integer number B by adding x to A. This x is an integer number which is a prime factor of A (please note that 1 and A are not being cons

周赛C题 LightOJ 1047 (DP)

C - C Time Limit:500MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu 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

POJ 1053 Integer Inquiry &amp;&amp; HDOJ 1047 Integer Inquiry (大数加法)

题目链接(POJ):http://poj.org/problem?id=1503 题目链接(HDOJ):http://acm.hdu.edu.cn/showproblem.php?pid=1047 Integer Inquiry Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 30856   Accepted: 12007 Description One of the first users of BIT's new su

解决方案--java执行cmd命令ProcessBuilder--出错Exception in thread "main" java.io.IOException: Cannot run program "dir d:\": CreateProcess error=2(xjl456852原创)

当我尝试在java中通过ProcessBuilder运行window的cmd命令时出现错误: public static void main(String [] args) throws IOException { ProcessBuilder builder = new ProcessBuilder(); Process process = builder.command("dir d:\\").start(); InputStream inputStream = process.g

杭电1047(Integer Inquiry)java水过

点击打开杭电1047 Problem Description One of the first users of BIT's new supercomputer was Chip Diller. He extended his exploration of powers of 3 to go from 0 to 333 and he explored taking various sums of those numbers. ``This supercomputer is great,'' re

HDUJ 1047 Integer Inquiry

Integer Inquiry Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 12426    Accepted Submission(s): 3123 Problem Description One of the first users of BIT's new supercomputer was Chip Diller. He e