Easy Graph Problem?(一道简单图论题?) bfs + 优先队列

                    Easy Graph Problem?

题目抽象:给你一个n * m 的矩阵.每个格子是数字或者障碍物‘*‘. 第一问:从起点到终点的最短路.第二问:要求相同的方向不能连续走两次,也就是每到达一个格子,需要改变方向.问最短路.  不过不存在就输出-1.

分析:可以直接bfs搜索.第一问有一个minv[MS][MS]数组记录到达(i,j)的最短距离.第二问用flag[MS][MS][4]及记录(i,j)格子k方向是否走过.

由于2<=n,m<=500,范围较大.所以需要优先队列优化.  不然超时.

  1 #include <cstdio>
  2 #include <algorithm>
  3 #include <queue>
  4 using namespace std;
  5 typedef long long LL;
  6 const int INF = 0X5FFFFF;
  7 const int MS = 505;
  8
  9 struct node {
 10     int x, y, dir, cost;
 11     bool operator < (const node & a) const {
 12         return cost > a.cost;
 13     }
 14 };
 15 int pic[MS][MS];
 16 int dir[4][2] = {0,1,1,0,0,-1,-1,0};
 17 int flag[MS][MS][4];
 18 int minv[MS][MS];
 19
 20 int n, m, r1, c1, r2, c2;
 21 int kase = 1;
 22 node s,t;
 23
 24 void bfs1() {
 25     minv[r1][c1] = pic[r1][c1];
 26     s.x = r1;
 27     s.y = c1;
 28     s.cost = pic[r1][c1];
 29     priority_queue<node> que;
 30     que.push(s);
 31     while (!que.empty()) {
 32         s = que.top();
 33         que.pop();
 34         for (int i = 0; i < 4; i++) {
 35             int x = s.x + dir[i][0];
 36             int y = s.y + dir[i][1];
 37             if (x > 0 && x <= n && y > 0 && y <= m && pic[x][y]) {
 38                 t.x = x;
 39                 t.y = y;
 40                 t.cost = s.cost + pic[x][y];
 41                 if (x == r2 && y == c2) {
 42                     printf(" %d", t.cost);
 43                     return ;
 44                 }
 45                 if (t.cost < minv[x][y]) {
 46                     minv[x][y] = t.cost;
 47                     que.push(t);
 48                 }
 49             }
 50         }
 51     }
 52     printf(" -1");
 53 }
 54
 55 void bfs2() {
 56     s.x = r1;
 57     s.y = c1;
 58     s.cost = pic[r1][c1];
 59     priority_queue<node> que;
 60     for (int i = 0; i < 4; i++) {
 61         flag[r1][c1][i] = 1;
 62         int x = s.x + dir[i][0];
 63         int y = s.y + dir[i][1];
 64         if (x > 0 && x <= n && y > 0 && y <= m && pic[x][y]) {
 65             flag[x][y][i] = 1;
 66             t.x = x;
 67             t.y = y;
 68             t.cost = s.cost + pic[x][y];
 69             t.dir = i;
 70             que.push(t);
 71         }
 72     }
 73     while (!que.empty()) {
 74         s = que.top();
 75         que.pop();
 76         for (int i = 0; i < 4; i++) {
 77             if (i == s.dir)
 78                 continue;
 79             t.x = s.x + dir[i][0];
 80             t.y = s.y + dir[i][1];
 81             if (t.x > 0 && t.x <= n && t.y > 0 && t.y <= m && pic[t.x][t.y]) {
 82                 t.cost = s.cost + pic[t.x][t.y];
 83                 t.dir = i;
 84                 if (t.x == r2 && t.y == c2) {
 85                     printf(" %d",t.cost);
 86                     return ;
 87                 }
 88                 if (flag[t.x][t.y][t.dir] == 0) {
 89                     flag[t.x][t.y][t.dir] = 1;
 90                     que.push(t);
 91                 }
 92             }
 93
 94         }
 95     }
 96     printf(" -1");
 97 }
 98
 99 int main() {
100     char str[MS];
101     while (scanf("%d%d%d%d%d%d", &n, &m, &r1, &c1, &r2, &c2) != EOF) {
102         for (int i  = 1; i <= n; i++)
103             for (int j = 1; j <= m; j++) {
104                 scanf("%s", str);
105                 if (str[0] == ‘*‘) {
106                     pic[i][j] = 0;
107                 }
108                 else {
109                     sscanf(str, "%d", &pic[i][j]);  // 初始化图
110                 }
111                 minv[i][j] = INF;   //  (i,j) 最小花费初始化
112                 for (int k = 0; k < 4; k++)
113                     flag[i][j][k] = 0;  // (i,j) 四个方向初始化
114             }
115         printf("Case %d:",kase++);
116         bfs1();
117         bfs2();
118         printf("\n");
119     }
120     return 0;
121 }
时间: 2024-08-08 05:38:21

