Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

比较易读的滚动数组代码及注意点

Posted by ajianhouyuan at 2015-12-17 10:55:53 on Problem 3254
1. 滚动数组注意清除前一次的数值(dp[PREVIOUS][j] = 0;),否则会导致累加值错误,有这个错误的情况下也能通过样例数据。

#include <iostream>

#define PREVIOUS ((i-1+2)%2)
#define CURRENT (i%2)

using namespace std;

int main()
{
//	freopen("in.txt", "r", stdin);
//	freopen("out.txt", "w", stdout);

	int m, n;
	int row[12] = {0};
	int plan[1024] = {0};
	int dp[2][1024] = {0};
	scanf("%d %d", &m, &n);

	for(int i = 0; i < m; i++)
	{
		for (int j = 0; j < n; j++)
		{
			int bit;
			scanf("%d", &bit);
//			printf("%d ", bit);
			row[i] = (row[i]<<1) | !bit;
		}
//		printf("\nrow[%d]:%d\n", i, row[i]);
	}

	int plan_count = 0;
	for(int i = 0; i < (1<<12); i++)
	{
		if (!(i & (i << 1)) && (i < (1<<n)))
		{
			plan[plan_count++] = i;
//			printf("plan[%d]:%d\n", plan_count, i);
		}
	}

	for(int i = 0; i < plan_count; i++)
	{
		if (!(plan[i] & row[0]))
		{
			dp[0][i] = 1;
		}
	}

	for (int i = 1; i < m; i++)
	{
		for (int j = 0; j < plan_count; j++)
		{
			if (!(plan[j] & row[i-1]))
			{
				for (int k = 0; k < plan_count; k++)
				{
					if (!(plan[k] & plan[j]) && !(row[i] & plan[k]))
					{
						dp[CURRENT][k] = dp[CURRENT][k] + (dp[PREVIOUS][j] % 100000000);
					}
				}
			}
			dp[PREVIOUS][j] = 0;
		}
	}

	int sum = 0;
	for (int i = 0; i < plan_count; i++)
	{
		sum = sum + dp[(m-1)%2][i] % 100000000;
	}

	printf("%d\n", sum % 100000000);

	return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator