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

15 篇文章 0 订阅
14 篇文章 1 订阅
#include<iostream>
#include<algorithm>
#include<cstring>
#include<string>
#include<sstream>
#include<queue>
using namespace std;
typedef long long LL;
const int M = 10000;
const int INF = 0x3f3f3f3f;

int n;
int dir[][2] = { 1,0,-1,0,0,-1,0,1 };
int mp[55][55];
int s[55][55];
bool vis[55][55];
LL dp[55][55];

struct node
{
	int x, y;
}st, nx;

queue<node >q;

void bfs()      //记录每个点到终点的最短路
{
	for (int i = 1; i <= n; ++i)
	{
		for (int j = 1; j <= n; ++j)
		{
			s[i][j] = INF;
		}
	}
	memset(vis, 0, sizeof(vis));
	vis[n][n] = 1;
	s[n][n] = mp[n][n];    //由于题目要求 需要逆向更新 
	node st;
	st.x = n;
	st.y = n;
	q.push(st);
	node tmp;
	while (!q.empty())  
	{
		node nx = q.front();
		q.pop();
		int x = nx.x;
		int y = nx.y;
		vis[x][y] = 0;
		for (int i = 0; i <4; ++i)
		{
			int xx = x + dir[i][0];
			int yy = y + dir[i][1];
			if (xx<1 || xx>n || yy<1 || yy>n)
				continue;
			if (s[xx][yy]>s[x][y] + mp[xx][yy])
			{
				s[xx][yy] = s[x][y] + mp[xx][yy];
				if (!vis[xx][yy])
				{
					tmp.x = xx;
					tmp.y = yy;
					q.push(tmp);
					vis[xx][yy] = 1;
				}
			}

		}
	}
	return;
}

LL dfs(int x, int y)
{
	if (x == n && y == n)
		return 1;
	LL ans = 0;
	if (!dp[x][y])
	{
		for (int i = 0; i < 4; ++i)
		{
			int xx = x + dir[i][0];
			int yy = y + dir[i][1];
			if (xx >= 1 && xx <= n && yy >= 1 && yy <= n && s[xx][yy] < s[x][y])
			{
				ans += dfs(xx, yy);
			}

		}
		dp[x][y] = ans;
	}

	return dp[x][y];
}


int main()
{
	while (~scanf("%d", &n))
	{
		for (int i = 1; i <= n; ++i)
		{
			for (int j = 1; j <= n; ++j)
			{
				scanf("%d", &mp[i][j]);
			}
		}
		//memset(s, 0, sizeof(s));
		bfs();
		memset(dp, 0, sizeof(dp));
		cout << dfs(1, 1) << endl;
	}

	return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值