Easy Graph Problem?(一道简单图论题?) bfs + 优先队列的相关文章

[ACM] hdu 1242 Rescue (BFS+优先队列)

Rescue Problem Description Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The prison is described as a N * M (N, M <= 200) matrix. There are WALLs, ROADs, and GUARDs in the prison. Angel's friends want to save Angel. Their task is:

Battle City BFS+优先队列

Battle City Many of us had played the game "Battle city" in our childhood, and some people (like me) even often play it on computer now. What we are discussing is a simple edition of this game. Given a map that consists of empty spaces, rivers,

hdu 3345 War Chess (bfs+优先队列)

War Chess Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1732    Accepted Submission(s): 416 Problem Description War chess is hh's favorite game: In this game, there is an N * M battle map, an

HDU-5025-Saving Tang Monk(BFS+优先队列)

Problem Description <Journey to the West>(also <Monkey>) is one of the Four Great Classical Novels of Chinese literature. It was written by Wu Cheng'en during the Ming Dynasty. In this novel, Monkey King Sun Wukong, pig Zhu Bajie and Sha Wujin

UVALive 6485 Electric Car Rally (BFS,优先队列)

https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=4496 In an attempt to demonstrate the practicality of electric cars, ElecCarCo is sponsoring a cross-country road rally. There are n chargin

POJ 1130(一道纯水,bfs+dfs)

POJ 1130 大概题意:给出一副图,求从起点到终点 (0->ET) 必须经过的一点. 我的思路:首先DFS求出经过每点的次数,必过的一点的次数一定最高,但是就这样吗?有可能有多个必过的点,所以还要找出离ET最近的点,这里就利用BFS逐层搜索的性质求每点到ET的距离. #include<iostream> #include<cstdio> #include<string.h> #include<iomanip> #include<queue&g

HDU-2437-Jerboas(BFS+优先队列)

Problem Description Jerboas are small desert-living animals, which resemble mice with a long tufted tail and very long hind legs. Jerboas shelter in well-hidden burrows. They create two types of burrow: temporary and permanent. The temporary burrows

hdu 1195 Open the Lock (bfs+优先队列)

Open the Lock Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 4253    Accepted Submission(s): 1858 Problem Description Now an emergent task for you is to open a password lock. The password is c

HDU-2102-A计划(BFS+优先队列)

Problem Description 可怜的公主在一次次被魔王掳走一次次被骑士们救回来之后,而今,不幸的她再一次面临生命的考验.魔王已经发出消息说将在T时刻吃掉公主,因为他听信谣言说吃公主的肉也能长生不老.年迈的国王正是心急如焚,告招天下勇士来拯救公主.不过公主早已习以为常,她深信智勇的骑士LJ肯定能将她救出. 现据密探所报,公主被关在一个两层的迷宫里,迷宫的入口是S(0,0,0),公主的位置用P表示,时空传输机用#表示,墙用*表示,平地用.表示.骑士们一进入时空传输机就会被转到另一层的相对位