题意:给定上一个n*m的矩阵,你从(1,1)这个位置发出水平向的光,碰到#可以选择四个方向同时发光,或者直接穿过去,

问你用最少的#使得光能够到达 (n,m)并且方向水平向右。

析:很明显的一个最短路,但是矩阵有点大啊。1000*1000,普通的肯定要超时啊,所以先通过#把该该图的行和列建立成二分图,

然后再跑最短路,这样就简单多了。

代码如下:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <sstream>
#define debug() puts("++++");
#define gcd(a, b) __gcd(a, b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std; typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const LL LNF = 1e17;
const double inf = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 2000 + 10;
const int mod = 1000000007;
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, 1, 0, -1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c){
return r >= 0 && r < n && c >= 0 && c < m;
} char s[maxn];
vector<int> G[maxn];
bool vis[maxn];
int dp[maxn]; int bfs(){
memset(dp, INF, sizeof dp);
dp[1] = 0;
vis[1] = true;
queue<int> q;
q.push(1); while(!q.empty()){
int u = q.front(); q.pop();
if(u == n) return dp[u];
for(int i = 0; i < G[u].size(); ++i){
int v = G[u][i];
if(dp[v] > dp[u] + 1){
dp[v] = dp[u] + 1;
if(vis[v]) continue;
vis[v] = true;
q.push(v);
}
}
}
return -1;
} int main(){
scanf("%d %d", &n, &m);
for(int i = 1; i <= n; ++i){
scanf("%s", s+1);
for(int j = 1; j <= m; ++j)
if(s[j] == '#'){
G[i].push_back(j+n);
G[j+n].push_back(i);
}
}
printf("%d\n", bfs());
return 0;
}

  

CodeForces 173B Chamber of Secrets (二分图+BFS)的更多相关文章

  1. CodeForces 173B Chamber of Secrets 二分图+最短路

    题目链接: http://codeforces.com/problemset/problem/173/B 题意: 给你一个n*m的地图,现在有一束激光从左上角往左边射出,每遇到‘#’,你可以选择光线往 ...

  2. CodeForces 173B Chamber of Secrets spfa

    Chamber of Secrets 题目连接: http://codeforces.com/problemset/problem/173/B Description "The Chambe ...

  3. 【CF173B】Chamber of Secrets(二分图,最短路)

    题意:给你一个n*m的地图,现在有一束激光从左上角往右边射出,每遇到‘#’,你可以选择光线往四个方向射出,或者什么都不做,问最少需要多少个‘#’往四个方向射出才能使关系在n行往右边射出. 思路:将每一 ...

  4. Codeforces Gym 100187E E. Two Labyrinths bfs

    E. Two Labyrinths Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100187/prob ...

  5. CodeForces 687A NP-Hard Problem(二分图判定)

    这本来一个挺简单的题呢,结果让我给想复杂了,二分图就是把图分成了两部分,然后不同颜色各一边,肯定是满足题目中说的边和点的条件的,真是犯二了.. 代码如下: #include<iostream&g ...

  6. CodeForces - 516B Drazil and Tiles(bfs)

    https://vjudge.net/problem/CodeForces-516B 题意 在一个n*m图中放1*2或者2*1的长方形,问是否存在唯一的方法填满图中的‘.’ 分析 如果要有唯一的方案, ...

  7. Codeforces.542E.Playing on Graph(二分图)

    题目链接 \(Description\) 给出一个n个点m条边的无向图. 你每次需要选择两个没有边相连的点,将它们合并为一个新点,直到这张图变成了一条链. 最大化这条链的长度,或输出无解. n< ...

  8. CodeForces - 616C(很有意思的bfs,set,map的使用)

    传送门: http://codeforces.com/problemset/problem/616/C C. The Labyrinth time limit per test 1 second me ...

  9. CodeForces - 580C Kefa and Park 【BFS】

    题目链接 http://codeforces.com/problemset/problem/580/C 题意 根节点是 1 然后所有的叶子结点都是饭店 从根节点到叶子结点的路径上 如果存在 大于m 个 ...

随机推荐

  1. sklearn_算法选择

  2. LeetCode Distribute Candies

    原题链接在这里:https://leetcode.com/problems/distribute-candies/#/description 题目: Given an integer array wi ...

  3. 「LOJ#10051」「一本通 2.3 例 3」Nikitosh 和异或(Trie

    题目描述 原题来自:CODECHEF September Challenge 2015 REBXOR 1​​≤r​1​​<l​2​​≤r​2​​≤N,x⨁yx\bigoplus yx⨁y 表示 ...

  4. GO语言heap剖析及利用heap实现优先级队列

    GO语言heap剖析 本节内容 heap使用 heap提供的方法 heap源码剖析 利用heap实现优先级队列 1. heap使用 在go语言的标准库container中,实现了三中数据类型:heap ...

  5. VS软件版本号定义、规则和相关的Visual Studio插件

    http://blog.csdn.net/cnhk1225/article/details/37500593 软件版本号主要标识了软件的版本,通过其可以了解软件.类库文件的当前版本,使得软件版本控制有 ...

  6. 跨域问题解决方案之chrome插件

    地址: https://chrome.google.com/webstore/detail/allow-control-allow-origi/nlfbmbojpeacfghkpbjhddihlkki ...

  7. jquery中stop停止动画笔记

    jQuery stop() 方法用于停止动画或效果,在它们完成之前. stop() 方法适用于所有 jQuery 效果函数,包括滑动.淡入淡出和自定义动画. 语法: $(selector).stop( ...

  8. HTML5两个打包工具

    AppCan:http://www.appcan.cn/ HBulider:http://www.dcloud.io/

  9. 机器学习:PCA(降噪)

    一.噪音 噪音产生的因素:可能是测量仪器的误差.也可能是人为误差.或者测试方法有问题等: 降噪作用:方便数据的可视化,使用样本特征更清晰:便于算法操作数据: 具体操作:从 n 维降到 k 维,再讲降维 ...

  10. java代码split分割数字类

    总结:正则表达式-- package com.c2; //写一个spli的用法,数字类 ===分割字符串 public class yqw { public static void main(Stri ...