BFS预处理——UVAlive 7263
来源:互联网 发布:围棋人机对弈软件 编辑:程序博客网 时间:2024/05/29 08:22
题目链接:https://vjudge.net/problem/UVALive-7263
题意:给出2个字符串,字符为1~6,现在给出2种操作,一种是将一种数字全部变为另外一种;一种操作是将某一个数字变成为另一个。求对第二个字符串进行最少多少次操作可以将它变为第一种
分析:我们可以发现对于任意两个字符串的转换过程,第二种操作利用得当总能带来最大的价值,所以一般都是当第二种操作无法转换更多的数字时,我们再考虑第一种操作。简而言之就是,第二种操作完后再考虑第一种操作。对于第二种操作,我们可以看出是一种状态的映射,假设原始状态为6个数字123456对应123456,第二种操作结束后将变成123456对应另外6个数(范围依旧是1~6),所以我们可以用BFS预处理出初始状态到任何可能的状态(大概比
66 小一些,因为有一些状态无法抵达)所需要的花费,然后对于任何给定的字符串,我们枚举所有结束状态,然后计算在这个状态下还需要多少次第一种操作才能转换,然后取最小值即可。
时间复杂度大致为:66∗O(len(str)) AC代码:
/************************************************************************* > File Name: test.cpp > Author: Akira > Mail: qaq.febr2.qaq@gmail.com ************************************************************************/#include<bits/stdc++.h>typedef long long LL;typedef unsigned long long ULL;typedef long double LD;#define MST(a,b) memset(a,b,sizeof(a))#define CLR(a) MST(a,0)#define Sqr(a) ((a)*(a))using namespace std;#define MaxN 100001#define MaxM MaxN*10#define INF 0x3f3f3f3f#define PI 3.1415926535897932384626const int mod = 1E9+7;const double eps = 1e-6;#define bug cout<<88888888<<endl;#define debug(x) cout << #x" = " << x << endl;int base[10] = {1000000, 100000, 10000, 1000, 100, 10, 1};char tar[111], now[111];int digit[10];vector<int> state;int cost[666666];void init(){ MST(cost,-1); int pre = 123456; queue<int> Q; Q.push(pre); state.push_back(pre); cost[pre] = 0; while(!Q.empty()) { int tmp = Q.front(); Q.pop(); for(int i=1;i<=6;i++) { for(int j=1;j<=6;j++) { if(i!=j) { int next = 0; for(int k=1;k<=6;k++) { digit[k] = tmp% base[k - 1] / base[k]; if( digit[k]==i) digit[k] = j; next = digit[k] + next*10; } if(cost[next]!=-1) continue; cost[next] = cost[tmp]+1; Q.push(next); state.push_back(next); } } } } //debug(state.size())}void solve(){ int len = strlen(tar); int ans = len; for(int i=0;i<state.size();i++) { int tmp = state[i]; for(int k=1;k<=6;k++) { digit[k] = tmp% base[k - 1] / base[k]; } int res = cost[tmp]; for(int j=0;j<len;j++) { int to = tar[j]-'0'; int cnt = now[j]-'0'; if( digit[cnt]==to) continue; else res++; } //if(res<ans) debug(res); ans = min(ans, res); } printf("%d\n", ans);}int main(){ init(); while(~scanf("%s%s", tar, now)) { solve(); }}
0 0
- BFS预处理——UVAlive 7263
- UVALive - 7263 Today Is a Rainy Day (BFS预处理)
- UVALive 7263 Today Is a Rainy Day(BFS预处理)
- HDU3533 Escape —— BFS / A*算法 + 预处理
- UVALive 6425 Intercity(bfs)
- UVALive 6658 - Fiasco(BFS)
- UVALive 3695 (博弈 bfs)
- 【BFS && 树】UVALive
- ACM-康托展开+预处理BFS之魔板——hdu1430
- poj_3669_Meteor Shower(BFS+预处理)
- HDU 3567 BFS+预处理
- hdu5637 Transform (bfs+预处理)
- poj 3669 bfs+预处理
- BFS+预处理 POJ
- Travel (BFS + 预处理优化)
- HihoCoder 1251 BFS预处理
- UVALive - 2093 Moving Pegs bfs
- UVALive - 3977 Summits (BFS染色)
- 设计模式之迭代器模式
- Windows 虚拟地址转译(PAE)
- 《现代操作系统(中文第三版)》课后习题——第四章 文件系统
- IDEA git 使用 将当前工程添加为git仓库 add root
- const关键字详细解析
- BFS预处理——UVAlive 7263
- 原来论文摘要并不是摘录原文,而是原文的总结和提炼
- 动态规划-矩阵连乘问题
- php.ini 关闭错误
- 有趣的排序-百度-排序
- OS进程调度及典型调度算法
- zookeeper简易操作
- HTML img 刷新图片的问题
- oscache页面局部缓存----使用