UVALive - 7263 Today Is a Rainy Day(bfs)
题意
给两个等长的只含数字1,2,3,4,5,6的字符串s(|s|≤110),有两种操作:
- 把一个位置的数字换成另一个数字,换成的数字也只能是1到6
- 把这个字符串中相同的数字都换成另一种数字
应用上面的两种操作把第二个字符串转换成第一个字符串至少需要多少次操作?
分析
首先尽可能多的进行第二次操作一定是最优的。
对于第二种操作,我们可以预处理出来变换到每个状态的最短步数,也就是用bfs跑。以123456标记为初始状态state,然后每次选择一个要变换的数字以及变换成的数字。
那么,如何求解从第二个字符串变到第一个字符串的最少步数呢?根据上面的状态定义,初始状态为123456(位置1对应数字1,位置2对应数字2....),那么每到一个新的状态nxt,先用数组存起来对应位置的change[],遍历s2,如果change[s2[j]]!=s1[j](change[s2[j]]表示s2[j]在nxt状态下变成了change[s2[j]]),则需要进行操作1,tmp++。
那么遍历所有状态,ans=min(ans,tmp+step[nxt])。
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <math.h>
#include <queue>
using namespace std;
const int MAX_N = ;
int total = , step[MAX_N], res[MAX_N], pw10[];
queue<int> que;
char s1[], s2[];
void bfs() {
pw10[] = ;
for (int i = ; i <= ; ++i) { pw10[i] = * pw10[i - ]; }
memset(step, -, sizeof (step));
int st = ;
step[st] = total, res[total++] = st;
que.push(st);
while (!que.empty()) {
int cur = que.front();
que.pop();
for (int i = ; i <= ; ++i) {
for (int j = ; j <= ; ++j) {
if (i == j) continue;
int nxt = ;
for (int k = ; k >= ; --k) {
int p = cur / pw10[k] % ;
if (p == i) nxt = nxt * + j;
else nxt = nxt * + p;
}
if (step[nxt] != -) continue;
step[nxt] = step[cur] + ;
res[total++] = nxt;
que.push(nxt);
}
}
}
}
int main() {
bfs();
while (~scanf("%s%s", s1, s2)) {
int len = strlen(s1);
int ans = len, change[];
for (int i = ; i < total; ++i) {
int cur = res[i], tmp = ;
for (int j = , k = ; j >= ; --j) {
change[k++] = cur / pw10[j] % ;
}
for (int j = ; j < len; ++j) {
int to = change[s2[j] - ''];
if (to != s1[j] - '') tmp++;
}
ans = min(ans, tmp + step[cur]);
}
printf("%d\n", ans);
}
return ;
}
UVALive - 7263 Today Is a Rainy Day(bfs)的更多相关文章
- LA 7263 Today Is a Rainy Day bfs+暴力 银牌题
7263 Today Is a Rainy Day Today is a rainy day. The temperature is apparently lower than yesterday. ...
- What a Ridiculous Election UVALive - 7672 (BFS)
题目链接: E - What a Ridiculous Election UVALive - 7672 题目大意: 12345 可以经过若干次操作转换为其它五位数. 操作分三种,分别为: 操作1:交 ...
- UVALive 5066 Fire Drill BFS+背包
H - Fire Drill Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Submit Sta ...
- UVALive 4025 Color Squares(BFS)
题目链接:UVALive 4025 Color Squares 按题意要求放带有颜色的块,求达到w分的最少步数. //yy:哇,看别人存下整个棋盘的状态来做,我什么都不想说了,不知道下午自己写了些什么 ...
- UVALive 5066 Fire Drill --BFS+DP
题意:有一个三维的地图,有n个人被困住,现在消防队员只能从1楼的一个入口进入,营救被困者,每一个被困者有一个价值,当消防队员找到一个被困者之后,他可以营救或者见死不救,如果救的话,他必须马上将其背到入 ...
- UVALive 6665 Dragonâs Cruller --BFS,类八数码问题
题意大概就是八数码问题,只不过把空格的移动方式改变了:空格能够向前或向后移动一格或三格(循环的). 分析:其实跟八数码问题差不多,用康托展开记录状态,bfs即可. 代码: #include <i ...
- UVALive 7297 bfs
题意 一个小偷偷到了项链 他想知道自己是否可以逃出去 地图中有一个小偷 一个警察 警察有一条狗 一开始 小偷和警察的移动速度都是1 当警察走到小偷经过过的地方时 警察会有一条狗嗅到小偷的气味并且以2的 ...
- UVALive 7297 Hounded by Indecision BFS
题目链接:Hounded by Indecision 题意:map中给出小偷的位置,警察的位置.警察有一只狗,开始的时候警察和狗一起行动,也就是看做一个格子,当警察遇见小偷走过的格子时,狗就会嗅到它的 ...
- UVALive 3956 Key Task (bfs+状态压缩)
Key Task 题目链接: http://acm.hust.edu.cn/vjudge/contest/129733#problem/D Description The Czech Technica ...
随机推荐
- Windows samba history
https://blogs.technet.microsoft.com/josebda/2013/10/02/windows-server-2012-r2-which-version-of-the-s ...
- ecshop2.73修改密码方法|ecshop2.73修改密码方法
ecshop2.73修改密码方法|ecshop2.73修改密码方法 ECSHOP教程/ ecshop教程网(www.ecshop119.com) 2012-09-09 ecshop2.73正式版后 ...
- Oracle 使用PLSQL 导出 一个表的insert 语句
1. 使用工具 plsql . GUI的方法,如图示 2. 操作界面 3. 然后就看到了插入语句
- php常用扩展安装
####memcache wget http://pecl.php.net/get/memcache-2.2.7.tgztar xf memcache-2.2.7.tgz cd memcache-2. ...
- C-Lodop获取打印机列表Create_Printer_List
C-Lodop获取打印机列表Create_Printer_List,此方法Lodop不支持,是C-Lodop特有的函数,客户端本地打印单独用c-lodop,或集中打印等,可以获得本机或云主机的打印机列 ...
- C# TreeView 连续点击 不触发AfterCheck事件
创建一个类 TreeView2 namespace System.Windows.Forms { public class TreeView2 : TreeView { protected overr ...
- day6 三级菜单
#__author__: Administrator #__date__: 2018/7/12 china = { "shandong":{ "linyi":[ ...
- Huawei运维记录
Huawei运维记录 01 Huawei运维记录-AC6005-8AP设备启动界面 02 Huawei运维记录-AC6005-8AP添加授权码 03 Huawei运维记录-AC6005版本升级步骤
- 【BZOJ2671】Calc(莫比乌斯反演)
[BZOJ2671]Calc 题面 BZOJ 给出N,统计满足下面条件的数对(a,b)的个数: 1.\(1\le a\lt b\le N\) 2.\(a+b\)整除\(a*b\) 我竟然粘了题面!!! ...
- THUWC2019 摸鱼记
Day1 菜狗选手无人权,衣服没有海星,狗牌手写全糊,餐票不发刷卡,住宿自理宾馆. 人菜没办法. 感受到了自己智商低 不想写了 想原地退役 不知道还要不要走下去