题意:给定上一个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. AtCoder Petrozavodsk Contest 001 B - Two Arrays

    Time limit : 2sec / Memory limit : 256MB Score : 300 points Problem Statement You are given two inte ...

  2. Unity物体上下反复漂浮效果

    using UnityEngine;using System.Collections;// 主界面的开始按钮使用该脚本,控制上下来回浮动public class Floating : MonoBeha ...

  3. poj 3421 X-factor Chains——质因数分解

    题目:http://poj.org/problem?id=3421 记忆化搜索竟然水过去了.仔细一想时间可能有点不对,但还是水过去了. #include<iostream> #includ ...

  4. nginx与二级域名的绑定 nginx安装

    nginx中文文档 http://www.nginx.cn/doc/ nginx 查看配置文件地址 http://blog.csdn.net/ljfrocky/article/details/5052 ...

  5. maven 历史版本下载

    1.登录http://maven.apache.org/download.cgi 2.拉倒最下面,点击 archives 3.可以看到maven个版本,找自己需要的下载

  6. [转载]Linux驱动-SPI驱动-概述

    转载地址http://blog.csdn.net/droidphone SPI是"Serial Peripheral Interface" 的缩写,是一种四线制的同步串行通信接口, ...

  7. (转)NHibernate之Generator主键生成方式

    本文转载自:http://www.cnblogs.com/lemon-love/archive/2010/03/10/1683058.html (1) assigned主键由外部程序负责生成,无需NH ...

  8. Mybatis多参数查询映射

    一.单个参数: public List<XXBean> getXXBeanList(String xxCode); <select id="getXXXBeanList&q ...

  9. PAT L3-008. 喊山(BFS)C4 初赛30分

    喊山(30 分) 喊山,是人双手围在嘴边成喇叭状,对着远方高山发出“喂—喂喂—喂喂喂……”的呼唤.呼唤声通过空气的传递,回荡于深谷之间,传送到人们耳中,发出约定俗成的“讯号”,达到声讯传递交流的目的. ...

  10. Cassandra 学习二

    Cassandra的架构 Cassandra的设计目的是处理跨多个节点的大数据工作负载,而没有任何单点故障.Cassandra在其节点之间具有对等分布式系统,并且数据分布在集群中的所有节点之间. 1 ...