题目传送门
解题思路:
f[i][j]表示FJ走了i步,Bessie走了j步的最小消耗值.方程比较好推.
横纵坐标要搞清楚,因为这东西WA了半小时.
AC代码:
1 #include<iostream> 2 #include<cstdio> 3 #include<cmath> 4 5 using namespace std; 6 7 long long n,m,x,y,xx,yy,f[1002][1002]; 8 long long x1[2003],y11[2003],x2[2003],y2[2003]; 9 string l,l1; 10 11 inline long long min(long long a,long long b) { 12 if(a > b) return b; 13 return a; 14 } 15 16 inline long long len(int id,int ii) { 17 long long u = 0; 18 long long lenx = abs(x1[id] - x2[ii]); 19 long long leny = abs(y11[id] - y2[ii]); 20 u = lenx * lenx + leny * leny; 21 return u; 22 } 23 24 int main() { 25 scanf("%lld%lld%lld%lld%lld%lld",&n,&m,&y,&x,&yy,&xx); 26 cin >> l >> l1; 27 x1[0] = x;y11[0] = y;x2[0] = xx;y2[0] = yy; 28 for(int i = 1;i <= n; i++) { 29 if(l[i-1] == 'W') y--; 30 if(l[i-1] == 'E') y++; 31 if(l[i-1] == 'N') x++; 32 if(l[i-1] == 'S') x--; 33 x1[i] = x;y11[i] = y; 34 } 35 for(int i = 1;i <= m; i++) { 36 if(l1[i-1] == 'W') yy--; 37 if(l1[i-1] == 'E') yy++; 38 if(l1[i-1] == 'N') xx++; 39 if(l1[i-1] == 'S') xx--; 40 x2[i] = xx;y2[i] = yy; 41 } 42 for(int i = 1;i <= n; i++) f[i][0] = f[i-1][0] + len(i,0); 43 for(int j = 1;j <= m; j++) f[0][j] = f[0][j-1] + len(0,j); 44 for(int i = 1;i <= n; i++) 45 for(int j = 1;j <= m; j++) 46 f[i][j] = min(f[i-1][j],min(f[i-1][j-1],f[i][j-1])) + len(i,j); 47 printf("%lld",f[n][m]); 48 return 0; 49 }