2015北京现场赛UVALive 7263 Today Is a Rainy Day 【bfs】

Today is a rainy day. The temperature is apparently lower than yesterday. Winter is coming. It always
leaves people feeling fatigued and tired.
Lee hasn’t prepared for winter yet. As he wakes up this morning, he looks out of the window.
Yesterday’s shining sunlight can no longer be seen. It is dark outside. The sky looks so heavy that it
may collapse down at any moment. Lee gets out of his bed, shakes his head slightly to make himself
more awake. But it’s of no use for him. Then he goes to the restroom and washes up.
Lee has a class in fifteen minutes. If he sets out immediately, he may gets to the classroom on time.
But he is not in the mood to do so. He decides to skip class and does something more interesting to
train his mind.
He takes out a draft paper and writes a list of digits using a dice. It is obvious that the digits are
all between 1 and 6. And then he applies two kind of modifications to the digits. The first kind is
to modify one digit into another. The second kind is to modify one kind of digits into another. For
example, he can modify “12123” to “12121” using the first kind of modification, or modify “12123” to
“13133” using the second kind of modification. In the process of modification, all digits should be in 123456
After a few modifications, he feels tired but pleased. He’s got a list of digits which is very different
from the original one. Thinking of the next thing to do, Lee becomes a little excited. He is going to
figure out the least number of modifications to transform the final list back to the original one using
the same rules.
Lee made it in a very short time. Can you do this like him?
Input
There are up to 100 test cases.
For each test case, there are two lines containing two lists of digits, representing the original list
and the final list in order. The digits are all between 1 and 6. It is guaranteed that two lists are of
same length. The length will not be greater than 110.
Output
For each test case, output one integer, the answer.
Sample Input
22345611
12345611
2234562221
1234561221
2234562211
1234561111
22345622112
12345611111
654321654321654321654321
123456123456123456123456
Sample Output
1
2
3
3
11

万万没想到这个题居然是bfs,本来以为是数位dp ⊙﹏⊙b,讲道理很像是poj3126prime path

题意:给定两个字符串(只含有123456)问最少用几步可以从后者变换到前者。两种操作:1.每次变一个2.每次把所有某种变成另一种

很明显应该多做后一种的操作才会使得整体最优,每个数字都可以有5种变换,一共6种原始字符,想到用bfs预处理123456变成任何情况的步数,定义dx[情况序号]=变换后的结果   stp[情况序号]=步数   step[变换后的结果]=步数

然后将给定的字符串带入这些情况中,剩下的用操作1,找最小的步数

#include <iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
using namespace std;
int tmp[11],step[1111111];
int a[111],b[111],c[11],dx[1111111],stp[1111111];
int tot,ans;
int len;
queue<int>Q;
string x1,x2;
void bfs()
{
    tmp[7]=1;
    for(int i=6;i>=1;i--) tmp[i]=tmp[i+1]*10;
    memset(step,0x3f3f3f3f,sizeof(step));
    step[123456]=0;
    Q.push(123456);
    tot=1;
    dx[tot]=123456;
    while(!Q.empty())
    {
        int d=Q.front();
        Q.pop();
        for(int i=1;i<=6;i++)
        {
            for(int j=1;j<=6;j++)
            {
                if(i==j)continue;
                int tt=0;
                for(int k=1;k<=6;k++)
                {
                    int t=(d%tmp[k])/tmp[k+1];
                    if(t==i)
                        tt=tt*10+j;
                    else tt=tt*10+t;
                }
                if(step[d]+1<step[tt])
                {
                    step[tt]=step[d]+1;
                    dx[++tot]=tt;
                    stp[tot]=step[tt];
                    Q.push(tt);
                }
            }
        }
    }
    //for(int i=1;i<=tot;i++)printf("i=%d,dx=%d\n",i,dx[i]);
}
void solve()
{
  //  printf("tot=%d\n",tot);
    for(int i=1;i<=tot;i++)
    {
        for(int j=1;j<=len;j++)
            a[j]=x1[j-1]-'0';
        for(int j=1;j<=6;j++)
            c[j]=(dx[i]%tmp[j])/tmp[j+1];
        for(int j=1;j<=len;j++)
            a[j]=c[a[j]];
        int sum=0;
        for(int j=1;j<=len;j++)
            if(a[j]!=b[j])
                sum++;
       //printf("sum=%d,ans=%d\n",sum,ans);
        ans=min(ans,sum+stp[i]);
    }
}
int main()
{
    //freopen("cin.txt","r",stdin);
    bfs();
    while(cin>>x2)
    {
        cin>>x1;
        len=x1.size();
        for(int i=1;i<=len;i++)
            a[i]=x1[i-1]-'0',b[i]=x2[i-1]-'0';
        ans=0x3f3f3f3f;
        solve();
        printf("%d\n",ans);
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值