POJ 1080 解题报告

这道题看着就是DP题。基本上也就是DP题的模式。值得注意的是DP table的初始化,要把边界情况考虑清楚。比如DP[i][0]这种情况,处理的是从0到i都对应到0的情况,不应该疏忽地理解为只有i对应0。

thestoryofsnow1080Accepted180K0MSC++2210B
/* 
ID: thestor1 
LANG: C++ 
TASK: poj1080 
*/
#include <iostream>
#include <fstream>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <limits>
#include <string>
#include <vector>
#include <list>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <algorithm>
#include <cassert>

using namespace std;

const int MAXLEN = 100;


void ACGTto0123(char str[], int len)
{
	for (int i = 0; i < len; ++i)
	{
		if (str[i] == 'A')
		{
			str[i] = '0';
		}
		else if (str[i] == 'C')
		{
			str[i] = '1';
		}
		else if (str[i] == 'G')
		{
			str[i] = '2';
		}
		else
		{
			assert (str[i] == 'T');
			str[i] = '3';
		}
	}
}


int DP(char str1[], int len1, char str2[], int len2, const int scoreTable[][5], int scores[][MAXLEN + 1])
{
	scores[0][0] = 0;
	for (int i = 1; i <= len1; ++i)
	{
		int num1 = str1[i - 1] - '0';
		scores[i][0] = scores[i - 1][0] + scoreTable[num1][4];
	}
	for (int j = 1; j <= len2; ++j)
	{
		int num2 = str2[j - 1] - '0';
		scores[0][j] = scores[0][j - 1] + scoreTable[4][num2];
	}

	for (int i = 1; i <= len1; ++i)
	{
		for (int j = 1; j <= len2; ++j)
		{
			int num1 = str1[i - 1] - '0', num2 = str2[j - 1] - '0';
			// max of (str1[i] <-> str2[j], str1[i] <-> -)
			scores[i][j] = max(scores[i - 1][j - 1] + scoreTable[num1][num2], scores[i - 1][j] + scoreTable[num1][4]);
			// or (- <-> str2[j])
			scores[i][j] = max(scores[i][j], scores[i][j - 1] + scoreTable[4][num2]);
		}
	}

	return scores[len1][len2];
}

int main()
{
	const int scoreTable[5][5] = {
		{5, -1, -2, -1, -3},
		{-1, 5, -3, -2, -4},
		{-2, -3, 5, -2, -2},
		{-1, -2, -2, 5, -1},
		{-3, -4, -2, -1, 0}
	};

	int T;	
	scanf("%d", &T);

	int len1, len2;
	char str1[MAXLEN + 1], str2[MAXLEN + 1];
	int scores[MAXLEN + 1][MAXLEN + 1];

	for (int t = 0; t < T; ++t)
	{
		scanf("%d", &len1);
		scanf("%s", str1);

		scanf("%d", &len2);
		scanf("%s", str2);

		// printf("[%s][%s]\n", str1, str2);

		ACGTto0123(str1, len1);
		ACGTto0123(str2, len2);

		// printf("[%s][%s]\n", str1, str2);		

		printf("%d\n", DP(str1, len1, str2, len2, scoreTable, scores));
	}
	return 0;  
}


POJ1753题目为"Flip Game",题目给出了一个4x4的棋盘,每个格子有黑色或白色,每次翻转一个格子会同时翻转它上下左右四个格子的颜色,目标是把整个棋盘都变为同一种颜色,求把棋盘变成同种颜色的最小步数。 解题思路: 一般关于棋盘变色的题目,可以考虑使用搜索来解决。对于POJ1753题目,可以使用广度优先搜索(BFS)来解决。 首先,对于每个格子,定义一个状态,0表示当前格子是白色,1表示当前格子是黑色。 然后,我们可以把棋盘抽象成一个长度为16的二进制数,将所有格子的状态按照从左往右,从上往下的顺序排列,就可以用一个16位的二进制数表示整个棋盘的状态。例如,一个棋盘状态为: 0101 1010 0101 1010 则按照从左往右,从上往下的顺序把所有格子的状态连接起来,即可得到该棋盘的状态为"0101101001011010"。 接着,我们可以使用队列来实现广度优先搜索。首先将初始状态加入队列中,然后对于队列中的每一个状态,我们都尝试将棋盘上的每个格子翻转一次,生成一个新状态,将新状态加入队列中。对于每一个新状态,我们也需要记录它是从哪个状态翻转得到的,以便在得到最终状态时能够输出路径。 在搜索过程中,我们需要维护每个状态离初始状态的步数,即将该状态转换为最终状态需要的最小步数。如果我们找到了最终状态,就可以输出答案,即最小步数。 代码实现:
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值