http://codeforces.com/gym/102028/problem/F

就是一个bfs,主要问题是建图,要注意奇数和偶数列的联通方案是略有不同的。比赛的时候写完一直不过样例最后才发现没考虑奇偶,心态炸裂。

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<map>
#include<set>
#include<stack>
#include<deque>
#include<bitset>
#include<unordered_map>
#include<unordered_set>
#include<queue>
#include<cstdlib>
#include<ctype.h>
#include<ctime>
#include<functional>
#include<algorithm>
#include<bits/stdc++.h>
using namespace std;
#define LL long long
#define pii pair<int,int>
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define inf 0x3f3f3f3f
#define debug puts("debug")
#define mid ((L+R)>>1)
#define lc (id<<1)
#define rc (id<<1|1)
const int maxn=;
const int maxm=;
const double PI=acos(-1.0);
const double eps=1e-;
const LL mod=1e9+;
LL gcd(LL a,LL b){return b==?a:gcd(b,a%b);}
LL lcm(LL a,LL b){return a/gcd(a,b)*b;}
LL qpow(LL a,LL b,LL c){LL r=; for(;b;b>>=,a=a*a%c)if(b&)r=r*a%c;return r;}
struct Edge{int v,w,next;}; template<class T>
ostream & operator<<(ostream &out,vector<T>&v){
for(auto x:v)cout<<x<<' ';
return out;
}
void read(LL &n){
n=; char c=getchar();
while(c<''||c>'')c=getchar();
while(c>=''&&c<='') n=(n<<)+(n<<)+(c-''),c=getchar();
}
char e[][];
vector<int>g[];
bool vis[];
int bfs(int S,int T){
memset(vis,,sizeof(vis));
queue<pii>q;
q.push(mp(S,));
while(!q.empty()){
pii tmp=q.front();q.pop();
int u=tmp.fi;
if(u==T) {return +tmp.se;}
if(vis[u])continue;
vis[u]=;
for(auto v:g[u]){
q.push(mp(v,tmp.se+));
}
}
return -;
}
char centor(int x,int y){
return e[x+][y+];
}
int fx[][][]={{-,,,,,-,,,,-,,},{-,,,,-,-,-,,,-,,}};
int zb[][]={,,,,,-,,,,-,,};
char ok[]={'-','-','/','\\','\\','/'};
void AC(){
int r,c,i,j,k,u,v;
int S,T;
char str[];
scanf("%d%d",&r,&c);
getchar();
for(int i=;i<=*r+;++i){
gets(e[i]+);
}
for(int i=;i<=r*c;++i)g[i].clear();
for(int i=;i<=r;++i){
for(int j=;j<=c;++j){
int id=(i-)*c+j;
int x1=+(i-)*;
int y1=;
if(j%==) x1+=;
y1=y1+(j-)*;
if(centor(x1,y1)=='S') S=id;
else if (centor(x1,y1)=='T') T=id;
for(int o=;o<;++o){
int dx=i+fx[j%][o][];
int dy=j+fx[j%][o][];
if(dx<||dy<||dx>r||dy>c)continue;
int id2=(dx-)*c+dy;
//if(id==7&&id2==12)cout<<o<<endl;
/*if(id==2){
cout<<o<<' '<<x1<<' '<<y1<<' '<<x1+zb[o][0]<<' '
<<y1+zb[o][1]<<' '<<e[x1+zb[o][0]][y1+zb[o][1]]<<endl;
}*/
if(e[x1+zb[o][]][y1+zb[o][]]==ok[o])continue;
else {
g[id].pb(id2);
g[id2].pb(id);
//cout<<id<<' '<<id2<<endl;
}
}
}
} printf("%d\n",bfs(S,T));
}
int main(){
int T;
cin>>T;
while(T--)AC();
return ;
}
/*
1
3 4
+---+ +---+
/ \ / \
+ +---+ +---+
\ \
+ + S + + T +
/ /
+ + + + +
\ \
+ + + + +
/ /
+ + + + +
\ \
+---+ +---+ +
\ / \ /
+---+ +---+ */

