NC24026. [USACO 2016 Jan G]Radio Contact
描述
输入描述
The first line of input contains N and M (1≤N,M≤1000). The second line contains integers fx and fy, and the third line contains bx and by (0≤fx,fy,bx,by≤1000). The next line contains a string of length N describing FJ's path, and the final line contains a string of length M describing Bessie's path.
It is guranteed that Farmer John and Bessie's coordinates are always in the range (0≤x,y≤1000) throughout their journey. Note that East points in the positive x direction and North points in the positive y direction.
输出描述
Output a single integer specifying the minimum energy FJ and Bessie can use during their travels.
示例1
输入:
2 7 3 0 5 0 NN NWWWWWN
输出:
28
C++(clang++11) 解法, 执行用时: 6ms, 内存消耗: 4472K, 提交时间: 2020-12-26 22:04:21
#include<bits/stdc++.h> using namespace std; const int MAX=1<<10; int dp[MAX][MAX],n,m,x,y,x2,y2; int a[MAX][2],b[MAX][2]; void Scan(int num,int x,int y,int arr[][2]){ char ch; arr[0][0]=x; arr[0][1]=y; for(int i=1;i<=num;++i){ ch=getchar(); switch(ch){ case 'N': y++; break; case 'S': y--; break; case 'W': x--; break; default: x++; } arr[i][0]=x; arr[i][1]=y; } getchar(); } int dis(int* a,int* b){ return ((a[0]-b[0])*(a[0]-b[0])+(a[1]-b[1])*(a[1]-b[1])); } void solve(){ memset(dp,0x3f,sizeof(dp)); dp[0][0]=0; for(int i=1;i<=n;i++) dp[i][0]=dp[i-1][0]+dis(a[i],b[0]); for(int j=1;j<=m;j++) dp[0][j]=dp[0][j-1]+dis(a[0],b[j]); for(int i=1;i<=n;i++){ for(int j=1;j<=m;j++){ dp[i][j]=min({dp[i-1][j-1],dp[i][j-1],dp[i-1][j]})+dis(a[i],b[j]); } } printf("%d\n",dp[n][m]); } int main(){ scanf("%d%d%d%d%d%d",&n,&m,&x,&y,&x2,&y2); getchar(); Scan(n,x,y,a); Scan(m,x2,y2,b); solve(); return 0; }