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 ...
随机推荐
- P5280 [ZJOI2019]线段树
题目链接:洛谷 题目描述:[比较复杂,建议看原题] 这道题太神仙了,线段树上做树形dp. 根据树形dp的套路,都是按照转移的不同情况给节点分类.这里每次modify的时候对于节点的影响也不同,所以我们 ...
- [js]浏览器同源策略(same-origin policy)
浏览器的同源策略 浏览器同源政策及其规避方法 什么是同源策略 A网页设置的 Cookie,B网页不能打开,除非这两个网页"同源".所谓"同源"指的是" ...
- git 如何删除已经add的文件
git rm --cached "文件" 参考: https://blog.csdn.net/kiss_the_sky/article/details/77921206
- jenkins配置工程目录-启动case
1.我们在python里面编辑的脚本可以正常跑,但是在cmd里面跑就不行了,找不到自己定义的方法模块,这个时候我们要搞个环境变量 name : PYTHONPATH val : 工程目录路劲 ...
- Spring Boot 全局异常捕获
import javax.servlet.http.HttpServletRequest; import org.springframework.web.bind.annotation.Control ...
- Poj3176 Cow Bowling (动态规划 数字三角形)
Description The cows don't use actual bowling balls when they go bowling. They each take a number (i ...
- Python基础(三)文件操作
[对文件进行循环操作] fw = open('nhy','w') for line in fw: print('line:',line) #直接循环文件对象,每次循环的时候就是取每一行的数据 fw ...
- axios的封装
function axios(options){ var promise = new Promise((resolve,reject)=>{ var xhr = null; if(window. ...
- mac 遇到的奇怪问题?
1: 卸载 xcode,发现git报错了. mac git xcrun error active developer path 错误 解决办法:sudo xcode-select -switch / ...
- [c/c++] programming之路(24)、字符串(五)——字符串插入,字符串转整数,删除字符,密码验证,注意事项
1.将字符串插入到某位置(原字符串“hello yincheng hello cpp hello linux”,查找cpp,找到后在cpp的后面插入字符串“hello c”) 需要用到strstr字符 ...