hdu 4474 bfs

来源:互联网 发布:节奏大师网络拦截 编辑:程序博客网 时间:2024/06/14 13:30
#include <iostream>#include <string>#include <string.h>#include <stdio.h>#include <algorithm>#include <queue>#include <stdlib.h>class HDU4474{    public :    const static int N = 10008 ;    bool del[10] ;    bool vis[N] ;    int father[N] ;    char text[N] ;    int cas = 1 ;    int n ;    void solve(){        int x  , m  ;        while(scanf("%d%d" , &n , &m) != EOF){            std::fill(del  , del + 10 , false) ;            for(int i = 0 ; i < m ; i++){                scanf("%d" , &x) ;                del[x] = true ;            }            printf("Case %d: " ,  cas++ ) ;            if(bfs()) printf("%s\n" , ans().c_str() ) ;            else printf("%d\n" , -1) ;        }    }    bool bfs(){        std::fill(vis , vis + N , false) ;        std::queue<int> q  ;        q.push(0)  ;        while(! q.empty()){            int u = q.front() ;            q.pop() ;            for(int i = 0 ; i <= 9 ; i++){                if(del[i] || (u == 0 && i == 0)) continue ;                int mod = (u * 10 + i ) % n ;                if(vis[mod]) continue ;                vis[mod] = true ;                text[mod] =  ('0' + i) ;                father[mod] = u ;                q.push(mod) ;                if(mod == 0) return true ;            }        }        return false ;    }    std::string ans(){        std::string sb = "" ;        int u = 0 ;        while(u != 0 || sb.length() == 0){            sb += text[u]  ;            u = father[u] ;        }        std::reverse(sb.begin() , sb.end())  ;        return sb ;    }} ;int main(){    HDU4474  task ;    task.solve() ;    return 0;}

0 0