CF 173B Chamber of Secrets 最短路
来源:互联网 发布:黑客帝国重访矩阵 编辑:程序博客网 时间:2024/05/19 02:22
题意:给定一个图,图中的#代表一个塔,图的右下角有一条蛇怪,向左边发射一条光线,光线经过被magic的塔能向四个方向发射,问经过最少多少次magic可以使每个从左上角进入的人都被石化。
思路:一开始思路很简单,直接用最简单的最短路,每次在四个方向上找到塔。然后push进队列,最后只要找到横坐标为1的塔即跳出。实现很简单,但是直接超时了,然后学长提供了一种很神的方法。
如果(x,y)是一个塔,那么代表节点i到j+n有一条路,j+n到i有一条路,然后问题就简化成找到节点1-n的最短路。
#include <iostream>#include <cstdio>#include <algorithm>#include <string>#include <cmath>#include <cstring>#include <queue>#include <set>#include <vector>#include <stack>#include <map>#include <iomanip>#define PI acos(-1.0)#define Max 1000005#define inf 1<<28#define LL(x) (x<<1)#define RR(x)(x<<1|1)using namespace std;vector<int>g[Max];int n,m;int visit[Max];int num[Max];queue<int >q;void bfs(){ int i,j; visit[n]=1; num[n]=0; q.push(n); while(!q.empty()) { int tt=q.front(); q.pop(); for(i=0; i<g[tt].size(); i++) { int temp=g[tt][i]; if(!visit[temp]) { visit[temp]=1; q.push(temp); num[temp]=num[tt]+1; } } }}int main(){ int i,j,k,l; char x; cin>>n>>m; for(i=1; i<=n; i++) { for(j=1; j<=m; j++) { cin>>x; if(x=='#') { g[i].push_back(j+n); g[j+n].push_back(i); } } } bfs(); if(num[1]) cout<<num[1]<<endl; else cout<<-1<<endl; return 0;}
- CF 173B Chamber of Secrets 最短路
- CodeForces 173B Chamber of Secrets(最短路)
- CF173B:Chamber of Secrets(最短路)
- B. Chamber of Secrets
- Codeforces 173B Chamber of Secrets 题解
- Harry Potter and the Chamber of Secrets
- cf 689 B(最短路)
- CF 715B 最短路
- CF 543B 图,最短路
- cf B. Obsession with Robots 判断最短路?
- CF 449B - Jzzhu and Cities(最短路)
- CF 505B Mr. Kitayuta's Colorful Graph(最短路)
- cf#33-B - String Problem-反向建图+最短路
- cf 602 C(最短路)
- bzoj 4956: [Wf2017]Secret Chamber at Mount Rushmore(最短路)
- 最短路 -B
- CF 96D Volleyball(最短路套最短路)
- cf 507E Breaking Good 最短路
- svn:externals实现项目资源的共享
- Android结束Activity退出程序的一种有效方法
- nginx 反向代理上网
- Tomcat入门
- 这个将虚函数,太详细了
- CF 173B Chamber of Secrets 最短路
- 页面跳转方法总结
- ffmpeg+sdl教程——编写一个简单的播放器1
- 文件系统制作(cramfs,jffs2)及busybox编译
- (VC)半透明的实现
- 再写服务器_打死不修改_第二版
- hdu1847
- ffmpeg+sdl教程——编写一个简单的播放器2(输出视频到屏幕)
- C语言 循环单链表各种基本运算的算法