CF173B:Chamber of Secrets(最短路)
来源:互联网 发布:c语言中char的意思 编辑:程序博客网 时间:2024/05/17 22:02
"The Chamber of Secrets has been opened again" — this news has spread all around Hogwarts and some of the students have been petrified due to seeing the basilisk. Dumbledore got fired and now Harry is trying to enter the Chamber of Secrets. These aren't good news for Lord Voldemort. The problem is, he doesn't want anybody to be able to enter the chamber. The Dark Lord is going to be busy sucking life out of Ginny.
The Chamber of Secrets is an n × m rectangular grid in which some of the cells are columns. A light ray (and a basilisk's gaze) passes through the columns without changing its direction. But with some spell we can make a column magic to reflect the light ray (or the gaze) in all four directions when it receives the ray. This is shown in the figure below.
The basilisk is located at the right side of the lower right cell of the grid and is looking to the left (in the direction of the lower left cell). According to the legend, anyone who meets a basilisk's gaze directly dies immediately. But if someone meets a basilisk's gaze through a column, this person will get petrified. We know that the door to the Chamber is located on the left side of the upper left corner of the grid and anyone who wants to enter will look in the direction of its movement (in the direction of the upper right cell) from that position.
Given the dimensions of the chamber and the location of regular columns, Lord Voldemort has asked you to find the minimum number of columns that we need to make magic so that anyone who wants to enter the chamber would be petrified or just declare that it's impossible to secure the chamber.
The first line of the input contains two integer numbers n and m (2 ≤ n, m ≤ 1000). Each of the next n lines contains m characters. Each character is either "." or "#" and represents one cell of the Chamber grid. It's "." if the corresponding cell is empty and "#" if it's a regular column.
Print the minimum number of columns to make magic or -1 if it's impossible to do.
3 3.#.....#.
2
4 3##.....#..#.
2
The figure above shows the first sample test. In the first sample we should make both columns magic. The dragon figure represents the basilisk and the binoculars represent the person who will enter the Chamber of secrets. The black star shows the place where the person will be petrified. Yellow lines represent basilisk gaze moving through columns.
题意:初始在第一行,目标最后一行,问最少需要几个'#'进行转弯。
思路:每个'#'自身x对y建双向边,最短路。
# include <bits/stdc++.h># define pb push_backusing namespace std;const int maxn = 2003;int n, m, q[maxn<<1], vis[maxn], dis[maxn];vector<int>v[maxn];int spfa(){ memset(vis, 0, sizeof(vis)); memset(dis, 0x3f, sizeof(dis)); int l=0, r=0; q[r++] = 1; dis[1] = 0; while(l<r) { int s = q[l++]; vis[s] = 0; for(auto e : v[s]) { if(dis[e] > dis[s] + 1) { dis[e] = dis[s] + 1; if(!vis[e]) q[r++] = e, vis[e] = 1; } } } return (dis[n] == 0x3f3f3f3f)?-1:dis[n];}int main(){ char c; scanf("%d%d",&n,&m); for(int i=1; i<=n; ++i) { getchar(); for(int j=1; j<=m; ++j) { c = getchar(); if(c == '#') { v[i].pb(j+n); v[j+n].pb(i); } } } printf("%d\n",spfa()); return 0;}
- CF173B:Chamber of Secrets(最短路)
- CodeForces 173B Chamber of Secrets(最短路)
- CF 173B Chamber of Secrets 最短路
- B. Chamber of Secrets
- Harry Potter and the Chamber of Secrets
- Codeforces 173B Chamber of Secrets 题解
- bzoj 4956: [Wf2017]Secret Chamber at Mount Rushmore(最短路)
- 最短路(最短路)
- (beginer) 最短路 UVA 10269 Adventure of Super Mario
- uva 10798 - Be wary of Roses(最短路)
- HDU 5545 The Battle of Guandu(最短路)
- POJ 2139 Six Degrees of Cowvin Bacon(最短路)
- Six Degrees of Cowvin Bacon(最短路)
- HDU 5545 The Battle of Guandu (最短路Dijkstra)
- Six Degrees of Cowvin Bacon (最短路)
- Secrets of Great Architects
- Secrets of Firefox 1.0
- Secrets of dispatch_once
- linux free命令中buffer和cache的区别
- Linux centos7第一次启动时无图形界面安装
- Linux下的五种I/O模型
- 栈和队列
- SQL优化
- CF173B:Chamber of Secrets(最短路)
- POJ
- 浅谈深度学习尝常识:泛化、过拟合、Dropout、Attention
- 第三章 第二节:修改数据表——ALTER(三) 删除约束
- HDU5444 Elven Postman(搜索二叉树模板)
- Spring—Quartz定时调度CronTrigger时间配置格式说明与实例
- UVA10618TangoTangoInsurrection
- 继承的限制
- springboot【15】缓存之EhCache