Topic links
Analysis: A graph of n * N, each point is a cheese volume, starting from 0, 0 each time up to the K-step, the next volume must be greater than the previous step, to find the maximum volume and
1#include <iostream>2#include <cstdio>3#include <queue>4#include <cstring>5#include <algorithm>6 using namespacestd;7 Const intMax =550+Ten;8 Const intINF =0x3f3f3f3f;9 intG[max][max], Dp[max][max];Ten intN, K; One intgx[4] = {0,0,1, -1}; A intgy[4] = {1, -1,0,0}; - structNode - { the intx, y; - - }; - voidBFsintXinty) + { - node node; +node.x =x; ANode.y =y; atDp[x][y] =G[x][y]; -Queue<node>que; - Que.push (node); - intMAXN =-INF; - while(!que.empty ()) - { innode =Que.front (); - Que.pop (); to if(Dp[node.x][node.y] >MAXN) +MAXN =Dp[node.x][node.y]; - for(inti =0; I <4; i++) the { * for(intj =1; J <= K; J + +) $ {Panax Notoginseng intFX = node.x + gx[i] *J; - intFY = node.y + gy[i] *J; the if(FX >=1&& FY >=1&& FX <= n && fy <= n && g[fx][fy] >G[node.x][node.y]) + { A if(Dp[fx][fy] < G[fx][fy] +Dp[node.x][node.y]) the { +DP[FX][FY] = G[fx][fy] +Dp[node.x][node.y]; - Node temp; $temp.x =FX; $TEMP.Y =fy; - Que.push (temp); - } the } - }Wuyi } the } -printf"%d\n", MAXN); Wu } - intMain () About { $ while(SCANF ("%d%d", &n, &k)! =EOF) - { - if(n = =-1&& k = =-1) - Break; A for(inti =1; I <= N; i++) + for(intj =1; J <= N; J + +) thescanf"%d", &g[i][j]); -Memset (DP,0,sizeof(DP)); $BFs1,1); the } the return 0; the}
View Code
ZOJ 1107FatMouse and Cheese (BFS)