#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;
}
HDU 1428 漫步校园(Spfa+记忆化搜索)
最新推荐文章于 2020-07-07 19:40:15 发布