暴力 hihoCoder 1251 Today Is a Rainy Day (15北京C)
题意:一串数字变成另一串数字,可以单个数字转变,或者一类数字转变,问最少操作次数
分析:首先一类转换比单个转换优,先枚举找出最优的映射方案,然后将零碎的操作加上。6位6进制表示map映射,比如123456,表示map[1] = 1, map[2] = 2, map[3] = 3...
先预处理出每种方案的的操作数,用BFS+dp写,然后暴力枚举取最小值。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 | #include <bits/stdc++.h> using namespace std; const int N = 110 + 5; const int M = 5e4 + 5; const int INF = 0x3f3f3f3f; char ori[N], fin[N]; int dp[M]; int cnt[6]; int g[6][6]; int idx( int *c) { int ret = 0; for ( int i=0; i<6; ++i) { ret = ret * 6 + c[i]; } return ret; } void ridx( int x, int *c) { for ( int i=5; i>=0; --i) { c[i] = x % 6; x /= 6; } } void BFS( void ) { memset (dp, INF, sizeof (dp)); int c[6]; for ( int i=0; i<6; ++i) c[i] = i; //nomal, not change int s = idx (c); dp[s] = 0; queue< int > que; que.push (s); while (!que.empty ()) { int u = que.front (); que.pop (); ridx (u, c); for ( int i=0; i<6; ++i) { for ( int j=0; j<6; ++j) { int t[6]; memcpy (t, c, sizeof (t)); for ( int k=0; k<6; ++k) { if (t[k] == i) t[k] = j; } int v = idx (t); if (dp[v] > dp[u] + 1) { dp[v] = dp[u] + 1; que.push (v); } } } } } int main( void ) { BFS (); while ( scanf ( "%s%s" , ori, fin) == 2) { int len = strlen (ori); memset (cnt, 0, sizeof (cnt)); memset (g, 0, sizeof (g)); for ( int i=0; i<len; ++i) { int d1 = fin[i] - '1' , d2 = ori[i] - '1' ; cnt[d1]++; g[d1][d2]++; } int ans = INF; int c[6]; for ( int i=0; i<M; ++i) { ridx (i, c); int tmp = dp[i]; for ( int j=0; j<6; ++j) { tmp += cnt[j] - g[j][c[j]]; } ans = min (ans, tmp); } printf ( "%d\n" , ans); } return 0; } /* 22345611 12345611 2234562221 1234561221 2234562211 1234561111 22345622112 12345611111 654321654321654321654321 123456123456123456123456 */ |
编译人生,运行世界!
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步