icpc2018-焦作-F Honeycomb bfs的更多相关文章

  1. 2018ICPC焦作 F. Honeycomb /// BFS

    题目大意: 给定n m表示一共n行每行m个蜂巢 求从S到T的最短路径 input 1 3 4 +---+ +---+ / \ / \ + +---+ +---+ \ \ / \ + + S +---+ ...

  2. Codeforces gym 100685 F. Flood bfs

    F. FloodTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100685/problem/F Desc ...

  3. 焦作F Modular Production Line 费用流

    题目链接 题解:这道题比赛的时候,学弟说是网络流,当时看N这么大,觉得网络流没法做,实际本题通过巧妙的建图,然后离散化. 先说下建图方式,首先每个覆盖区域,只有左右端点,如果我们只用左右端点的话,最多 ...

  4. icpc2018焦作-I. Distance

    第一发又超时了... 题目大意:给你n个点,然后给你n-1的数,表示两两距离,然后让你输出n个答案,第i个答案表示从这n个点里面挑i个点,然后这i个点两两之间会有一个距离,答案要求这些距离和的最大值. ...

  5. ICPC 2018 焦作区域赛

    // 2019.10.7 练习赛 // 赛题来源:2018 ICPC 焦作区域赛 // CF链接:http://codeforces.com/gym/102028 A Xu Xiake in Hena ...

  6. ACM/ICPC 之 靠墙走-DFS+BFS(POJ3083)

    //POJ3083 //DFS求靠左墙(右墙)走的路径长+BFS求最短路 //Time:0Ms Memory:716K #include<iostream> #include<cst ...

  7. 1128. Partition into Groups(图着色bfs)

    1128 写的dfs貌似不太对 bfs重写 用bfs将图进行黑白染色 如果有超过一个与自己颜色相同的点 就把该点存入栈中 最后处理栈中的点 判断此点是否合法 不合法 取反 取反后再判断相邻点是否合法 ...

  8. USACO3.25Magic Squares(bfs)

    /* ID: shangca2 LANG: C++ TASK: msquare */ #include <iostream> #include<cstdio> #include ...

  9. HDU ACM 1495 非常可乐(广搜BFS)

    非常可乐 Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Total Submissi ...

随机推荐

  1. TestFlight 的使用记载

    TestFlight:     TestFlight内测网上很多资料.大概是先打包,然后在App Store Connect 里添加测试员的邮箱地址. Testflight.top:公测要用到test ...

  2. Fiddler修改图片显示

    培训课讲修改请求值,记录一下操作步骤: 步骤如下: 1. 点击人人网图片另存为到桌面 2. 打开fiddler,找到图片发送的请求(单击图片的链接,点击右边面板的Inspectors.查看ImageV ...

  3. 【LeetCode每天一题】Combinations(组合)

    Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. Example: I ...

  4. 一、iOS开发环境搭建

    前置条件 1. 必要:一台装有Mac OS X操作系统的电脑:经济允许的话可以买一部Mac book:否则的话,可以试试黑苹果或虚拟机. 2.必要:一个有可用的Apple ID:免费,在Apple的官 ...

  5. NOIP2017感想

    说实话,这次刚刚看到题目的时候真的有点懵.尤其是第一天的第一题,浪费了太多的时间,一开始天真的以为10的9次方,会爆long long.然后就特别傻的写一个高精度,总觉得自己有哪些细节方面处理的不到位 ...

  6. python3.x 读写文件要使用UTF8编码的话需要。。

    读写文件常遇到编码不正确的情况,都用UTF8读写文件就好了,在读写的时候加上编码格式:encoding='UTF-8'如下:with open(filename, 'r', encoding='UTF ...

  7. Kube-DNS搭建(1.4版本)

    目录贴:Kubernetes学习系列 1.介绍 之前介绍过DNS的搭建(基于Kubernetes集群部署skyDNS服务),但那个版本的DNS是随着Kubernetes1.2发布出来的,有点原始.本文 ...

  8. 用PHP实现反向代理服务器

    什么是反向代理: 百度百科有云: 反向代理(Reverse Proxy)方式是指以代理服务器来接受internet上的连接请求,然后将请求转发给内部网络上的服务器,并将从服务器上得到的结果返回给int ...

  9. TLS握手、中断恢复与证书中心的原因

    在双方都拿到随机数A.B.C后,将会使用这三个随机数生成一个对话密钥,然后使用该对话密钥进行对称加密通信,这种方式我们可以看到,安全性取决于随机数C的加密,前面的几个都是明文传的,这里就取决于服务器的 ...

  10. 基于ABP模块组件与依赖注入组件的项目插件开发

    注意,阅读本文,需要先阅读以下两篇文章,并且对依赖注入有一定的基础. 模块系统:http://www.cnblogs.com/mienreal/p/4537522.html 依赖注入:http://w ...