本文主要是介绍lightoj 1047 Neighbor House(Dp),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
思路:定义dp[i][j] 为粉刷第i个房子用的颜色j
dp[i][j] = min(dp[i-1][(j+1)%3] , dp[i-1][(j+2) % 3]);
一共有三种颜色{0, 1, 2},任取一种颜色{j},那么和颜色j不同的颜色就为{(j + 1) % 3 , (j + 2) % 3};
/************************************************ Author: fisty* Created Time: 2015-08-22 10:19:02* File Name : 1047.cpp*********************************************** */
#include <iostream>
#include <cstring>
#include <deque>
#include <cmath>
#include <queue>
#include <stack>
#include <list>
#include <map>
#include <set>
#include <string>
#include <vector>
#include <cstdio>
#include <bitset>
#include <algorithm>
using namespace std;
#define Debug(x) cout << #x << " " << x <<endl
#define Memset(x, a) memset(x, a, sizeof(x))
const int INF = 0x3f3f3f3f;
typedef long long LL;
typedef pair<int, int> P;
#define FOR(i, a, b) for(int i = a;i < b; i++)
#define lson l, m, k<<1
#define rson m+1, r, k<<1|1
#define MAX_N 22
int t, n;
int a[MAX_N][3];
int dp[MAX_N][3];void solve(){for(int i = 1;i <= n; i++){for(int j = 0;j < 3; j++){dp[i][j] = min(dp[i-1][(j+1)%3], dp[i-1][(j+2)%3]) + a[i][j];}}printf("%d\n", min(dp[n][0], min(dp[n][1], dp[n][2])));
}
int main() {//freopen("in.cpp", "r", stdin);//cin.tie(0);//ios::sync_with_stdio(false);scanf("%d", &t);int cnt = 1;while(t--){scanf("%d", &n);for(int i = 1;i <= n; i++){for(int j = 0;j < 3; j++){scanf("%d", &a[i][j]);}}printf("Case %d: ", cnt++);solve();}return 0;
}
这篇关于lightoj 1047 Neighbor House(Dp)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!