hihocoder 1426 What a Ridiculous Election
来源:互联网 发布:淘宝店以开启,怎么找货 编辑:程序博客网 时间:2024/05/20 09:23
题目链接:What a Ridiculous Election
题目大意:有三种操作:
1、交换相邻两个数
2、对某一个数位上的数,数值加一,对十求余,这个操作最多用3次
3、对某一个数位上的数,数值乘二,对十求余,这个操作最多用2次
现在问你能不能从12345变成给定的数字(五位数),能的话输出最少的操作数
题目思路:BFS预处理一下,res数组记一下数字,剩余的操作2,剩余的操作3,然后模拟的时候好好做一下就好了
#include <map>#include <set>#include <cmath>#include <stack>#include <queue>#include <vector>#include <cstdio>#include <cstdlib>#include <cstring>#include <iostream>#include <algorithm>using namespace std;const int maxn = 1e5+10;const int inf = 0x3f3f3f3f;char s[10];bool flag,vis[10];int res[maxn][5][5];struct node{ int a[10],sec,thr,step; bool operator < (const node &a)const{ return step > a.step; }};node target;int check(node now){ int num = 0; for(int i = 1;i <= 5;i++) if(now.a[i] != target.a[i]) return 0; return 1;}int turn(node now){ int sum = 0; for(int i = 1;i <= 5;i++){ sum += now.a[i]; sum *= 10; } sum /= 10; return sum;}void bfs(node now){ priority_queue<node>q; q.push(now); int tmp = turn(now); res[tmp][now.sec][now.thr] = 0; node u,next; bool flag; while(!q.empty()){ u = q.top(); q.pop(); for(int i = 2;i <= 5;i++){ next = u; next.step++; swap(next.a[i],next.a[i-1]); int p = turn(next); if(next.step >= res[p][next.sec][next.thr]) continue; q.push(next); res[p][next.sec][next.thr] = next.step; } if(u.sec > 0){ for(int i = 1;i <= 5;i++){ next = u; next.sec--; next.step++; next.a[i] = (next.a[i]+1)%10; int p = turn(next); if(next.step >= res[p][next.sec][next.thr]) continue; q.push(next); res[p][next.sec][next.thr] = next.step; } } if(u.thr > 0){ for(int i = 1;i <= 5;i++){ next = u; next.thr--; next.step++; next.a[i] = (next.a[i]*2)%10; int p = turn(next); if(next.step >= res[p][next.sec][next.thr]) continue; q.push(next); res[p][next.sec][next.thr] = next.step; } } }}int main(){ node now; now.sec = 3,now.thr = 2,now.step = 0; for(int i = 1;i <= 5;i++) now.a[i] = i; memset(res,inf,sizeof(res)); bfs(now); while(~scanf("%s",s)){ for(int i = 1;i <= 5;i++) target.a[i] = s[i-1]-'0'; int p = turn(target); int ans = inf; for(int i = 0;i <= 3;i++) for(int j = 0;j <= 2;j++) ans = min(ans,res[p][i][j]); if(ans == inf) puts("-1"); else printf("%d\n",ans); } return 0;}
阅读全文
0 0
- hihocoder 1426 What a Ridiculous Election
- HihoCoder 1426 What a Ridiculous Election (BFS)
- Hihocoder 1426 E. What a Ridiculous Election (BFS)
- hihocoder1426 What a Ridiculous Election(bfs预处理)
- 2016 ACM-ICPC 亚洲区域赛北京站E题 What a Ridiculous Election (BFS预处理)
- 2016北京区域赛E UVAlive 7672 题目:What a Ridiculous Election 带约束条件的BFS
- What a Simple Research HihoCoder
- hihocoder 1425 What a Beautiful Lake
- hihocoder 1427 What a Simple Research
- hihocoder 1425What a Beautiful Lake(实验专用)
- Election
- Election
- hihoCoder #1000: A+B
- hihocoder 八十八 A
- hihocoder 练习 A+B
- hihoCoder 1000 A+B
- hihocoder-#1338 : A Game
- HihoCoder--A+B解答
- 初级测试工程师面试总结
- fl2440使能linux-3.0内核自带的led驱动,并测试
- PAT 甲级 Consecutive Factors (20)
- 菜鸟的自定轮播图
- 计算几何基础算法
- hihocoder 1426 What a Ridiculous Election
- 如何配置 Sublime Text 的 LaTeXTools?
- 八数码 BFS+HASH
- HDU4553(线段树区间合并)
- colorAccent,colorPrimary,colorPrimaryDark做什么的?
- 牛客网-剑指offer-12-数值的整数次方
- Make Palindrome
- 面试题:整数反序
- Java 用两个队列实现一个栈,要求top()的时间复杂度为O(1)