标签:
题目链接:
http://acm.hdu.edu.cn/showproblem.php?pid=1104
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 3566 Accepted Submission(s): 828
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<queue> 5 #include<string> 6 using namespace std; 7 8 const int maxn = 1e3 + 10; 9 10 struct Node { 11 int val, step; 12 string s; 13 Node(int v, int st, string s) :val(v), step(st),s(s){} 14 Node() {} 15 }; 16 17 int n, k, m,km; 18 19 int vis[maxn*maxn]; 20 21 void bfs(int ans) { 22 queue<Node> q; 23 Node node=Node(((n%km) + km) % km, 0, ""); 24 vis[node.val] = 1; 25 q.push(node); 26 while (!q.empty()) { 27 node = q.front(); q.pop(); 28 if (node.val%k == ans) { 29 cout << node.step << endl; 30 cout << node.s << endl; 31 return; 32 } 33 int tmp = (node.val + m) % km; 34 if (!vis[tmp]) { 35 vis[tmp] = 1; 36 q.push(Node(tmp, node.step + 1, node.s + "+")); 37 } 38 tmp = ((node.val - m) % km + km) % km; 39 if (!vis[tmp]) { 40 vis[tmp] = 1; 41 q.push(Node(tmp, node.step + 1, node.s + "-")); 42 } 43 tmp = (node.val*m) % km; 44 if (!vis[tmp]) { 45 vis[tmp] = 1; 46 q.push(Node(tmp, node.step + 1, node.s + "*")); 47 } 48 tmp = (node.val%m) % km; 49 if (!vis[tmp]) { 50 vis[tmp] = 1; 51 q.push(Node(tmp, node.step + 1, node.s + "%")); 52 } 53 } 54 cout << "0" << endl; 55 } 56 57 void init() { 58 memset(vis, 0, sizeof(vis)); 59 } 60 61 int main() { 62 while (scanf("%d%d%d", &n, &k, &m) == 3) { 63 if (n == 0 && k == 0 && m == 0) break; 64 km = k*m; 65 init(); 66 bfs(((n+1)%k+k)%k); 67 } 68 return 0; 69 }
标签:
原文地址:http://www.cnblogs.com/fenice/p/5247048.html