hdu 1428 优先队列广搜+记忆化搜索

来源:互联网 发布:智能网络电视哪个好 编辑:程序博客网 时间:2024/05/21 06:31

理解清楚题意就好了。。。


ACcode:

#include<stdio.h>#include<string.h>#include<queue>using namespace std;const int nsize=55;int mark[nsize][nsize];int map[nsize][nsize],n;__int64 dp[nsize][nsize];int dir[4][2]= {0,1,0,-1,1,0,-1,0};struct Node{    int x,y,time;    friend bool operator < (Node x1,Node x2) //优先队列,时间小的先出队    {        return x1.time > x2.time;    }};bool isboundary(int x,int y){    if (x<1||x>n||y<1||y>n) return false;    return true;}void bfs(){    priority_queue<Node>q;    Node cur,next;    cur.x=n; cur.y=n;//从(n,n)开始往前找,每次找最小时间到达(n,n)    cur.time=map[n][n];    mark[n][n]=false;    q.push(cur);    while(!q.empty())    {        cur=q.top();        q.pop();        for(int i=0; i<4; i++)        {            next.x=cur.x+dir[i][0];            next.y=cur.y+dir[i][1];            if(isboundary(next.x,next.y)&&mark[next.x][next.y])            {                mark[next.x][next.y]=false;                map[next.x][next.y]+=cur.time;//在原数组记录每个点到终点的最短时间                next.time=map[next.x][next.y];                q.push(next);            }        }    }}__int64 dfs(int x,int y){    if(dp[x][y]>0) return dp[x][y];    for(int i=0; i<4; i++)    {        int v=x+dir[i][0];        int u=y+dir[i][1];        if(isboundary(u,v)&&map[x][y]>map[v][u])//保证离终点原来越近            dp[x][y]+=dfs(v,u);    }    return dp[x][y];}int main(){    while(scanf("%d",&n)!=EOF)    {        memset(dp,0,sizeof(dp));        memset(mark,true,sizeof(mark));        for(int i=1; i<=n; i++)            for(int j=1; j<=n; j++)                scanf("%d",&map[i][j]);        bfs();        dp[n][n]=1;        printf("%I64d\n",dfs(1,1));    }    return 0;}


原创粉丝点击