# 麻烦的DP-BZOJ-1605-[Usaco2008 Open]Crisis on the Farm 牧场危机

Description

Input

Output

Sample Input
3 6 3

3 4

6 2

5 7

8 2

9 2

6 4

5 4

6 7

8 7

Sample Output
Use the ‘east’ whistle three times, at which point the milk floods

the area. Each haystack ends up saving 1 cow.

6

EEE

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <vector>
#include <algorithm>
#include <cmath>
#include <queue>
#include <set>
using namespace std;
int n,m,k;
typedef struct node
{
int x,y;
} node;
node cow[1005];
bool grass[1005][1005];
int  get[105][105];
int dp[105][105][35];
char Turn(int x,int y)
{
if(x==-1)
return 'W';
if(x==1)
return 'E';
if(y==-1)
return 'S';
return 'N';
}
int moved[4][2]= {{1,0},{0,1},{0,-1},{-1,0}};
bool check(int x,int y)
{
if(x<0 || x>1000 || y<0 || y>1000)
return 0;
return 1;
}
char pre[105][105][35];
char nn[4]= {'E','N','S','W'};
int main()
{
int x,y;
cin >> n >> m >> k;
int fix=k+1;
for(int i=0; i<n; i++)
scanf("%d%d",&cow[i].x,&cow[i].y);
for(int i=0; i<m; i++)
{
scanf("%d%d",&x,&y);
grass[x][y]=1;
}
for(int i=1; i<=2*k+1; i++)
for(int j=1; j<=2*k+1; j++)
{
if(abs(i-fix)+abs(j-fix)>k)
continue;
for(int z=0; z<n; z++)
{
x=cow[z].x+i-fix,y=cow[z].y+j-fix;
if(!check(x,y))
continue;
if(grass[x][y])
get[i][j]++;
}
}
int out=0;
for(int t=1; t<=k; t++)
for(int i=1; i<=2*k+1; i++)
for(int j=1; j<=2*k+1; j++)
{
int tmp=abs(i-fix)+abs(j-fix);
if(tmp>t)
continue;
if(tmp==t || (t-tmp)%2==0)
{
dp[i][j][t]=max(max(dp[i-1][j][t-1],dp[i+1][j][t-1]),max(dp[i][j-1][t-1],dp[i][j+1][t-1]))+get[i][j];
if(t==k && dp[i][j][t]>=out)
out=dp[i][j][t];
}
}
for(int i=1; i<=2*k+1; i++)
for(int j=1; j<=2*k+1; j++)
if(dp[i][j][k]==out)
pre[i][j][k]='F';
cout << out << endl;
for(int t=k-1; t>=0; t--)
for(int i=1; i<=2*k+1; i++)
for(int j=1; j<=2*k+1; j++)
{
int tmp=abs(i-fix)+abs(j-fix);
if(tmp>t)
continue;
for(int z=3; z>=0; z--)
if(dp[i][j][t]+get[i+moved[z][0]][j+moved[z][1]]==dp[i+moved[z][0]][j+moved[z][1]][t+1] && pre[i+moved[z][0]][j+moved[z][1]][t+1]!=0)
pre[i][j][t]=nn[z];
}
int nowx=fix,nowy=fix;
for(int i=0; i<k; i++)
{
printf("%c",pre[nowx][nowy][i]);
switch(pre[nowx][nowy][i])
{
case 'E':
nowx++;
break;
case 'N':
nowy++;
break;
case 'S':
nowy--;
break;
case 'W':
nowx--;
break;
}
}
return 0;
}


12-18 94
12-24 50
08-23 2720
12-20
©️2020 CSDN 皮肤主题: 编程工作室 设计师:CSDN官方博客