Dirt

— 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.
Example
input
3 7
1 1
3 7
1200121
1212020
1112021
output
8 4

题目大意: 有一张N*M的地图,上面有些格子是干净的有些是脏的,人从干净的格子走到脏的格子需要换鞋,从脏的走到干净的也需要换鞋。一个人从给定起点走到给定终点,问在换鞋次数最小的情况下最短距离是多少?
解题思路: 建图,将地图中每个格子作为点,相邻的非障碍物格子之间连边,用优先队列BFS,求出到达每个格子的最小换鞋数和步数,最后看终点的步数是否是INF,若是,则不可达,否则输出相应的结果。时间复杂度O(NM)O(NM)
注意点:这里的相邻是八个方向!

#include<iostream>
#include<cstdio>
#include<vector>
#include<cstring>
#include<queue>
using namespace std;
typedef long long LL;
const int MAXN=505;
const int INF=0x3f3f3f3f;
int sx,sy,ex,ey;
char G[MAXN][MAXN];
int dx[8]={0,0,1,-1,1,1,-1,-1};
int dy[8]={1,-1,0,0,1,-1,1,-1};
bool vis[MAXN][MAXN];
int n,m;
int ans1,ans2;struct node
{int x,y;int step;int change;/*node(int _x,int _y,int _step,int _change){x=_x;y=_y;step=_step;change=_change;}*/friend bool operator < (node a,node b){if(a.change==b.change) return a.step>b.step;return a.change>b.change;}
}grid[MAXN][MAXN];void bfs(int sx,int sy)
{vis[sx][sy]=true;grid[sx][sy].step=1;grid[sx][sy].change=0;priority_queue<node> pq;pq.push(grid[sx][sy]);while(!pq.empty()){node fst=pq.top();pq.pop();/*if(fst.x==ex-1&&fst.y==ey-1){ans1=fst.step;ans2=fst.change;break;}*/for(int i=0;i<8;i++){int tx=fst.x+dx[i];int ty=fst.y+dy[i];if(tx<0||tx>=n||ty<0||ty>=m||G[tx][ty]=='0') continue;vis[tx][ty]=true;node nxt;nxt.x=tx;nxt.y=ty;if(G[tx][ty]!=G[fst.x][fst.y])nxt.change=fst.change+1;elsenxt.change=fst.change;nxt.step=fst.step+1;if(grid[tx][ty]<nxt){grid[tx][ty]=nxt;pq.push(nxt);}}}
}int main()
{ios::sync_with_stdio(false);while(cin>>n>>m){cin>>sx>>sy>>ex>>ey;for(int i=0;i<n;i++){for(int j=0;j<m;j++){vis[i][j]=false;cin>>G[i][j];grid[i][j].x=i;grid[i][j].y=j;grid[i][j].change=INF;grid[i][j].step=INF;}}bfs(sx-1,sy-1);if(vis[ex-1][ey-1])cout<<grid[ex-1][ey-1].step<<" "<<grid[ex-1][ey-1].change<<endl;elsecout<<0<<" "<<0<<endl;}return 0;
}

