zoukankan      html  css  js  c++  java
  • HDU 1428 漫步校园(记忆化搜索,BFS, DFS)

    漫步校园

    http://acm.hdu.edu.cn/showproblem.php?pid=1428

    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
     
    解题思路:运用广搜,从终点往下遍历,保存每一点到达终点所需要的最短时间,然后按最短时间从起点往上遍历,记忆化搜索可行路径,并保存可行路径数
     
    解题代码:
     1 #include <stdio.h>
     2 #include <string.h>
     3 #include <math.h>
     4 #include <iostream>
     5 #include<queue>
     6 using namespace std;
     7 typedef __int64 LL;
     8 const int max_n = 52;
     9 
    10 int map[max_n][max_n], m[max_n][max_n];
    11 LL s[max_n][max_n];
    12 int n;
    13 
    14 struct Point
    15 {
    16     int x, y;
    17 };
    18 Point pp[10000];//开小了会出现RE 
    19 
    20 const char alt_x[4] = {0, 0, -1, 1};
    21 const char alt_y[4] = {-1, 1, 0, 0};
    22 
    23 void BFS() //广搜将每一点到达终点所需的最短时间保存起来, m[x][y]为点(x,y)到达终点所需的最短时间 
    24 {
    25     int dx, dy, temp;
    26     int rear = 0, top = 0;
    27     Point tm1, tm2;
    28     pp[rear++] = (Point){n-1, n-1};
    29     m[n-1][n-1] = map[n-1][n-1];
    30     while (rear > top)
    31     {
    32         tm1 = pp[top];
    33         top ++;
    34         for (int i = 0; i < 4; i ++)
    35         {
    36             dx = tm1.x + alt_x[i];
    37             dy = tm1.y + alt_y[i];
    38             if (dx >= 0 && dx < n && dy >= 0 && dy < n)
    39             {
    40                 temp = m[tm1.x][tm1.y] + map[dx][dy];
    41                 if (m[dx][dy] == -1 || temp < m[dx][dy])
    42                 {
    43                     tm2.x = dx;
    44                     tm2.y = dy;
    45                     m[dx][dy] = temp;
    46                     pp[rear++] = tm2;
    47                 }
    48             }
    49         }
    50     }
    51 }
    52 
    53 LL DFS(int x, int y)//深搜,将路径数统计起来 
    54 {
    55     if (s[x][y] > 0)
    56         return s[x][y];
    57     if (x == n-1 && y == n-1)
    58         return 1;
    59     for (int i = 0; i < 4; i ++)
    60     {
    61         int dx = x + alt_x[i];
    62         int dy = y + alt_y[i];
    63         if (dx >= 0 && dx < n && dy >= 0 && dy < n)
    64             if (m[x][y] > m[dx][dy])//考虑从A区域到B区域仅当存在一条从B到机房的路线比任何一条从A到机房的路线更近
    65                 s[x][y] += DFS(dx, dy);
    66     }
    67     return s[x][y];
    68 }
    69 
    70 int main()
    71 {
    72     while(scanf("%d", &n) != EOF)
    73     {
    74         for (int i = 0; i < n; i ++)
    75         {
    76             for (int j = 0; j < n; j ++)
    77                 scanf ("%d", &map[i][j]);
    78         }
    79         memset(m, -1, sizeof (m));
    80         memset(s, 0, sizeof (s));
    81         BFS();
    82 DFS(0, 0); 83 printf ("%I64d ", s[0][0]); 84 } 85 return 0; 86 }
     
  • 相关阅读:
    UpdatePanel 脚本失效的解决
    DataTable 中的查询、排序及分页(c#)
    如何让Gridview在没有数据的时候显示表头
    Jquery 对.net服务器控件RadioButtonList进行赋值和取值的操作
    给Repeater控件里添加序号的5种方法
    C#缓存absoluteExpiration、slidingExpiration两个参数的疑惑
    筛选DataTable数据的方法
    CSS前端基础应用实践
    js实现页面自动刷新
    什么是MVC
  • 原文地址:https://www.cnblogs.com/shengshouzhaixing/p/3197841.html
Copyright © 2011-2022 走看看