洛谷 P2905 [USACO08OPEN]農場危機Crisis on the Farm
題目描述
約翰和他的奶牛組建了一只樂隊“後街奶牛”,現在他們正在牧場裏排練.奶牛們分成一堆 一堆,共1000)堆.每一堆裏,30只奶牛一只踩在另一只的背上,疊成一座牛塔.牧場 裏還有M(1 < M < 1000)個高高的草垛.
作為出色的指揮家,約翰可以通過口哨指揮奶牛們移動.他的口哨有四個音,分別能使所有 的牛塔向東南西北四個方向移動一格.
每一次,當一個牛塔到達了一個草垛所在的格子,牛塔最上方的奶牛就會跳到草垛上,而且 不再下來,而其他奶牛仍然呈塔狀站在草垛所在的格子裏.當牛塔只剩一只奶牛時,這只奶牛也 會跳到草垛上.
突然,約翰大驚失色:原來鄰家的奶缸爆炸了!滾滾而下的牛奶正朝著約翰的牧場沖來,不久就要將牧場淹沒.約翰必須馬上行動,用口哨聲挽救奶牛們的生命.他要指揮奶牛盡量多地跳 上草操,草操上的奶牛將不會被淹死.
約翰還有K次吹口哨的機會.那他最多還能救多少奶牛呢?請計算最多能挽救的奶牛數,以及 達到這個數目約翰需要吹的口哨調子序列.序列用E,W,S,N表示東西南北.如果有多種序列能達到 要求,輸出作為字符串最小的.
輸入輸出格式
輸入格式:
-
Line 1: Three space-separated integers: N, M, and K
-
Lines 2..N+1: Line i+1 describes the X,Y location of a stack of 30 cows using two space-separated integers: X_i and Y_i
- Lines N+2..N+M+1: Line i+N+1 describes the X,Y location of a haystack using two space-separated integers: X_i and Y_i
輸出格式:
-
Line 1: A single integer that is the most number of cows that can be saved.
- Line 2: K characters, the lexicographically least sequence of commands FJ should issue to maximize the number of cows saved.
輸入輸出樣例
輸入樣例#1:3 6 3 3 4 6 2 5 7 8 2 9 2 6 4 5 4 6 7 8 7輸出樣例#1:
6 EEE
說明
Use the ‘east‘ whistle three times, at which point the milk floods the area. Each haystack ends up saving 1 cow.
dp 惡心。。
一個神奇的問題:
考試時我讀不完數據 (#‵′)靠
前面的很正常
m個草都是0的讀不完。。
讀入優化超時
cin scanf讀不進去 直接炸內存。。
屠龍寶刀點擊就送
#include <cstring> #include <cstdio> #define N 35 #define M 2005 #define INF 0x3f3f3f3f int n,m,k,ans=0,x[M],y[M],li=31,lj=31,f[N<<2][N<<2][N],newmap[N<<2][N<<2],Map[M][M],fx[5]={-1,0,0,1},fy[5]={0,-1,1,0}; char pre[N<<2][N<<2][N],pick[N]; inline int max(int a,int b) {return a>b?a:b;} void init_map() { for(int i=0;i<=62;i++) { for(int j=0;j<=62;j++) { for(int l=1;l<=n;l++) { int X=x[l],Y=y[l]; if(X+i-31<=0||Y+j-31<=0||X+i-31>1000||Y+j-31>1000) continue; newmap[i][j]+=Map[X+i-31][Y+j-31]; } } } } int main() { // freopen("dance.in","r",stdin);freopen("dance.out","w",stdout); pick[0]=‘W‘,pick[1]=‘S‘,pick[2]=‘N‘,pick[3]=‘E‘; scanf("%d%d%d",&n,&m,&k); for(int i=1;i<=n;i++) scanf("%d%d",&x[i],&y[i]); for(int x,y,i=1;i<=m;i++) { scanf("%d%d",&x,&y); Map[x][y]++; } init_map(); for(int i=0;i<=k;i++) for(int j=0;j<=62;j++) for(int l=0;l<=62;l++) f[j][l][i]=-INF,pre[j][l][i]=‘Z‘; f[31][31][0]=0; for(int i=1;i<=k;i++) { for(int j=1;j<=61;j++) { for(int l=1;l<=61;l++) { f[j][l][i]=max(max(f[j-1][l][i-1],f[j][l-1][i-1]),max(f[j][l+1][i-1],f[j+1][l][i-1]))+newmap[j][l]; if(i==k) ans=max(ans,f[j][l][i]); } } } for(int i=1;i<=61;i++) for(int j=1;j<=61;j++) if(f[i][j][k]==ans) pre[i][j][k]=‘A‘; for(int l=k-1;l>=0;l--) { for(int i=1;i<=61;i++) { for(int j=1;j<=61;j++) { for(int o=0;o<4;o++) { if(f[i][j][l]+newmap[i+fx[o]][j+fy[o]]==f[i+fx[o]][j+fy[o]][l+1]&&pre[i+fx[o]][j+fy[o]][l+1]<‘Z‘) pre[i][j][l]=pick[o]; } } } } printf("%d\n",ans); for(int i=0;i<k;i++) { printf("%c",pre[li][lj][i]); if(pre[li][lj][i]==‘E‘) li++; else if(pre[li][lj][i]==‘N‘) lj++; else if(pre[li][lj][i]==‘S‘) lj--; else if(pre[li][lj][i]==‘W‘) li--; } return 0; }
洛谷 P2905 [USACO08OPEN]農場危機Crisis on the Farm