URAL1325-Dirt相关推荐

  1. Codeforces 1148D. Dirty Deeds Done Dirt Cheap

    Codeforces 1148D. Dirty Deeds Done Dirt Cheap 传送门:https://codeforces.com/problemset/problem/1148/D 提 ...

  2. HDU - 6070 Dirt Ratio (二分 + 线段树)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6070 题目大意:给定一个序列a,对于任何一个区间 [l,r],它的"Dirt Ratio&q ...

  3. HDU 6070 Dirt Ratio(线段树、二分)

    http://acm.hdu.edu.cn/showproblem.php?pid=6070 题解 首先不难看出错误率是单调的,那么我们可以直接二分答案x,某个区间的错误率=区间数的种类cnt/区间长 ...

  4. 【二分】【线段树】hdu6070 Dirt Ratio

    size(l,r)表示区间l,r权值的种类数,让你求min{size(l,r)/(r-l+1)}(1<=l<=r<=n). last[r]表示a[r]上一次出现的位置, 就是二分验证 ...

  5. 2017 Multi-University Training Contest - Team 4:1004. Dirt Ratio

    题意:给你n个数,其中每个区间的权值就是这个区间内不同数的个数除以区间长度,求最小的权值 令size(l, r)表示区间[l, r]内不同数的个数,那么就是要求min(size(l, r)/(r-l+ ...

  6. HDU 6070 Dirt Ratio

    暑假多校赛的一道题,印象深刻,今天终于补了,现在感觉也不是特别难,题意是很经典的那种问题,就是给你一个数列,问一个区间不同的个数比区间的长度的值,在这个数列里的最小值.之前搜过区间不同数的个数的查询问 ...

  7. 腐蚀rust电脑分辨率调多少_腐蚀Rust怎么设置画面 腐蚀Rust提高帧数画面设置方法...

    腐蚀Rust这个游戏的细节取决于图像质量也就是我们进入游戏的时候可以选择画质,这里为大家带来腐蚀Rust画质设置教程. 图像质量 1~3为一个大档 4~5是一个大档 4以上你在游戏里面的画面会显示更多 ...

  8. Boom Library 93套影视游戏无损配乐音效素材合集包

    Boom Library 93套影视游戏无损配乐音效素材合集包 素材压缩包大小共:851G 每个合集为独立压缩包 可选择性下载 云桥网络 平台获取合集包 01.BOOM Library Assault ...

  9. 兰戈 —— Rango

    2019独角兽企业重金招聘Python工程师标准>>> 一部西部卡通片,据说恶搞了<正午>这部著名的西部片,可惜我没有看过<正午>.非常喜欢这部片子里的音乐, ...

  10. 面试官问你想找什么工作_找工作时如何面试面试官

    面试官问你想找什么工作 在技​​术面试中要问的十二个问题 (Twelve questions to ask at tech interviews) I've just come off six wee ...

最新文章

  1. Could not find the main class: org.apache.catalina.startup.Boostrap. Program will exit.
  2. u盘文件看得见却打不开_win7下u盘文件打不开怎么办 win7下u盘文件打不开解决方法...
  3. java 原子类能做什么_Java原子类中可以靠版本号比较为什么还需要CAS操作??
  4. OpenCASCADE绘制测试线束:OCAF 命令之基本命令
  5. oracle中如何创建一个过程,如何开发ORACLE存储过程
  6. java 正则匹配 sql星号,正则表达式匹配星号和换行符之间的字符串
  7. 成本预算的四个步骤_全网推广步骤有哪些?
  8. 【Rényi差分隐私和零集中差分隐私(差分隐私变体)代码实现】差分隐私代码实现系列(九)
  9. 二套“非普通住宅”是否认贷不认房 各地口径不一
  10. Advanced User Administration
  11. 3. PCRE 兼容正则表达式
  12. 怎样利用php记录时间差,使用PHP计算出时间差的实现方法
  13. 计算机专业代码933,数学类专业代码
  14. 刘强东宣布:京东减员50%,每天工作3小时!这样的未来是你想要的吗?
  15. hp 磁带机安装配置
  16. java多线程那些靠谱的总结
  17. Docker的Pull Digest和Image ID
  18. Texmacs使用注意事项
  19. 华为云云原生之多云管理利器Karmada从0到1的实操【与云原生的故事】
  20. 多址接入技术 FDMA TDMA CDMA NOMA

热门文章

  1. 高端存储系统的发展方向展望
  2. Excel创建堆积柱形混合折线图
  3. mysql byte存入数据库_byte 保存到数据库
  4. Python实战:导出QQ聊天记录生成词云看看你和你的女友聊了什么
  5. 计算机专业的宣传,计算机专业招生宣传口号
  6. HDU 2079 (母函数)
  7. 信息安全 —— 密码学
  8. 代理ip填写格式有什么要求?
  9. (五)谷歌地图坐标转换:经纬度如何实现转换成谷歌地图平面坐标
  10. 【转】【干案例】江小白的微博营销 看完直接给跪了!