题意:给定上一个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. UVA 11176 Winning Streak

    #include <iostream> #include <stdio.h> #include <cstring> #define N 501 using name ...

  2. LeetCode Shortest Unsorted Continuous Subarray

    原题链接在这里:https://leetcode.com/problems/shortest-unsorted-continuous-subarray/description/ 题目: Given a ...

  3. python沉淀之路~~整型的属性

    python的基础知识: 基本数据类型:int   str   list   tuple   dict   bool 一.整型的属性功能 1.工厂方法将字符串转换成整型 a = " b = ...

  4. CF 622F The Sum of the k-th Powers——拉格朗日插值

    题目:http://codeforces.com/problemset/problem/622/F 发现 sigma(i=1~n) i 是一个二次的多项式( (1+n)*n/2 ),sigma(i=1 ...

  5. ECMAScript 2016(ES7) 知多少

    ECMAScript 2016(ES7) 知多少 1. 数组方法 Array.prototype.includes(value : any) : boolean 2. 幂运算符 x ** y 扩展阅读 ...

  6. java代码排序问题

    总结: package com.ja; import java.util.Arrays; import java.util.Collections; public class mili { publi ...

  7. 并集(union和union all的区别)、交集、差集、全连接

    一.并集 Union因为要进行重复值扫描,所以效率低.如果合并没有刻意要删除重复行,那么就使用Union All 两个要联合的SQL语句 字段个数必须一样,而且字段类型要“相容”(一致): 如果我们需 ...

  8. 关于SQL结构化查询语言中(+)的用法

    一.概述 (+):从符号上理解为添加一些内容进入结果集中,那么自然会问到这么几个问题 1.添加什么内容 2.怎么添加 3.添加到什么结果集中 以下内容将以实例说明上面3个问题. 二.实例 以Oracl ...

  9. java.lang.ClassCastException: org.apache.catalina.connector.RequestFacade cannot be cast to org.springframework.web.multipart.MultipartHttpServletRequest

    转自:https://blog.csdn.net/iteye_17476/article/details/82651580 java.lang.ClassCastException: org.apac ...

  10. 对数组名取地址 a[ ],&a

    C语言规定,数组名代表数组的首地址,也就是第0号元素的地址.所以a==&a[0] 但对数组名取地址时却要注意了,在理解“对数组名取地址”这一表达式的含义时一定要记住:数组名是“数组”这种变量的 ...