UVALive
来源:互联网 发布:湖南大学校园网域名 编辑:程序博客网 时间:2024/05/22 00:52
参考:http://blog.csdn.net/qq_18661257/article/details/52896203
题目:https://vjudge.net/contest/194842#problem/C
给定两个字符串s1和s2,将s2变为s1,变化操作有以下两种: 将一个位置上的数字变为另外一个数字 将一种数字变为另外一种数
自己的bfs居然写炸了。。
#include<bits/stdc++.h>using namespace std;#define inf 0x3f3f3f3f#define N 50005char s1[N],s2[N];int dp[N];int id(int c[]) { int ret = 0; for(int i = 0; i < 6; i ++) { ret = ret * 6 + c[i]; } return ret;}void rid(int s, int c[]) { for(int i = 5; i >= 0; i --) { c[i] = s % 6; s /= 6; }}void BFS() { memset(dp, 0x3f, sizeof(dp)); int c[6] = {0, 1, 2, 3, 4, 5}, s = id(c), tmp[6]; queue<int>Q; dp[s] = 0; Q.push(s); while(!Q.empty()) { int s = Q.front(); Q.pop(); rid(s, c); for(int i = 0; i < 6; i ++) { for(int j = 0; j < 6; j ++) { memcpy(tmp, c, sizeof(tmp)); for(int k = 0; k < 6; k ++) { if(tmp[k] == i) { tmp[k] = j; } } int ns = id(tmp); if(dp[ns] > dp[s] + 1) { dp[ns] = dp[s] + 1; Q.push(ns); } } } }}int num[10],step[10][10];int main(){ BFS(); while(~scanf("%s",s1)){ memset(num,0,sizeof(num)); memset(step,0,sizeof(step)); scanf("%s",s2); int n=strlen(s1); int ans=inf; for(int i=0;i<n;++i){ int a=s1[i]-'1',b=s2[i]-'1'; num[b]++; step[b][a]++; } for(int i=0;i<N;++i){ int sum=dp[i]; int tmp[10]; rid(i,tmp); for(int j=0;j<6;++j) sum+=num[j]-step[j][tmp[j]]; ans=min(ans,sum); } printf("%d\n",ans); }}
阅读全文
0 0
- UVALive
- UVALive
- UVALive
- UVALive
- UVALive
- UVALive
- UVALive
- UVALive
- UVALive
- UVALive
- UVALive
- UVALive
- UVALive
- UVALive
- UVALive
- UVALive
- UVALive
- UVALive
- UML中的事物
- 如何拼接GET请求的参数
- 模板方法模式学习和思考
- 畅通工程之局部最小花费问题(35 分)
- Miss:和闺蜜一起换一种方式生活
- UVALive
- java Math类
- python 重定向到文件
- Leetcode712. 计算两个字符串删除任意字符后使二者相等的最小删除字符和
- 10.28/29上课内容总结
- python3 字符集编码以及python3 乱码问题
- 安卓学习之解决Spinner不显示的问题
- 395. Longest Substring with At Least K Repeating Characters
- 第二周周总结