特别想吐槽,这个中文题目,我真的读不懂!
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
出题人是魔鬼吗??
Problem Description中的加粗语句应该是这么个意思:
当我真正了解了这句话的意思之后,也就是当我看了很多AC代码之后,我终于知道了BFS的时候,求得是 所有点距离 终点的最短距离。当最短距离满足上图中的不等式时,方案才可行。 代表位置
与终点之间的 可行的方案数量。
对于优先权队列的自定义排序顺序。
参考了这个博客:priority_queue & 结构体||类 & 自定义比较函数cmp
虽然我下面代码是一种写法,但是我现在想让自己学会这个写法
struct Point
{
int x,y;
ll step;
};
typedef struct Point pt;
struct cmp
{
bool operator() (pt a,pt b)
{
return a.step>b.step;//从小到大排序
}
};
priority_queue<pt,vector<pt>,cmp > q;
这样,跟我以前背的 sort的自定义函数很相似。
//AC 终于A了,难受
//HDU 1428
#include<iostream>
#include<algorithm>
#include<set>
#include<cstring>
#include<queue>
#include<stack>
#include<vector>
#include<stdio.h>
using namespace std;
#define ll long long
ll mp[55][55];
ll book[55][55];
ll dp[55][55];
int nextx[4]={-1,1,0,0};
int nexty[4]={0,0,-1,1};
int n;
struct Point
{
int x,y;
ll step;
};
typedef struct Point pt;
bool operator > (pt a,pt b)
{
return a.step>b.step;//从小到大排序
}
int bfs()
{
pt s;
s.x=n; s.y=n; s.step=mp[n][n];
book[n][n]=mp[n][n];
priority_queue<int,vector<pt>,greater<pt> > q;
q.push(s);
while(!q.empty())
{
pt temp=q.top();
q.pop();
int tx=temp.x;
int ty=temp.y;
ll tstep=temp.step;
for(int i=0;i<4;i++)
{
int xx=tx+nextx[i];
int yy=ty+nexty[i];
ll ss;
if(book[xx][yy]!=-1)
continue;
if(xx<1 || yy<1 || xx>n || yy>n)
continue;
ss=mp[xx][yy]+tstep;
book[xx][yy]=ss;
pt next;
next.step=ss; next.x=xx ; next.y=yy;
q.push(next);
}
// book[tx][ty]=true;
}
}
ll DP(int x,int y)
{
if(x==n && y==n) return 1;
if(dp[x][y]) return dp[x][y];
for(int i=0;i<4;i++)
{
int xx=x+nextx[i];
int yy=y+nexty[i];
if(xx<1 || yy<1 || xx>n || yy>n)
continue;
if(book[xx][yy]<book[x][y])
dp[x][y]+=DP(xx,yy);
}
return dp[x][y];
}
int main()
{
while(scanf("%d",&n)!=EOF)
{
memset(mp,0,sizeof(mp));
memset(book,-1,sizeof(book));
memset(dp,0,sizeof(dp));
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
cin>>mp[i][j];
}
bfs();
// cout<<"----"<<endl;
// for(int i=1;i<=n;i++)
// {
// for(int j=1;j<=n;j++)
// cout<<book[i][j]<<" ";
// cout<<endl;
// }
dp[n][n]=1;
DP(1,1);
// cout<<"----"<<endl;
// for(int i=1;i<=n;i++)
// {
// for(int j=1;j<=n;j++)
// cout<<dp[i][j]<<" ";
// cout<<endl;
// }
cout<<dp[1][1]<<endl;
}
}