ZOJ 1107FatMouse and Cheese(BFS)

题目链接

分析: 一个n * n的图,每个点是一个奶酪的体积,从0,0开始每次最多可以走k步,下一步体积必须大于上一步,求最大体积和

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <queue>
 4 #include <cstring>
 5 #include <algorithm>
 6 using namespace std;
 7 const int Max = 550 + 10;
 8 const int INF = 0x3f3f3f3f;
 9 int g[Max][Max], dp[Max][Max];
10 int n, k;
11 int gx[4] = {0, 0, 1, -1};
12 int gy[4] = {1, -1, 0, 0};
13 struct Node
14 {
15     int x, y;
16
17 };
18 void bfs(int x, int y)
19 {
20     Node node;
21     node.x = x;
22     node.y = y;
23     dp[x][y] = g[x][y];
24     queue<Node> que;
25     que.push(node);
26     int maxn = -INF;
27     while (!que.empty())
28     {
29         node = que.front();
30         que.pop();
31         if (dp[node.x][node.y] > maxn)
32             maxn = dp[node.x][node.y];
33         for (int i = 0; i < 4; i++)
34         {
35             for (int j = 1; j <= k; j++)
36             {
37                 int fx = node.x + gx[i] * j;
38                 int fy = node.y + gy[i] * j;
39                 if (fx >= 1 && fy >= 1 && fx <= n && fy <= n && g[fx][fy] > g[node.x][node.y])
40                 {
41                     if (dp[fx][fy] < g[fx][fy] + dp[node.x][node.y])
42                     {
43                         dp[fx][fy] = g[fx][fy] +  dp[node.x][node.y];
44                         Node temp;
45                         temp.x = fx;
46                         temp.y = fy;
47                         que.push(temp);
48                     }
49                 }
50             }
51         }
52     }
53     printf("%d\n", maxn);
54 }
55 int main()
56 {
57     while (scanf("%d%d", &n, &k) != EOF)
58     {
59         if (n == -1 && k == -1)
60             break;
61         for (int i = 1; i <= n; i++)
62             for (int j = 1; j <= n; j++)
63                 scanf("%d", &g[i][j]);
64         memset(dp, 0, sizeof(dp));
65         bfs(1, 1);
66     }
67     return 0;
68 }

时间: 2024-10-26 02:43:50

ZOJ 1107FatMouse and Cheese(BFS)的相关文章

ZOJ 3890 Wumpus (BFS)

题目链接:ZOJ 3890 Wumpus 题意:一个人在n*n的地图的左下角,他想逃出地图,并且他想要分数达到最高,他可以做的操作有直走,左转,右转,射击,爬,挖金矿6个操作,每个操作后分数减10,但是挖到金矿分数加1000,问你逃出地图的最大分数是多少.他可能遇到怪兽但是他不射击(也就是说没有射击的操作),金库在地图中也只有一个. 思路:BFS搜一遍就好了 AC代码: #include <stdio.h> #include <string.h> #include <queu

zoj 2913 Bus Pass (BFS)

Bus Pass Time Limit: 5 Seconds      Memory Limit: 32768 KB You travel a lot by bus and the costs of all the seperate tickets are starting to add up. Therefore you want to see if it might be advantageous for you to buy a bus pass. The way the bus syst

HDU 1026 Ignatius and the Princess I (基本算法-BFS)

Ignatius and the Princess I Problem Description The Princess has been abducted by the BEelzebub feng5166, our hero Ignatius has to rescue our pretty Princess. Now he gets into feng5166's castle. The castle is a large labyrinth. To make the problem si

pots(BFS)

D - Pots Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu Submit Status Description You are given two pots, having the volume of A and B liters respectively. The following operations can be performed: Input On the first and

USACO抓牛catchcow (bfs)

这题是黄巨大出的比赛题. http://poj.org/problem?id=3278 Description Farmer John has been informed of the location of a fugitive cow and wants to catch her immediately. He starts at a point N (0 ≤ N ≤ 100,000) on a number line and the cow is at a point K (0 ≤ K ≤

[ACM] hdu 1253 胜利大逃亡 (三维BFS)

胜利大逃亡 Problem Description Ignatius被魔王抓走了,有一天魔王出差去了,这可是Ignatius逃亡的好机会. 魔王住在一个城堡里,城堡是一个A*B*C的立方体,可以被表示成A个B*C的矩阵,刚开始Ignatius被关在(0,0,0)的位置,离开城堡的门在(A-1,B-1,C-1)的位置,现在知道魔王将在T分钟后回到城堡,Ignatius每分钟能从一个坐标走到相邻的六个坐标中的其中一个.现在给你城堡的地图,请你计算出Ignatius能否在魔王回来前离开城堡(只要走到出

UVA 657-The die is cast(双重BFS)

InterGames is a high-tech startup company that specializes in developing technology that allows users to play games over the Internet. A market analysis has alerted them to the fact that games of chance are pretty popular among their potential custom

HDU 1242 Rescue(优先队列+bfs)

题目地址:HDU 1242 这个题相比于普通的bfs有个特殊的地方,经过士兵时会额外消耗时间,也就是说此时最先搜到的时候不一定是用时最短的了.需要全部搜一遍才可以.这时候优先队列的好处就显现出来了.利用优先队列,可以让队列中的元素按时间排序,让先出来的总是时间短的,这样的话,最先搜到的一定是时间短的,就不用全部搜一遍了.PS:我是为了学优先队列做的这题..不是为了这题而现学的优先队列.. 代码如下: #include <iostream> #include <stdio.h> #i

POJ 3083:Children of the Candy Corn(DFS+BFS)

Children of the Candy Corn Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9311 Accepted: 4039 Description The cornfield maze is a popular Halloween treat. Visitors are shown the entrance and must wander through the maze facing zombies, ch