zoukankan      html  css  js  c++  java
  • 产生数

    参考来源:http://blog.csdn.net/hbhszxyb/article/details/19048825

    题目链接https://www.luogu.org/problemnew/show/P1037

    或者oj.noi.cn里面题号1126.

    题目描述
    给出一个整数n(n<=2000)和k个变换规则(k≤15)。规则:
    ① 1个数字可以变换成另1个数字;
    ② 规则中,右边的数字不能为零。
    例如:n=234,k=2规则为
    2 → 5
    3 → 6
    上面的整数234经过变换后可能产生出的整数为(包括原数)234,534,264,564共4种不同的产生数。
    求经过任意次的变换(0次或多次),能产生出多少个不同的整数。仅要求输出不同整数个数。
    输入格式
    n
    k
    x1 y1
    x2 y2
    … …
    xn yn

    输出
    格式为一个整数(满足条件的整数个数)。
    样例输入
    234
    2
    2 5
    3 6
    样例输出

    4

     1 #include<iostream>
     2 #include<queue>
     3 #include<cstring>
     4 #include<cstdio>
     5 using namespace std;
     6 int n,k;
     7 int a[20],b[20];
     8 bool f[10000];//数组千万不要开小了,因为数会变,4位数至少开1万 
     9 queue<int> q;
    10 void init();
    11 void work();
    12 int main()
    13 {
    14     //freopen("produce5.in","r",stdin);
    15     init();
    16     work();    
    17     return 0;
    18 }
    19 void init()
    20 {
    21     memset(f,0,sizeof(f));
    22     cin>>n;
    23     cin>>k;
    24     for(int i=1;i<=k;i++) cin>>a[i]>>b[i];    
    25 }
    26 void work()
    27 {
    28     int tot=0;
    29     f[n]=true;//标记n已出现 
    30     q.push(n);//把n进队 
    31     tot++;    
    32     while(!q.empty())//队列非空 
    33     {
    34         int x=q.front();//对队首的每一位进行操作        
    35         int wei=1;//记录当前处理的是那一位,第一位时为1,第二位时时10……100                
    36         while(x>0)
    37         {
    38             int temp=x%10;//取个位            
    39             for(int i=1;i<=k;i++)
    40             {
    41                 if(temp==a[i])
    42                 {
    43                     int p=q.front()+(b[i]-temp)*wei;//好好理解                    
    44                     if(!f[p])//如果p没出现过,进队,并设为已出现 
    45                     {                        
    46                         q.push(p);
    47                         f[p]=true;
    48                         tot++;                        
    49                     }
    50                 }
    51             }
    52             x=x/10;            
    53             wei*=10;
    54         }
    55         q.pop();
    56     }
    57     cout<<tot<<endl;
    58 }
  • 相关阅读:
    查看git submodule更改
    /var/lib/docker空间占用过大迁移
    docker -修改容器
    docker重命名镜像repository和tag
    方法的重写、重载。
    方法的声明与使用。
    二维数组。
    标准输入输出流概述和输出语句。
    冒泡排序法。
    IO流,对象操作流优化。
  • 原文地址:https://www.cnblogs.com/huashanqingzhu/p/7159429.html
Copyright © 2011-2022 走看看