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. xutils android studio引用问题

    然后rebuild--->关闭项目-->重启,ok public class MyApplication extends Application { @Override public vo ...

  2. Linux命令:在线练习地址

    1.https://www.tutorialspoint.com/unix_terminal_online.php 2.https://www.tutorialspoint.com/index.htm ...

  3. form提交循环

    使用ajax提交数据时,如果数据非常多,提交会比较麻烦,这时可以给form表一个id=“form”,用serializeArray()方法进行提交. erializeArray()方法通过序列化表单值 ...

  4. aws cloudwatch监控怎么通过钉钉机器人报警

    最近在完善海外业务在aws服务的CloudWatchh监控,发现CloudWatch报警通知要通过aws的sns服务,直接支持的通道有短信和邮件,但是我们想推到钉钉群里面的群机器人里面这个就要借助aw ...

  5. 实验隐藏参数"_allow_resetlogs_corruption"的使用

    实验环境:OEL 5.7 + Oracle 10.2.0.5 Tips:该参数仅在特殊恢复场景下使用,需要在专业Oracle工程师指导下进行操作. 1.隐藏参数说明 2.故障场景再现 3.非常规恢复 ...

  6. windows下cmd无法使用telnet命令解决方法 (原)

    telnet不是内部或者外部命令,也不是可运行程序-----解决办法: (win7为例) 控制面板 --- > 程序(小图标下直接到[程序和功能]) --- >程序和功能 --- > ...

  7. egg.js基础入门

    之前一直使用koa, 刚刚接触egg, 做了一些入门的笔记 准备工作 1  首先安装脚手架,,并创建项目. $ npm i egg-init -g $ egg-init egg-demo --type ...

  8. ExecuteNonQuery方法、ExecuteScalar方法、ExecuteReader方法的区别

    ----ExecuteNonQuery():执行命令对象的SQL语句,返回一个int类型变量,如果SQL语句是对数据库的记录进行操作(如记录的增加.删除和更新),那么方法将返回操作所影响的记录条数.- ...

  9. React高级教程(es6)——(1)JSX语法深入理解

    从根本上来说,JSX语法提供了一种创建React元素的语法糖,JSX语句可以编译成: React.createElement(component, props, …children)的形式,比如: & ...

  10. 8、Spring-Kafka Recving Messages

    Record Listeners The @KafkaListener annotation provides a mechanism for simple POJO listeners. The f ...