URAL 1325--Dirt

2024-04-09 20:38
文章标签 ural 1325 dirt

本文主要是介绍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

inputoutput
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的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/889156

相关文章

ural 1297. Palindrome dp

1297. Palindrome Time limit: 1.0 second Memory limit: 64 MB The “U.S. Robots” HQ has just received a rather alarming anonymous letter. It states that the agent from the competing «Robots Unli

ural 1149. Sinus Dances dfs

1149. Sinus Dances Time limit: 1.0 second Memory limit: 64 MB Let  An = sin(1–sin(2+sin(3–sin(4+…sin( n))…) Let  Sn = (…( A 1+ n) A 2+ n–1) A 3+…+2) An+1 For given  N print  SN Input One

ural 1820. Ural Steaks 贪心

1820. Ural Steaks Time limit: 0.5 second Memory limit: 64 MB After the personal contest, happy but hungry programmers dropped into the restaurant “Ural Steaks” and ordered  n specialty steaks

ural 1017. Staircases DP

1017. Staircases Time limit: 1.0 second Memory limit: 64 MB One curious child has a set of  N little bricks (5 ≤  N ≤ 500). From these bricks he builds different staircases. Staircase consist

ural 1026. Questions and Answers 查询

1026. Questions and Answers Time limit: 2.0 second Memory limit: 64 MB Background The database of the Pentagon contains a top-secret information. We don’t know what the information is — you

ural 1014. Product of Digits贪心

1014. Product of Digits Time limit: 1.0 second Memory limit: 64 MB Your task is to find the minimal positive integer number  Q so that the product of digits of  Q is exactly equal to  N. Inpu

后缀数组 - 求最长回文子串 + 模板题 --- ural 1297

1297. Palindrome Time Limit: 1.0 second Memory Limit: 16 MB The “U.S. Robots” HQ has just received a rather alarming anonymous letter. It states that the agent from the competing «Robots Unlim

【URAL】1057 Amount of Degrees 数位DP

传送门:【URAL】1057 Amount of Degrees 题目分析:将数转化成能达到的最大的01串,串上从右往左第i位为1表示该数包括B^i。 代码如下: #include <cstdio>#include <cstring>#include <algorithm>using namespace std ;typedef long long LL ;#de

HDU 1325(并查集判断一个图是否是一棵树)

题意:每组数据都以0 0结束,-1 -1结束程序。 每组数据中的每两个数字为一小组,前一个数字代表的结点指向后一个数字代表的结点。   #include <iostream>#include <cstring>using namespace std;int father[100010];int find_father(int x){while (father[x] != x)x

【并查集】HDU 1325 Is It A Tree?

HDU 1325 Is It A Tree? 就是判断能不能成为一棵树。空树也是树。 根据连通性,根节点个数,入度出度等。 #include <stdio.h>#include <iostream>#include <string>#include <cstring>using namespace std;int fa[100005], vis[100005];int in[1