Title Link: http://acm.hdu.edu.cn/showproblem.php?pid=1876
Test instructions: Ask the robot to reach the end of the process up to a few times completely exhausted the energy, consume so many times the way of energy there are several.
Analysis: Simulation of the next time you go to the end of the consumption must be on a diagonal line.
Take sample as an example:
As the entire process is pushed down the diagonal direction, each must be to the right down, so just press the full cycle recursion can, of course, according to the diagonal right below the loop can also, but that slightly troublesome. Dp[i][j].num indicates the number of times the i,j consumes the most energy at the end of a walk, and Dp[i][j].cnt says that the most energy-consuming way to go to the point (I,J) is to eliminate it.
The state transition equation is:
if (dp[x][y].num<dp[i][j].num+1)
{
dp[x][y].num=dp[i][j].num+1;
dp[x][y].cnt=dp[i][j].cnt;
}
else if (dp[x][y].num==dp[i][j].num+1)
dp[x][y].cnt+=dp[i][j].cnt;
In the case there are several pits, the energy of 0 of the lattice can not go, at the end of the time without recursion, the sample did not just finish at the end of the consumption of energy, so here is a good hole t^^t
#include <cstdio>#include<cstring>#include<cmath>#include<iostream>#include<algorithm>#include<queue>#include<cstdlib>#include<vector>#include<Set>#include<map>#defineLL Long Long#defineMoD 1000000007#defineINF 0x3f3f3f3f#defineN 10010using namespacestd;structnode{intCnt,num;} dp[ the][ the];intflag[ the][ the],a[ the][ the];intMain () {intT,n,m,x,y; scanf ("%d",&t); while(t--) {scanf ("%d%d",&n,&m); for(intI=1; i<=n; i++) for(intj=1; j<=m; J + +) scanf ("%d",&A[i][j]); memset (Flag,0,sizeof(flag)); Memset (DP,0,sizeof(DP)); flag[1][1]=1; dp[1][1].cnt=1; for(intI=1; i<=n; i++) for(intj=1; j<=m; J + +) { if(i==n&&j==m)Continue;//This is especially noteworthy if(Flag[i][j]) {if(!a[i][j])Continue;//You can't go when the energy is 0.x=i+A[i][j]; Y=J; intt=a[i][j]+1; if(x+y<=m+N) { while(t--) { if(x>n| | y>m| | x<=0|| y<=0)//not in the grid .{x--; Y++; Continue; } Flag[x][y]=1; if(dp[x][y].num<dp[i][j].num+1)//take a lot of times{dp[x][y].num=dp[i][j].num+1; Dp[x][y].cnt=dp[i][j].cnt; } Else if(dp[x][y].num==dp[i][j].num+1) dp[x][y].cnt+=dp[i][j].cnt; X--; Y++; } } Else//go to the finish line and not burn the energy out. { if(dp[n][m].num<dp[i][j].num) {Dp[n][m].num=Dp[i][j].num; Dp[n][m].cnt=dp[i][j].cnt; } Else if(dp[n][m].num==dp[i][j].num) dp[n][m].cnt+=dp[i][j].cnt; }}} printf ("%d%d\n", dp[n][m].num,dp[n][m].cnt); }}View Code
hdu1876 (DP)