码迷,mamicode.com
首页 > 其他好文 > 详细

zoj1530 bfs

时间:2014-09-04 18:18:49      阅读:251      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   os   io   ar   for   art   

bubuko.com,布布扣
 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
bubuko.com,布布扣
 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

标签:style   blog   http   color   os   io   ar   for   art   

原文地址:http://www.cnblogs.com/djingjing/p/3956438.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!