不容易系列之(3)—— LELE的RPG难题(HDOJ2045)

原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=2045

解题分析:

假设n个方格有f(n)种涂法,n-1个方格有f(n-1)种涂法。

分两种情况:

1、前n-1个合法,则第1个、第n-1个还有第n个颜色都不一样。这时有f(n-1)种涂法;

2、前n-1个不合法,则第1个和第n-1个颜色不同,也就是前n-2个合法,有f(n-2)种涂法,第n个有2种涂法。所以这种情况下有f(n-2)*2种涂法。

所以f(n)=f(n-1)+2*f(n-2)

源代码如下:

#include<iostream>
using namespace std;
int main()
{
  long long int arr[51];
  arr[1]=3,arr[2]=6,arr[3]=6;//此题虽是二阶的公式,但要给出前三个数字
  for(int i=4;i<51;i++)
   arr[i]=arr[i-1]+arr[i-2]*2;
  int a;
  while(cin>>a)
  {
      cout<<arr[a]<<endl;
  }
  return 0;
}

 

posted @ 2013-03-25 14:55  supersnow0622  Views(124)  Comments(0)    收藏  举报