【HDU】4272 LianLianKan(DFS)
来源:互联网 发布:金山画王软件下载 编辑:程序博客网 时间:2024/05/17 02:10
LianLianKan
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.
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)
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
直接暴力DFS,超简单,无话可说。
#include<stdio.h>#include<string.h>#include<iostream>#include<map>#include<algorithm>using namespace std;const int maxn=1010;int a[maxn];bool used[maxn];int dfs(int n){ while(n>0&&used[n])n--; if(n==0)return 1; if(n==1)return 0; int i=0; int j=n-1; for(;i<=5;)//这里取i<5和i<=5都可以ac { if(j<=0)return 0;//没有找到相等的 if(used[j]) { j--; continue; } if(a[n]==a[j]) { used[j]=true; if(dfs(n-1)) return 1; used[j]=false; } i++; j--; } return 0;}map<int,int>mp;int main(){ int n; while(scanf("%d",&n)!=eof) { mp.clear(); for(int i=1;i<=n;i++) { scanf("%d",&a[i]); used[i]=false; mp[a[i]]++; } if(n&1) { printf("0\n"); continue; } int t=1; //加个map判断就是0ms,否则就是tle map<int,int>::iterator it; for(it=mp.begin();it!=mp.end();it++) { if((it->second)%2==1) { t=0; break; } } if(t==0) { printf("0\n"); continue; } printf("%d\n",dfs(n)); } return 0;}
—————————————————————————————————
本文原创自Slience的csdn技术博客。
本博客所有原创文章请以链接形式注明出处。
欢迎关注本技术博客,本博客的文章会不定期更新。
大多数人想要改造这个世界,但却罕有人想改造自己。
世上没有绝望的处境,只有对处境绝望的人。
————By slience
—————————————————————————————————
0 0
- 【HDU】4272 LianLianKan(DFS)
- HDU 4272 LianLianKan DFS+map优化
- hdu 4272 LianLianKan(模拟dfs+map)
- HDU 4272 LianLianKan(模拟)
- HDU 4272 LianLianKan(贪心!!!!!)
- hdu 4272 LianLianKan
- hdu 4272 LianLianKan
- HDU 4272 lianliankan
- HDU 4272 LianLianKan
- Hdu 4272 LianLianKan(贪心+链表)
- HDU 4272LianLianKan 2012长春网络赛F题(思维) 暴力,dfs可以水过,正解是状态压缩dp
- HDU 4272 LianLianKan 状态压缩DP
- HDU 4272 LianLianKan 记忆化搜索
- hdu4272 LianLianKan(dfs)
- 杭电4272 Lianliankan dfs和map的使用
- HDOJ 4272 LianLianKan(简单模拟)
- LianLianKan
- LianLianKan
- PM需积累
- replaceAll(String,String)方法注意
- The Horse Racing
- qt在linux与ok6410上的安装移植(6)——opencv的安装与在qtcreator中的应用
- Linux简介(用户管理操作命令、常用操作命令)
- 【HDU】4272 LianLianKan(DFS)
- perl 处理文件中的一些空字符
- c++基础语法(构造函数初始化列表)
- bzoj1001/BJOI2006 狼抓兔子
- Android开发之旅>>环境搭建:PANIC: Could not open
- MySQL权限分配
- 关于独立按键扫描程序的思考(整合两种算法)
- Hadoop大数据处理读书笔记
- CVPR 2014的文章又出来了,跟不及啊