HDUOJ 1428

漫步校园

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 5161    Accepted Submission(s): 1610


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
 
BFS
 
相对于普通的BFS题,难点在于记录最短路的条数,在适当的时候num++,HDU讨论版上大佬们的思路是从终点开始搜索。
 
用记忆化搜索也可以做。
 
新学到的奇巧淫技是对比较运算符进行重载,可以省去一部分麻烦。
 
以下是AC代码:
 
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <algorithm>
 5 #include <cmath>
 6 #include <queue>
 7 
 8 using namespace std;
 9 
10 const int INF = 0x3f3f3f3f;//无穷大
11 const int maxn = 51;
12 
13 int go[4][2] = { 1,0,-1,0,0,1,0,-1 };//方向
14 struct node
15 {
16     int x, y;
17 };
18 
19 int board[maxn][maxn];//记录位置(i,j)的值
20 int dis[maxn][maxn];//位置(i,j)所经历的最短路
21 int vis[maxn][maxn], n;//记录是否走过
22 
23 bool operator < (const node &t1, const node &t2)
24 {
25     return dis[t1.x][t1.y] > dis[t2.x][t2.y];
26 }//重载了比较运算符,用于
27 
28 long long num[maxn][maxn];//注意数据范围
29 
30 void bfs()
31 {
32     int i;
33     priority_queue<node> q;//定义一个优先队列
34     node now, next;//动态储存现在所在位置和下一步的位置对应的值
35 
36     now.x = n;
37     now.y = n;//重点,从重点开始搜索
38 
39     q.push(now);
40     memset(vis, 0, sizeof(vis));//根据求最小的决策,初始化为0
41     vis[n][n] = 1;//搜索开始
42     while (!q.empty())//搜索持续到队列为空
43     {
44         now = q.top();
45         q.pop();
46         for (i = 0; i <= 3; i++)
47         {
48             next.x = now.x + go[i][0];
49             next.y = now.y + go[i][1];
50             if (next.x >= 1 && next.x <= n && next.y >= 1 && next.y <= n && dis[next.x][next.y] > dis[now.x][now.y])
51             //加上计数数组累加的条件
52             {
53                 num[next.x][next.y] += num[now.x][now.y];//记录最短路的数量
54                 if (!vis[next.x][next.y] && dis[next.x][next.y] >= dis[now.x][now.y] + board[next.x][next.y])
55                 {
56                     dis[next.x][next.y] = dis[now.x][now.y] + board[next.x][next.y];
57                     vis[next.x][next.y] = 1;//将next标记为已访问
58                     q.push(next);//更新队列
59                 }
60             }
61         }
62     }
63 }
64 
65 int main()
66 {
67     int i, j;
68     while (~scanf_s("%d", &n))
69     {
70         memset(dis, 0x3f3f, sizeof(dis));
71         memset(num, 0, sizeof(num));
72         num[n][n] = 1;
73 
74         for (i = 1; i <= n; i++)
75             for (j = 1; j <= n; j++)
76                 scanf_s("%d", &board[i][j]);
77 
78         dis[n][n] = board[n][n];
79 
80         bfs();
81 
82         printf("%I64d
", num[1][1]);//long long 类型对应的格式字符
83     }
84     return 0;
85 }
原文地址:https://www.cnblogs.com/CofJus/p/10028815.html