hdoj_4272

LianLianKan

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


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

需要加map判断下,否则超时。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<map>
#include<cmath>
#include<vector>
#include<algorithm>
#include<set>
#include<string>
#include<queue>
#include <stack>
using namespace std;
#pragma warning(disable : 4996)
const int MAXN = 1010;
int num[MAXN];
bool visited[MAXN];
map<int, int>Map;

int dfs(int n)
{
	int i, j;
	while (n > 0 && visited[n])
	{
		n--;
	}
	if(n == 0)
	{
		return 1;
	}
	if(n == 1)
	{
		return 0;
	}
	i = 0;
	j = n - 1;
	while(i < 5)
	{
		if(j <= 0)
		{
			return 0;
		}
		if(visited[j])
		{
			j--;
			continue;
		}
		if(num[n] == num[j])
		{
			visited[j] = true;
			if(dfs(n-1) == 1)
			{
				return 1;
			}
			visited[j] = false;
		}
		j--;
		i++;
	}
	return 0;
}

int main()
{
	freopen("in.txt", "r", stdin);
	int n, x;
	bool flag;
	map<int, int>::iterator it;
	while (scanf("%d", &n) != EOF)
	{
		Map.clear();
		for (int i = 1; i <= n; i++)
		{
			scanf("%d", &x);
			num[i] = x;
			Map[x]++;
		}
		if(n % 2 == 1)
		{
			printf("0\n");
			continue;
		}
		flag = false;
		for (it = Map.begin(); it != Map.end(); it++)
		{
			if(it->second % 2 == 1)
			{
				flag = true;
				break;
			}
		}
		if(flag)
		{
			printf("0\n");
		}
		else
		{
			memset(visited, false, sizeof(visited));
			printf("%d\n", dfs(n));
		}
	}
	return 0;
}


posted @ 2013-06-05 08:16  N3verL4nd  阅读(89)  评论(0编辑  收藏  举报