HDU 1428 漫步校园(SPFA+记忆化搜索)

126 篇文章 2 订阅

先用spfa求出每个点到达终点的最小距离,然后再记忆化搜索终点到原点最多的路径。

漫步校园

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 2615    Accepted Submission(s): 772


Problem Description
LL最近沉迷于AC不能自拔,每天寝室、机房两点一线。由于长时间坐在电脑边,缺乏运动。他决定充分利用每次从寝室到机房的时间,在校园里散散步。整个HDU校园呈方形布局,可划分为n*n个小方格,代表各个区域。例如LL居住的18号宿舍位于校园的西北角,即方格(1,1)代表的地方,而机房所在的第三实验楼处于东南端的(n,n)。因有多条路线可以选择,LL希望每次的散步路线都不一样。另外,他考虑从A区域到B区域仅当存在一条从B到机房的路线比任何一条从A到机房的路线更近(否则可能永远都到不了机房了…)。现在他想知道的是,所有满足要求的路线一共有多少条。你能告诉他吗?
 

Input
每组测试数据的第一行为n(2=<n<=50),接下来的n行每行有n个数,代表经过每个区域所花的时间t(0<t<=50)(由于寝室与机房均在三楼,故起点与终点也得费时)。
 

Output
针对每组测试数据,输出总的路线数(小于2^63)。
 

Sample Input
  
  
3 1 2 3 1 2 3 1 2 3 3 1 1 1 1 1 1 1 1 1
 

Sample Output
  
  
1 6
#include <algorithm>
#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <iomanip>
#include <stdio.h>
#include <string>
#include <queue>
#include <cmath>
#include <stack>
#include <map>
#include <set>
#define eps 1e-7
#define M 10001000
//#define LL __int64
#define LL long long
#define INF 0x3f3f3f3f
#define PI 3.1415926535898
const int maxn = 101000;

using namespace std;

struct node
{
    int x, y;
};
queue<node> p;
int dic[4][2] = {{-1,0}, {1,0}, {0,1},{0, -1}};
LL num[100][100];
LL  dp[100][100];
LL Map[100][100];
int n;

void spfa()
{
    node a, b;
    a.x = n;
    a.y = n;
    num[n][n] = Map[n][n];
    p.push(a);
    while(!p.empty())
    {
        b = p.front();
        p.pop();
        for(int i = 0; i < 4; i++)
        {
            int tx = b.x+dic[i][0];
            int ty = b.y+dic[i][1];
            if(1 <= tx && tx <= n && 1 <= ty && ty <= n)
            {
                int x = num[b.x][b.y]+Map[tx][ty];
                if(num[tx][ty] == -1 || x < num[tx][ty])
                {
                    a.x = tx;
                    a.y = ty;
                    num[tx][ty] = x;
                    p.push(a);
                }
            }
        }
    }
}

LL dfs(int x, int y)
{
    if(dp[x][y] > 0)
        return dp[x][y];
    if(x == n && y == n)
        return 1;
    for(int i = 0; i < 4; i++)
    {
        int tx = x+dic[i][0];
        int ty = y+dic[i][1];
        if(1 <= tx && tx <= n && 1 <= ty && ty <= n)
        {
            if(num[x][y] > num[tx][ty])
            {
                dp[x][y] += dfs(tx, ty);
            }
        }
    }
    return dp[x][y];
}

int main()
{
    while(cin >>n)
    {
        for(int i = 1; i <= n; i++)
            for(int j = 1; j <= n; j++)
                cin >>Map[i][j];
        memset(num, -1, sizeof(num));
        memset(dp, 0, sizeof(dp));
        while(!p.empty())
            p.pop();
        spfa();
        dfs(1, 1);
        cout<<dp[1][1]<<endl;
    }
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值