Codeforces - 65D - Harry Potter and the Sorting Hat - 简单搜索
https://codeforces.com/problemset/problem/65/D
哈利波特!一种新思路的状压记忆化dfs,记得每次dfs用完要减回去。而且一定是要在dfs外部进行加减!防止在中间return的时候忘记弄回来。用哈希记录状态实现真正的记忆化。
#include<bits/stdc++.h> using namespace std; #define ll long longint n; char s[10005];ll getID(int a[]){ll res=0;for(int i=0;i<4;i++){res=res*10000+a[i];}return res; }int a[4]={}; int c[4]={};set<ll> se;int dfs(int i){ll ID=getID(a);if(se.count(ID))return 0;else{se.insert(ID);//printf("%d-%lld\n",i,ID);if(i==n){int minnum=*min_element(a,a+4);for(int j=0;j<4;j++){if(a[j]==minnum){c[j]=1;}}return 0;}}if(s[i]!='?'){switch (s[i]){case 'G':a[0]++;dfs(i+1);a[0]--;break;case 'H':a[1]++;dfs(i+1);a[1]--;break;case 'R':a[2]++;dfs(i+1);a[2]--;break;case 'S':a[3]++;dfs(i+1);a[3]--;}}else{int minnum=*min_element(a,a+4);for(int j=0;j<4;j++){if(a[j]==minnum){//cout<<"j="<<j<<endl;a[j]++;dfs(i+1);a[j]--;}}} }int main(){scanf("%d",&n);scanf("%s",s);dfs(0);//printf("%d\n",se.size());if(c[0])puts("Gryffindor");if(c[1])puts("Hufflepuff");if(c[2])puts("Ravenclaw");if(c[3])puts("Slytherin");}
转载于:https://www.cnblogs.com/Yinku/p/10280299.html
Codeforces - 65D - Harry Potter and the Sorting Hat - 简单搜索相关推荐
- Codeforces Round #699 (Div. 2) E.Sorting Books(贪心+DP / 线段树)超高质量题解,看不懂来打我 ~
整理的算法模板合集: ACM模板 点我看算法全家桶系列!!! 实际上是一个全新的精炼模板整合计划 E - Sorting Books 一排书架上有 nnn 本书排成一排,每本书上有一个颜色 aia_i ...
- CodeForces 258D Little Elephant and Broken Sorting(期望)
CF258D Little Elephant and Broken Sorting 题意 题意翻译 有一个\(1\sim n\)的排列,会进行\(m\)次操作,操作为交换\(a,b\).每次操作都有\ ...
- 【CodeForces - 205B 】Little Elephant and Sorting (思维)
题干: The Little Elephant loves sortings. He has an array a consisting of n integers. Let's number the ...
- Codeforces Round #336 (Div. 2) D. Zuma 记忆化搜索
D. Zuma 题目连接: http://www.codeforces.com/contest/608/problem/D Description Genos recently installed t ...
- Codeforces Round #297 (Div. 2)D. Arthur and Walls 搜索bfs
题目链接: http://codeforces.com/contest/525/problem/D 题意 给你一个n*m的田地,有一些*的地方是可以移除变成"."的,然后问你移除最 ...
- CODEFORCES 272C Dima and Staircase 细节理解题+简单技巧
传送门:http://codeforces.com/problemset/problem/272/C C. Dima and Staircase time limit per test 2 secon ...
- Codeforces Round #406 (Div. 1) A. Berzerk 记忆化搜索
A. Berzerk 题目连接: http://codeforces.com/contest/786/problem/A Description Rick and Morty are playing ...
- Codeforces Round #540 (Div. 3)--A. Water Buying(简单思维题-有点坑)
A. Water Buying 题目链接http://codeforces.com/problemset/problem/1118/A time limit per test:1 second mem ...
- codeforces 贪心+优先队列_算法基础04-深度优先搜索、广度优先搜索、二分查找、贪心算法...
深度优先搜索DFS.广度优先搜索BFS 比较 拿谚语打比方的话,深度优先搜索可以比作打破砂锅问到底.不撞南墙不回头:广度优先搜索则对应广撒网,多敛鱼 两者没有绝对的优劣之分,只是适用场景不同 当解决方 ...
最新文章
- C语言如何实现随机打印24个母,菜鸟求助,写一个随机输出26个英文字母的程序...
- (最新最全)windows使用anaconda安装pytorch进行深度学习并使用GPU加速
- CISCO-生成树-安全保护配置
- java B2B2C Springboot多租户电子商城系统-spring-cloud-eureka-consumer...
- 服务器异常下电文件系统,SUN服务器Solaris异常情况下恢复操作步骤(8页)-原创力文档...
- mysql事务所_MySQL事务,这篇文章就够了
- 学微电子要学计算机哪种语言,微电子学与计算机,模板.doc
- 转载:性能计数器分析
- [UITableView]简介
- linux 内核 内存管理 bootmem alloctor 的初始化
- zgc,JDK8有类似的shenandoah,华为移植到JDK11
- Cannot find module ‘https-proxy-agent‘
- PDF转CAD格式软件下载及使用教程
- jquery的点击事件驱动获取值
- Python数据分析案例07——二手车估价(机器学习全流程,数据清洗、特征工程、模型选择、交叉验证、网格搜参、预测储存)
- 【一级讲解】不可思议唤来不可思议β——数学排列组合
- 电脑双屏变单屏后,界面显示问题
- mcafee 8.5杀毒软件下载
- 鉴源实验室丨汽车电子架构和CAN网络基础
- zero eclipse_全球首款真无孔机!魅族zero亮相:支持屏幕发声和18W无线快充