首页 > 代码库 > zoj1530 bfs

zoj1530 bfs

 1 //Accepted  zoj1530  270ms 40008KB 2 #include <cstdio> 3 #include <cstring> 4 #include <iostream> 5 #include <queue> 6 using namespace std; 7 const int imax_n = 205; 8 string a[imax_n]; 9 int vis[imax_n];10 queue<int > q;11 queue<string >sq;12 void bfs(int n)13 {14     if (a[n]!="") return ;15     while (!q.empty()) q.pop();16     while (!sq.empty()) sq.pop();17     q.push(1);18     sq.push("1");19     while (1)20     {21         int x=q.front();22         q.pop();23         string s=sq.front();24         sq.pop();25         if (x%n==0)26         {27             a[n]=s;28             return ;29         }30         q.push(10*x%n);31         sq.push(s+"0");32         q.push((10*x+1)%n);33         sq.push(s+"1");34     }35 }36 int main()37 {38     int n;39     while (scanf("%d",&n),n)40     {41         bfs(n);42         cout<<a[n]<<endl;43     }44     return 0;45 }
View Code
 1 //Accepted  zoj1530 110ms 4544kB 2 #include <cstdio> 3 #include <cstring> 4 #include <iostream> 5 #include <queue> 6 using namespace std; 7 const int imax_n = 205; 8 long long a[imax_n]; 9 queue<long long > q;10 void bfs()11 {12     int cnt=0;13     while (!q.empty()) q.pop();14     q.push(1);15     while (1)16     {17         long long x=q.front();18         q.pop();19         for (int i=1;i<=200;i++)20         if (a[i]==0 && x%i==0)21         {22             a[i]=x;23             cnt++;24         }25         if (cnt>=200) return ;26         q.push(10*x);27         q.push(10*x+1);28     }29 }30 int main()31 {32     bfs();33     int n;34     //for (int i=1;i<=200;i++)35     //{36      //   printf("a[%d]=%lld\n",i,a[i]);37     //}38     while (scanf("%d",&n),n)39     {40         printf("%lld\n",a[n]);41     }42     return 0;43 }
View Code

 

zoj1530 bfs