标签:
转:http://blog.csdn.net/wangjian8006/article/details/7460523
(比较好的记录路径方案)
1 #include<iostream> 2 #include<cstring> 3 #include<cstdlib> 4 #include<cstdio> 5 #include<algorithm> 6 #include<cmath> 7 #include<queue> 8 #include<map> 9 #include<set> 10 #include<stack> 11 #include<string> 12 13 #define N 1000005 14 #define M 9 15 #define mod 1000000007 16 //#define p 10000007 17 #define mod2 1000000000 18 #define ll long long 19 #define LL long long 20 #define eps 1e-6 21 #define inf 1000000 22 #define maxi(a,b) (a)>(b)? (a) : (b) 23 #define mini(a,b) (a)<(b)? (a) : (b) 24 25 using namespace std; 26 27 int n; 28 typedef struct 29 { 30 int v; 31 int yu; 32 int num; 33 int pre; 34 }PP; 35 36 int tot; 37 int vis[1005]; 38 PP ans[N]; 39 40 void ini() 41 { 42 tot=0; 43 memset(vis,0,sizeof(vis)); 44 } 45 46 void out(int now) 47 { 48 // for(int i=0;i<=1;i++) 49 //printf(" v=%d pre=%d\n",ans[i].v,ans[i].pre); 50 //printf(" now=%d v=%d pre=%d\n",now,ans[now].v,ans[now].pre); 51 if(ans[now].pre!=-1){ 52 53 out(ans[now].pre); 54 55 } 56 printf("%d",ans[now].v); 57 } 58 59 void bfs() 60 { 61 queue<PP>q; 62 PP te,nt; 63 te.v=1; 64 te.pre=-1; 65 te.num=0; 66 te.yu=1%n; 67 ans[0].v=1; 68 ans[0].pre=-1; 69 tot=1; 70 vis[1]=1; 71 q.push(te); 72 73 while(q.size()>=1) 74 { 75 76 te=q.front(); 77 q.pop(); 78 // printf(" v=%d yu=%d num=%d pre=%d\n",te.v,te.yu,te.num,ans[te.num].pre); 79 if(te.yu==0){ 80 //printf(" num=%d\n",te.num); 81 out(te.num); 82 printf("\n"); 83 return; 84 } 85 nt.pre=te.num; 86 nt.yu=(te.yu*10)%n; 87 if(vis[nt.yu]==0){ 88 vis[nt.yu]=1; 89 nt.num=tot; 90 ans[tot].v=0; 91 ans[tot].pre=te.num; 92 tot++; 93 94 nt.v=0; 95 q.push(nt); 96 } 97 nt.yu=(te.yu*10+1)%n; 98 if(vis[nt.yu]==0){ 99 vis[nt.yu]=1; 100 nt.num=tot; 101 ans[tot].v=1; 102 ans[tot].pre=te.num; 103 tot++; 104 nt.v=1; 105 q.push(nt); 106 } 107 } 108 } 109 110 void solve() 111 { 112 bfs(); 113 } 114 115 int main() 116 { 117 //freopen("data.in","r",stdin); 118 // freopen("data.out","w",stdout); 119 //scanf("%d",&T); 120 //for(int ccnt=1;ccnt<=T;ccnt++) 121 //while(T--) 122 while(scanf("%d",&n)!=EOF) 123 { 124 if(n==0) break; 125 ini(); 126 solve(); 127 // out(); 128 } 129 130 return 0; 131 }
poj1426 - Find The Multiple [bfs 记录路径]
标签:
原文地址:http://www.cnblogs.com/njczy2010/p/4242518.html