本文主要是介绍URAL 1325--Dirt,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
题目:
1325. Dirt
Time limit: 0.5 second
Memory limit: 64 MB
— Hello, may I speak to Petrov, please? Hello, my darling… You know, there was a little accident at our home… No, no, don't worry, your computer was not damaged. It is only a bit dirty there now. Well, I should say it's very dirty there and I'm at my Mom's now. Of course, I'll clean it… When? Well, maybe when I have my vacation. What? Well, when we are back from Turkey… the next vacation then. I'll stay at Mother's until then, and you may live here also. No, no, I don't insist, sure, you may stay at home if you wish so. I prepared boots for you, they are at the door. But please, don't make it worse, before you step on a clean floor, change your boots, put on your slippers, they are at the door also. Take them with you when you walk through the dirt. And when you walk on a clean floor, take the boots with you. You see, the dirt is in different places. OK, my love? Thank you!
It is not a great pleasure to change boots each time you get from a clean floor to a dirty floor and vice versa, it's easier to walk extra several meters. So it is necessary to find a way of getting from one place in the apartment to another with the minimal possible number of boots changes; and among these paths the shortest one must be found.
To begin with, it is natural to determine an optimal way of passing the Most Important Route: from the computer to the refrigerator.
Input
The first line of the input contains two integers M and N, which are dimensions of the apartment (in meters), 1 <= N, M <= 500. The two integers in the second line are the coordinates of the computer, and the third line contains the coordinates of the refrigerator. Each of the following M lines contains N symbols; this is the plan of the apartment. On the plan, 1 denotes a clean square, 2 denotes a dirty square, and 0 is either a wall or a square of impassable dirt. It is possible to get from one square to another if they have a common vertex. When you pass from a clean square to a dirty one or vice versa, you must change shoes. The computer and the refrigerator are not on the squares marked with 0.
The upper left square of the plan has coordinates (1, 1).
Output
You should output two integers in one line separated with a space. The first integer is the length of the shortest path (the number of squares on this path including the first and the last squares) with the minimal possible number of boots changes. The second number is the number of boots changes. If it is impossible to get from the computer to the refrigerator, you should output 0 0.
Sample
input | output |
---|
3 7
1 1
3 7
1200121
1212020
1112021 | 8 4 |
题意:从冰箱的地方去电脑的地方找到一条换鞋的次数最少并且在换鞋次数最少的基础上找到一条最短路。换鞋的规则如下:
1. 从干净的地方去脏的地方要换鞋,反之亦然。
2. 从脏的地方去脏的地方不用换鞋,干净去干净的地方也不用。
思路:题目涉及到两个优先级,首先是换鞋次数少的优先,如果换鞋次数一致那么路程短的优先。之前用优先队列,用isit数组标记入队的点不再入队,一直wa,后来发现会出现问题,具体原理现在还没找到,以后再去看看。后来单独用了一个结构体记录到某一点的最短换鞋及最短路,每次更新的时候再判断一下是否换鞋次数更少并且路最短,如果是及时该点之前入队过依然再入队,并且更新,这样更有保证。
实现:
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <math.h>
#include <queue>
using namespace std;const int MAX = 505;
const int L = 10000000000;int _map[MAX][MAX];
int m, n;
int sx, sy, ex, ey;
int x[8] = {-1, -1, 0, 1, 1, 1, 0, -1};
int y[8] = {0, 1, 1, 1, 0, -1, -1, -1};struct node {int dirty;int step;friend bool operator < (node n1, node n2) {if (n1.dirty != n2.dirty)return n1.dirty > n2.dirty;return n1.step > n2.step;}int xj, yj;
};struct path {int p;int d;
}pa[MAX][MAX];bool bfs() {priority_queue <node>q;node start;start.xj = sx;start.yj = sy;start.dirty = 0;start.step = 1;pa[sx][sy].p = 1;pa[sx][sy].d = 0;q.push(start);while(!q.empty()) {node head;node tail;head = q.top();if (head.xj == ex && head.yj == ey)return true;q.pop();for (int i = 0; i < 8; i++) {int xi = head.xj + x[i];int yi = head.yj + y[i];tail.xj = xi;tail.yj = yi;if (xi >= 1 && xi <= m && yi >= 1 && yi <= n && _map[xi][yi] != 0) {tail.step = head.step + 1;if (_map[xi][yi] != _map[head.xj][head.yj])tail.dirty = head.dirty + 1;elsetail.dirty = head.dirty;if (pa[tail.xj][tail.yj].d > tail.dirty || pa[tail.xj][tail.yj].d == tail.dirty && pa[tail.xj][tail.yj].p > tail.step) {pa[tail.xj][tail.yj].d = tail.dirty;pa[tail.xj][tail.yj].p = tail.step;q.push(tail);}}}}return false;
}int main() {while (scanf("%d%d", &m, &n) != EOF) {scanf("%d%d%d%d", &sx, &sy, &ex, &ey);getchar();char l;for (int i = 1; i <= m; i++) {for (int j = 1; j <= n; j++) {scanf("%c", &l);_map[i][j] = l - '0';pa[i][j].d = L;pa[i][j].p = L;}getchar();}if (bfs())printf("%d %d\n", pa[ex][ey].p, pa[ex][ey].d);elseprintf("0 0\n");}
}
这篇关于URAL 1325--Dirt的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!