icpc2018-焦作-F Honeycomb bfs
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的更多相关文章
- 2018ICPC焦作 F. Honeycomb /// BFS
题目大意: 给定n m表示一共n行每行m个蜂巢 求从S到T的最短路径 input 1 3 4 +---+ +---+ / \ / \ + +---+ +---+ \ \ / \ + + S +---+ ...
- Codeforces gym 100685 F. Flood bfs
F. FloodTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100685/problem/F Desc ...
- 焦作F Modular Production Line 费用流
题目链接 题解:这道题比赛的时候,学弟说是网络流,当时看N这么大,觉得网络流没法做,实际本题通过巧妙的建图,然后离散化. 先说下建图方式,首先每个覆盖区域,只有左右端点,如果我们只用左右端点的话,最多 ...
- icpc2018焦作-I. Distance
第一发又超时了... 题目大意:给你n个点,然后给你n-1的数,表示两两距离,然后让你输出n个答案,第i个答案表示从这n个点里面挑i个点,然后这i个点两两之间会有一个距离,答案要求这些距离和的最大值. ...
- ICPC 2018 焦作区域赛
// 2019.10.7 练习赛 // 赛题来源:2018 ICPC 焦作区域赛 // CF链接:http://codeforces.com/gym/102028 A Xu Xiake in Hena ...
- ACM/ICPC 之 靠墙走-DFS+BFS(POJ3083)
//POJ3083 //DFS求靠左墙(右墙)走的路径长+BFS求最短路 //Time:0Ms Memory:716K #include<iostream> #include<cst ...
- 1128. Partition into Groups(图着色bfs)
1128 写的dfs貌似不太对 bfs重写 用bfs将图进行黑白染色 如果有超过一个与自己颜色相同的点 就把该点存入栈中 最后处理栈中的点 判断此点是否合法 不合法 取反 取反后再判断相邻点是否合法 ...
- USACO3.25Magic Squares(bfs)
/* ID: shangca2 LANG: C++ TASK: msquare */ #include <iostream> #include<cstdio> #include ...
- HDU ACM 1495 非常可乐(广搜BFS)
非常可乐 Time Limit : 2000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other) Total Submissi ...
随机推荐
- python实现获取身份证号码的方法
记录瞬间 1.号码的结构 公民身份号码是特征组合码,由十七位数字本体码和一位校验码组成.排列顺序从左至右依次为:六位数字地址码,八位数字出生日期码,三位数字顺序码和一位数字校验码.2.地址码 表示编码 ...
- git之概念图
1.git四大区. . 2. 3. 4.
- zw量化交易·实盘操作·系列培训班
参见: <zw量化交易·实盘操作·系列培训班> http://blog.sina.com.cn/s/blog_7100d4220102w0q5.html
- Quartz Scheduler misfireThreshold属性的意义与触发器超时后的处理策略
Quartz misfireThreshold属性的意义与触发器超时后的处理策略. 在配置quartz.properties有这么一个属性就是misfireThreshold,用来指定调度引擎设置触发 ...
- is 和 == 以及 编码和解码
1.is 比较的是内存地址 a="name" b="snow" print(a is b) # False id() 获取内存地址 a=" == ...
- VMware vSphere 5.x 与 vSphere 6.0各版本功能特性对比
各版本中的新特性及功能对比: VMware vSphere 5.0 VMware vSphere 5.1 VMware vSphere 5.5 VMware vSphere 6.0 ESXi 5. ...
- 【数据使用】3k水稻数据库现成SNP的使用
---恢复内容开始--- 我们经常说幻想着使用已有数据发表高分文章,的确,这样的童话故事每天都在发生,但如何走出第一步我们很多小伙伴不清楚,那么我们就从水稻SNP数据库的使用来讲起. http://s ...
- Java 创建文本内容
Java 创建文本内容 import java.io.FileWriter; import java.io.IOException; public class TestFile { public st ...
- 剑指offer(13)调整数组顺序使奇数位于偶数前面
题目描述 输入一个整数数组,实现一个函数来调整该数组中数字的顺序,使得所有的奇数位于数组的前半部分,所有的偶数位于位于数组的后半部分,并保证奇数和奇数,偶数和偶数之间的相对位置不变. 题目分析 判断是 ...
- 剑指offer(40)数组中只出现一次的数字
题目描述 一个整型数组里除了两个数字之外,其他的数字都出现了两次.请写程序找出这两个只出现一次的数字. 题目分析 第一种方法:使用js中的indexOf()和lastIndexOf(),只要两个相等, ...