light oj 1047 - Neighbor House(贪心)

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 B are 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

题解:这个题我一开始想的是每次都找最小的的那一个(除了第一个),并且颜色不相同,这时候开个标记数组,把每次可以涂得选一个较小的,这种解法有很大的问题,既然你考虑了第一个可能不是最小的那后边也可能不是最小的相加就是和最小的,因为你所致的最小是去除不能标记点的最小。而不是单纯每次求最小,和就是最小,是受前边影响的,那我们要求的是和最小,我们就可以每次找和最小的,这样最后找出来肯定是最小的。

附上两次的代码:

错误代码:

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>

using namespace std;


int main()
{
	int n;
	int a[1005][3];
    int mark[1005][3];
	scanf("%d",&n);
	for(int t=0;t<n;t++)
	{
		memset(mark,0,sizeof(mark));
		int m;
		scanf("%d",&m);
		for(int j=0;j<m;j++)
		{
			for(int k=0;k<3;k++)
			{
				scanf("%d",&a[j][k]);
			}
		}
		long long sum1=a[0][0];
		memset(mark,0,sizeof(mark));
		mark[0][0]=1;
		int temp1;
	
	    for(int j=1;j<m;j++)
	    {
	    	int minn=9999;
	    	for(int k=0;k<3;k++)
	    	{
	    		if(!mark[j-1][k])
	    		{
	    			if(minn>a[j][k])
	    			{
	    				minn=a[j][k];
	    				temp1=k;
					}
				}
			    
			}
			sum1+=minn;
			mark[j][temp1]=1;
			
		}
		long long sum2=a[0][1];
		memset(mark,0,sizeof(mark));
		mark[0][1]=1;
		int temp2;
	
	    for(int j=1;j<m;j++)
	    {
	    	int minn=9999;
	    	for(int k=0;k<3;k++)
	    	{
	    		if(!mark[j-1][k])
	    		{
	    			if(minn>a[j][k])
	    			{
	    				minn=a[j][k];
	    				temp2=k;
					}
				}
			    
			}
			sum2+=minn;
			mark[j][temp2]=1;
			
		}
		long long sum3=a[0][2];
		memset(mark,0,sizeof(mark));
		mark[0][2]=1;
		int temp3;
	
	    for(int j=1;j<m;j++)
	    {
	    	int minn=9999;
	    	for(int k=0;k<3;k++)
	    	{
	    		if(!mark[j-1][k])
	    		{
	    			if(minn>a[j][k])
	    			{
	    				minn=a[j][k];
	    				temp3=k;
					}
				}
			    
			}
			sum3+=minn;
			mark[j][temp3]=1;
			
		}
		if(min(sum1,sum2)>sum3)
		{
			printf("Case %d: %lld\n",t+1,sum3);
		}
		else
		{
			printf("Case %d: %lld\n",t+1,min(sum1,sum2));
		}
	}
	
	return 0;
} 

AC代码:

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>

using namespace std;

int main()
{
	int a[1005][3];
	int sum[1005][3];
	int n;
	scanf("%d",&n);
	for(int t=1;t<=n;t++)
	{
	   int m;
	   scanf("%d",&m);
	   for(int j=0;j<m;j++)
	   {
	   	for(int k=0;k<3;k++)
	   	{
	   	  scanf("%d",&a[j][k]);	
		}
	   }
	   memset(sum,0,sizeof(sum));
	   sum[0][0]=a[0][0];
	   sum[0][1]=a[0][1];
	   sum[0][2]=a[0][2];
	   for(int  j=1;j<m;j++)
	   {
	   	sum[j][0]=min(sum[j-1][1],sum[j-1][2])+a[j][0];
	   	sum[j][1]=min(sum[j-1][0],sum[j-1][2])+a[j][1];
	   	sum[j][2]=min(sum[j-1][0],sum[j-1][1])+a[j][2];
	   }
	   printf("Case %d: %d\n",t,(min(sum[m-1][0],sum[m-1][1])<sum[m-1][2])?min(sum[m-1][0],sum[m-1][1]):sum[m-1][2]);
	}
	
	return 0;
}

 

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

black-hole6

你的鼓励将是我创作的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值