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 ...
随机推荐
- 判断是手机请求还是pc请求
网址 http://detectmobilebrowsers.com/ string u = Request.ServerVariables["HTTP_USER_AGENT"]; ...
- 访问GitLab的PostgreSQL数据库,查询、修改、替换等操作
1.登陆gitlab的安装服务查看配置文件 cat /var/opt/gitlab/gitlab-rails/etc/database.yml production: adapter: postgre ...
- Ajax取PHP JSON数据并显示
<!DOCTYPE html> <html lang="zh"> <head> <meta charset="UTF-8&quo ...
- 你不知道的JavaScript-2.词法作用域
考虑以下代码: function foo(a) { var b = a * 2; function bar(c) { console.log( a, b, c ); } bar( b * 3 ); } ...
- Centos配置tomcat服务并且开机自启动
把要配置成服务的tomcat文件夹中的catalina.sh脚本文件拷一份到/etc/init.d目录,并且改文件名称为tomcat6 cp /usr/web/tomcat/tomcat-/bin/c ...
- Mac下编译android4.0.4遇到的问题
这里总结下自己遇到的问题 jdk6安装,这个去苹果官网有 这里下载10.5的sdk https://github.com/phracker/MacOSX-SDKs/releases
- Python 操作 MySQL 的5种方式(转)
Python 操作 MySQL 的5种方式 不管你是做数据分析,还是网络爬虫,Web 开发.亦或是机器学习,你都离不开要和数据库打交道,而 MySQL 又是最流行的一种数据库,这篇文章介绍 Pytho ...
- cookies增删改擦操作
//判断是否存在名为aaa的cookie function hasSetCookie(name){ var strCookie = document.cookie; var arrCookie = s ...
- SLF4J bindings
see: https://www.slf4j.org/manual.html
- java线程学习之线程创建
线程是程序控制的一个内部数据流.线程的状态转化如下 或者 在java中创建线程有两种方式: 1.实现runnable接口(这个比较好,推荐这个.原因是:用的时候比较灵活,相比较继承Thread类,用接 ...