• 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. 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 {1, 2, 3, 4, 5, 6}; 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 ACM-ICPC Live Archive: 7263 – Today Is a Rainy Day 2/2 Sample Output 1 2 3 3 11

    题目链接:给你两个字符由1到6组成的a,b字符串(长度<=110),现在你有两种操作,1.将其中某个字符改成另一个,2.将某种字符改成另一种。求从b字符串变换到a字符串所需要的最少步数。

    #include <cstdio>
    #include <iostream>
    #include <algorithm>
    #include <cstring>
    #include <iostream>
    #include <cmath>
    #include <map>
    #include <stack>
    #include <queue>
    #include <vector>
    #include <bitset>
    #include <set>
    #define MM(a,b) memset(a,b,sizeof(a));
    #define inf 0x3f3f3f3f
    using namespace std;
    typedef long long ll;
    #define CT continue
    #define SC scanf
    const int N=1e6;
    int dp[N],state[N];
    char sa[120],sb[120];
    int cnt=0,x[7],savex[7],len;
    
    int getn()
    {
       int res=0;
       for(int i=1;i<=6;i++) res=res*10+x[i];
       return res;
    }
    
    queue<int> q;
    void presolve()
    {
       for(int i=1;i<=6;i++) x[i]=i;
       MM(dp,inf);
       state[++cnt]=getn();
       dp[state[cnt]]=0;
       q.push(state[cnt]);
       while(q.size()){
           int u=q.front();q.pop();
           for(int i=6;i>=1;i--) {x[i]=u%10;u/=10;}
           memcpy(savex,x,sizeof(x));
           int ori=getn();
           for(int i=1;i<=6;i++)
             for(int j=1;j<=6;j++){
                memcpy(x,savex,sizeof(x));
                for(int k=1;k<=6;k++)
                    if(x[k]==i) x[k]=j;
                int lat=getn();
                if(dp[lat]>dp[ori]+1){
                   if(dp[lat]==inf) state[++cnt]=lat;
                   dp[lat]=dp[ori]+1;
                   q.push(lat);
                }
             }
       }
    }
    
    int f[8][8],num[8];
    int main()
    {
        presolve();
        while(~SC("%s%s",sa,sb)){
            int ans=inf;
            MM(f,0);MM(num,0);
            for(int i=0;sa[i]!='';i++){
                int a=sa[i]-'0',b=sb[i]-'0';
                f[b][a]++;
                num[b]++;
            }
           for(int i=1;i<=cnt;i++){
                int step=dp[state[i]],st=state[i];
                for(int i=6;i>=1;i--) {x[i]=st%10;st/=10;}
                for(int i=1;i<7;i++)  {
                   step+=num[i]-f[i][x[i]];
                }
                ans=min(ans,step);
            }
            printf("%d
    ",ans);
        }
        return 0;
    }
    

      分析:题解链接

  • 相关阅读:
    PHP入门:在Windows中安装PHP工作环境
    Oracle数据库优化的经验总结
    引入js和css文件的总结
    PHP的加密解密字符串函数
    js+html5双人五子棋(源码下载)
    JAVA的网络编程基础概念
    asp.net的code-Behind技术
    10个调试Java的技巧
    Oracle基础 各种语句的定义格式
    CSS行高line-height的理解
  • 原文地址:https://www.cnblogs.com/smilesundream/p/5886505.html
Copyright © 2020-2023  润新知