首页 > 代码库 > HDU-4272 LianLianKan

HDU-4272 LianLianKan

          http://acm.hdu.edu.cn/showproblem.php?pid=4272

        据说是状态压缩,+dfs什么什么的,可我这样也过了,什么算法都是浮云 ,暴力才是王道。我也归类为状态压缩,可以用状态压缩来做。

           LianLianKan

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2482    Accepted Submission(s): 773


Problem Description
I like playing game with my friend, although sometimes looks pretty naive. Today I invent a new game called LianLianKan. The game is about playing on a number stack.
Now we have a number stack, and we should link and pop the same element pairs from top to bottom. Each time, you can just link the top element with one same-value element. After pop them from stack, all left elements will fall down. Although the game seems to be interesting, it‘s really naive indeed.

To prove I am a wisdom among my friend, I add an additional rule to the game: for each top element, it can just link with the same-value element whose distance is less than 6 with it.
Before the game, I want to check whether I have a solution to pop all elements in the stack.
 

 

Input
There are multiple test cases.
The first line is an integer N indicating the number of elements in the stack initially. (1 <= N <= 1000)
The next line contains N integer ai indicating the elements from bottom to top. (0 <= ai <= 2,000,000,000)
 

 

Output
For each test case, output “1” if I can pop all elements; otherwise output “0”.
 

 

Sample Input
2
1 1
3
1 1 1
2
1000000 1
 

 

Sample Output
1
0
0
 1 #include<iostream> 2 #include<cstring> 3 #include<cstdio> 4 using namespace std; 5 int main() 6 { 7     int n,j,i,k,a[1005],v[1005]; 8     while(~scanf("%d",&n)) 9     {10         for(i=1;i<=n;i++)11           scanf("%d",&a[i]);12        memset(v,0,sizeof(v));13        int cnt=0;14        int flag=0;15        while(cnt<n)16        {17            for(i=1;i<=n;i++)18            {19                if(v[i]==1)20                   continue;21                   k=0;22                 for(j=i+1;j<=n&&k<=5;j++)23                 {24                     if(v[j]==1)25                       continue;26                     if(a[i]==a[j])27                     {28                        v[i]=v[j]=1;29                         break;30                     }31                     k++;32                 }33            }34            cnt++;35        }36        for(i=1;i<=n;i++)37         {38                if(v[i]==0)39                {40                    printf("0\n");41                    break;42                }43         }44         if(i>n)45           printf("1\n");46 47     }48     return 0;49 }