2016北京区域赛E UVAlive 7672 题目:What a Ridiculous Election 带约束条件的BFS
来源:互联网 发布:英语会被取代知乎 编辑:程序博客网 时间:2024/05/20 09:21
这几天做的北京的模拟,当时被这个题给卡住了,自己写的BFS,一直WA,结束后对拍才找到自己的致命错误
自己做的时候直接通过最优情况去推导最有情况,导致自己掉进死胡同了
这里举一个例子,比如变成01034
依次是,12345,12354,12534,12034,21034,31034,41034,51034,01034
可以见到这里的51034用了7步,而实际上51034最优策略是6步,这里为什么用7步的51034,而不用6步的51034呢?
是因为变成7步的51034,用了一次乘法,三次加法,变成6布的51034,用了两次乘法,三次加法,而变成01034,
需要一步乘法,可是6步的51034不能再使用乘法了
所以这个约束条件就导致了不能用最优的低步数答案去推到最优的高步数答案
这里的解决方法应该是把用了三次加法,一次乘法的51034和用了三次加法,两次乘法的51034分别记录下来
这里开一个数组ans[num][ins][dou],分别代表这个数,加法次数,乘法(乘二)次数
祈福 FREE LOOP 西安获得奖牌,加油!!!
附上改过之后AC的代码
#include <queue>#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>using namespace std;const int maxn = 100000 + 10;const int INF = 0x3f3f3f3f;struct mynum{ int num; int tt;};struct sta{ string str; int time; int dou; int ins;};int ans[maxn][5][5];///[number][ins][dou]void bfs(string str1, int time1, int dou1, int ins1){ queue<sta> q; q.push(sta{str1, time1, dou1,ins1}); while(!q.empty()) { sta top = q.front(); q.pop(); int n = 0; int ten = 1; for(int i = 4; i >= 0; i--) { n += (int)(top.str[i] - '0') * ten; ten = ten * 10; } if(ans[n][top.ins][top.dou] <= top.time) continue; ans[n][top.ins][top.dou] = min(ans[n][top.ins][top.dou], top.time); /// printf("n : %d : time : %d\n" ,n ,top.time); if(top.dou >= 1)///乘二 { string tmp = top.str; for(int i = 0; i < 5; i++) { int a = (int)(top.str[i] - '0'); if(a == 0) continue; a = (a * 2) % 10; top.str[i] = a + '0'; q.push(sta{top.str, top.time+1, top.dou-1, top.ins}); top.str = tmp; } } for(int i = 0 ; i < 4; i++)///相邻交换 { char c; string tmp = top.str; /// cout<<top.str<<" "; c = top.str[i]; if(top.str[i] == top.str[i+1]) continue; top.str[i] = top.str[i+1]; top.str[i+1] = c; /// cout<<top.str<<endl; q.push(sta{top.str, top.time + 1, top.dou, top.ins}); top.str =tmp; } if(top.ins >= 1)///自增加一 { string tmp = top.str; for(int i = 0; i < 5; i++) { int a = (int)(top.str[i] - '0'); a = (a + 1)% 10; top.str[i] = a + '0'; /// cout<<str<<endl; q.push(sta{top.str, top.time+1, top.dou, top.ins-1}); top.str = tmp; } } }}int main(){ /// freopen("2.out","w",stdout); memset(ans, INF, sizeof(ans)); string sss = "12345"; bfs(sss, 0, 2, 3); string myans; /// cout<< " total:" << cnt <<endl; /// for(int i = 0; i <cnt; i++) printf("%d %d\n", a[i].num, a[i].tt); while(cin>>myans) { int n = 0; int ten = 1; //myans.length()-1 for(int i = myans.length()-1; i >= 0; i--) { n += (int)(myans[i] - '0') * ten; ten = ten * 10; } int freeloop = INF; for(int i = 0; i <= 3; i++) { for(int j = 0; j <= 2; j++) { freeloop = min(freeloop, ans[n][i][j]); } } if(freeloop == INF) { printf("-1\n"); } else { printf("%d\n", freeloop); } /// n++; } return 0;}
阅读全文
0 0
- 2016北京区域赛E UVAlive 7672 题目:What a Ridiculous Election 带约束条件的BFS
- 2016 ACM-ICPC 亚洲区域赛北京站E题 What a Ridiculous Election (BFS预处理)
- Hihocoder 1426 E. What a Ridiculous Election (BFS)
- hihocoder1426 What a Ridiculous Election(bfs预处理)
- HihoCoder 1426 What a Ridiculous Election (BFS)
- hihocoder 1426 What a Ridiculous Election
- UVALive 7269 (构造) 15北京区域赛。。
- 2014 北京区域赛题目
- 2015北京现场赛UVALive 7263 Today Is a Rainy Day 【bfs】
- 2016亚洲区域赛现场赛北京赛区e题
- UVALive 7269 Snake Carpet 构造 2015北京区域赛的铜牌题
- 2011年北京区域赛A题
- 2015 北京区域赛现场赛1C 裸的BFS
- 2016icpc北京网络赛 E A Research on "The Hundred Family Surnames" 【Lca+树的直径】
- Hihocoder 1259 :A Math Problem(2015 北京区域赛 K,二进制的数位dp)
- 2015 北京区域赛 Today Is a Rainy Day(暴搜)
- MY SQL的字段带约束条件插入
- hiho1251Today Is a Rainy Day【2015北京现场赛】BFS
- muduo网络库学习(一)对io复用的封装Poller,面向对象与基于对象
- aes对称加密 -- node版
- Android开发之--布局中包含listview时 如何去除listview的默认置顶(焦点)
- 在linux搭建集群(nginx)
- 关键字标示符包的命名注释常量进制转换变量基本数据类型引用数据类型数据类型转换
- 2016北京区域赛E UVAlive 7672 题目:What a Ridiculous Election 带约束条件的BFS
- 漫步最优化二十九——D.S.C.算法
- table去掉边框或者横纵分隔线
- codevs 2218 补丁 vs 错误 (最短路)
- 题目33:蛇形填数
- 并查集算法
- AngularJS表单的查询与增加
- Deep Linking 深度链接 2
- 项目Shiro注解权限角色使用