LianLianKan
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2389 Accepted Submission(s): 382
Problem Description
I like playing game with my friends, although sometimes look 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 the 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 wisdom among my friends, 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 5.
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 integers 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
题意:类似于我们玩的连连看,从上往下,6个水果内如果有相同的2个水果则可以消去,直至水果被消完,输出1,或是找不到可以消去的水果,输出0。
思路:数据顶多1000个,直接暴力过。
#include<stdio.h> int main() { int a[1111]; int n,i,j; while(scanf("%d",&n)!=EOF) { for(i=0;i<n;i++) scanf("%d",&a[i]); if(n%2) //因为每次只能消去2个,所以奇数个水果必然消不完 { printf("0\n"); continue; } int flag=n; while(flag>0) { int hash=0; for(i=0;i<n;i++) { if(a[i]!=-1) { int num=0; for(j=i+1;j<n;j++) { if(num>4) //6个水果内找不到2个相同的 break; if(a[j]==a[i]) { a[j]=-1; //消去的水果进行标记 a[i]=-1; //消去的水果进行标记 hash=1; //输出标记 flag-=2; //总水果数对应的减去2 break; } else { if(a[j]!=-1) num++; } } } if(hash==1) break; } if(hash==0) { printf("0\n"); break; } } if(flag==0) printf("1\n"); } return 0; }