标签:+= \n std sizeof using font hide des 状态
http://acm.hdu.edu.cn/showproblem.php?pid=3567
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 130000/65536 K (Java/Others)
Total Submission(s): 4541 Accepted Submission(s): 990
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<queue> 5 #include<stack> 6 #include<string> 7 using namespace std; 8 string str1, str2; 9 bool vis[370000]; 10 int dx[4] = { 1,0,0,-1 }; 11 int dy[4] = { 0,-1,1,0 }; 12 char cs[4] = { ‘d‘,‘l‘,‘r‘,‘u‘ }; 13 int pre[10][370000]; 14 int op[10][370000]; 15 int jc[12]; 16 int kt(int s) //康托展开 17 { 18 int code = 0; 19 int st[9]; 20 for (int i = 8; i >= 0; i--, s /= 10) 21 st[i] = s % 10; 22 for (int i = 0; i<9; i++) 23 { 24 int cnt = 0; 25 for (int j = i + 1; j<9; j++) 26 if (st[j]<st[i]) cnt++; 27 code += jc[8 - i] * cnt; 28 } 29 return code; 30 } 31 int skt = 0; 32 int mypow(int x, int y) { 33 int ans = 1; 34 while (y) { 35 if (y & 1)ans *= x; 36 x *= x; 37 y /= 2; 38 } 39 return ans; 40 } 41 void bfs(string str,int x) { 42 memset(vis, 0, sizeof(vis)); 43 queue<int>pq; 44 queue<int>pq2; 45 queue<int>pq3; 46 while (!pq.empty()) { 47 pq.pop(); 48 } 49 while (!pq3.empty()) { 50 pq3.pop(); 51 } 52 while (!pq2.empty()) { 53 pq2.pop(); 54 } 55 int tmps = 0; 56 for (int i = 0; i < 9; i++) { 57 tmps = tmps * 10 + str[i] - ‘0‘; 58 } 59 pq.push(tmps); 60 pq2.push(x); 61 int kt000 = kt(tmps); 62 pq3.push(kt000); 63 vis[kt000] = 1; 64 while (!pq.empty()) { 65 int str0 = pq.front(); pq.pop(); 66 //cout << str0 << endl; 67 int s0 = pq2.front(); pq2.pop(); 68 int kt010 = pq3.front(); pq3.pop(); 69 for (int i = 0; i < 4; i++) { 70 int x0 = s0 / 3; 71 int y0 = s0 % 3; 72 int tx = x0 + dx[i]; 73 int ty = y0 + dy[i]; 74 int s00 = tx * 3 + ty; 75 if (tx >= 0 && ty >= 0 && ty < 3 && tx < 3) { 76 int str00=str0; 77 int skt1 = ((str0) / (mypow(10, (8 - s0)))) % 10; 78 str00 -= skt1*mypow(10,(8-s0)); 79 int skt2= ((str0) / (mypow(10, (8 - s00)))) % 10; 80 str00 += skt2 * mypow(10,(8-s0)); 81 str00 -= skt2 * mypow(10, (8 - s00)); 82 str00 += skt1 * mypow(10, (8 - s0)); 83 //str00[s00] = str0[s0]; 84 int kt0 = kt(str00); 85 //skt++; 86 // cout << skt << endl; 87 // cout << kt0 << endl; 88 if (!vis[kt0]) { 89 vis[kt0] = 1; 90 op[x][kt0] = i; 91 pre[x][kt0] = kt010; 92 pq.push(str00); 93 pq2.push(s00); 94 pq3.push(kt0); 95 } 96 } 97 } 98 } 99 100 } 101 int main() { 102 int t; 103 jc[0] = 1; 104 for (int i = 1; i < 9; i++) { 105 jc[i] = jc[i - 1] * i; 106 } 107 int case1 = 1; 108 string str[9]; 109 str[0] = "012345678"; 110 bfs(str[0], 0); 111 // cout << "%%%%%\n"; 112 str[1] = "102345678"; 113 bfs(str[1], 1); 114 str[2] = "120345678"; 115 bfs(str[2], 2); 116 str[3] = "123045678"; 117 bfs(str[3], 3); 118 str[4] = "123405678"; 119 bfs(str[4], 4); 120 str[5] = "123450678"; 121 bfs(str[5], 5); 122 str[6] = "123456078"; 123 bfs(str[6], 6); 124 str[7] = "123456708"; 125 bfs(str[7], 7); 126 str[8] = "123456780"; 127 bfs(str[8], 8); 128 scanf("%d", &t); 129 while (t--) { 130 cin >> str1 >> str2; 131 int u; 132 for (int i = 0; i < 9; i++) { 133 if (str1[i] == ‘X‘) { 134 str1[i] = ‘0‘; 135 u = i; 136 } 137 if (str2[i] == ‘X‘) { 138 str2[i] = ‘0‘; 139 } 140 } 141 char hash0[10]; 142 for (int i = 0; i < 9; i++) { 143 hash0[str1[i] - ‘0‘] = str[u][i]; 144 } 145 string tmp = ""; 146 for (int i = 0; i < 9; i++) { 147 tmp += hash0[str2[i] - ‘0‘]; 148 } 149 int s1=0, s2=0; 150 for (int i = 0; i < 9; i++) { 151 s1 = s1 * 10 + str[u][i] - ‘0‘; 152 } 153 for (int i = 0; i < 9; i++) { 154 s2 = s2 * 10 + tmp[i] - ‘0‘; 155 } 156 int sta = kt(s1); 157 int en = kt(s2); 158 stack<int>stk; 159 while (!stk.empty())stk.pop(); 160 while (sta != en) { 161 stk.push(en); 162 en = pre[u][en]; 163 } 164 printf("Case %d: %d\n", case1++, stk.size()); 165 while (!stk.empty()) { 166 int sss = stk.top(); 167 stk.pop(); 168 if (sss != sta) { 169 printf("%c",cs[op[u][sss]]); 170 } 171 } 172 printf("\n"); 173 } 174 return 0; 175 }
【HDOJ3567】【预处理bfs+映射+康拓展开hash】
标签:+= \n std sizeof using font hide des 状态
原文地址:https://www.cnblogs.com/MekakuCityActor/p/9735741.html