【BZOJ4510】[Usaco2016 Jan]Radio Contact【DP】
来源:互联网 发布:样本制作软件 编辑:程序博客网 时间:2024/06/07 01:55
【题目链接】
看了半天题才看懂...
题解:
设dp[i][j]表示FJ走到第i步,Bessie走到第j步时,的最小花费。
那么从dp[i][j]向dp[i + 1][j],dp[i][j + 1],dp[i + 1][j + 1]转移就完了。
复杂度:
时间复杂度O(nm),空间复杂度O(nm)
收获:
要留意到无后效性
/* Forgive me Not */#include <cstdio>#include <cstring>#include <algorithm>using namespace std;typedef long long LL;const int maxn = 1005;int n, m;int dx[] = {0, 0, -1, 1}, dy[] = {1, -1, 0, 0};char str[maxn];LL dp[maxn][maxn];struct _po {int x, y;} F[maxn], B[maxn];inline int getid(char ch) {if(ch == 'N') return 0;if(ch == 'S') return 1;if(ch == 'W') return 2;if(ch == 'E') return 3;}inline LL dis(_po A, _po B) {return (LL)(A.x - B.x) * (A.x - B.x) + (LL)(A.y - B.y) * (A.y - B.y);}int main() {scanf("%d%d%d%d%d%d", &n, &m, &F[0].x, &F[0].y, &B[0].x, &B[0].y);scanf("%s", str + 1); for(int i = 1, op; str[i]; i++) op = getid(str[i]), F[i] = (_po){F[i - 1].x + dx[op], F[i - 1].y + dy[op]};scanf("%s", str + 1); for(int i = 1, op; str[i]; i++) op = getid(str[i]), B[i] = (_po){B[i - 1].x + dx[op], B[i - 1].y + dy[op]};memset(dp, 0x3f, sizeof(dp));dp[0][0] = 0;for(int i = 0; i < n; i++) for(int j = 0; j < m; j++) {dp[i + 1][j] = min(dp[i + 1][j], dp[i][j] + dis(F[i + 1], B[j]));dp[i][j + 1] = min(dp[i][j + 1], dp[i][j] + dis(F[i], B[j + 1]));dp[i + 1][j + 1] = min(dp[i + 1][j + 1], dp[i][j] + dis(F[i + 1], B[j + 1]));}printf("%lld\n", dp[n][m]);return 0;}
0 0
- 【BZOJ4510】[Usaco2016 Jan]Radio Contact【DP】
- BZOJ4510——[Usaco2016 Jan]Radio Contact
- BZOJ 4510: [Usaco2016 Jan]Radio Contact dp
- bzoj4509【Usaco2016 Jan】Angry Cows
- bzoj4512: [Usaco2016 Jan] Build Gates
- BZOJ4525——[Usaco2016 Jan]Angry Cows
- USACO2016 Jan Bronze 2:Angry Cows
- USACO2016 Jan Bronze 3:Mowing the Field
- USACO2016 Jan Bronze 1:Promotion Counting
- USACO2016 Jan Bronze 4:Promotion Counting
- BZOJ 4525: [Usaco2016 Jan]Angry Cows 二分
- [bzoj4511][Usaco2016 Jan]Subsequences Summing to Sevens
- BZOJ 4510 Radio Contact
- bzoj4742 [Usaco2016 Dec] -- DP
- BZOJ4580: [Usaco2016 Open]248 dp
- 【BZOJ】4511: [Usaco2016 Jan]Subsequences Summing to Sevens
- BZOJ 4511: [Usaco2016 Jan]Subsequences Summing to Sevens
- bzoj 4511: [Usaco2016 Jan]Subsequences Summing to Sevens
- CTF【每日一题20160609】
- 设计模式系列:单一职责原则
- Sublime Text编辑器如何显示顶部的菜单栏
- 51NOD-1405 树的距离之和(树形DP)
- 设计模式系列:开闭原则
- 【BZOJ4510】[Usaco2016 Jan]Radio Contact【DP】
- NOI10超级玛丽游戏
- 设计模式系列:里氏替换原则
- 对堆栈的认识
- 设计模式系列:依赖倒置原则
- 设计模式系列:接口隔离原则
- iOS CGRectInset
- 【lightoj】-1294 Positive Negative Sign
- WebGL中纹理贴图过滤器(filter)参数设置详解