icpc2018-焦作-F Honeycomb bfs

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

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

  1 #include<iostream>
  2 #include<cstdio>
  3 #include<cstring>
  4 #include<map>
  5 #include<set>
  6 #include<stack>
  7 #include<deque>
  8 #include<bitset>
  9 #include<unordered_map>
 10 #include<unordered_set>
 11 #include<queue>
 12 #include<cstdlib>
 13 #include<ctype.h>
 14 #include<ctime>
 15 #include<functional>
 16 #include<algorithm>
 17 #include<bits/stdc++.h>
 18 using namespace std;
 19 #define LL long long 
 20 #define pii pair<int,int>
 21 #define mp make_pair
 22 #define pb push_back
 23 #define fi first
 24 #define se second
 25 #define inf 0x3f3f3f3f
 26 #define debug puts("debug")
 27 #define mid ((L+R)>>1)
 28 #define lc (id<<1)
 29 #define rc (id<<1|1)
 30 const int maxn=10010;
 31 const int maxm=50050;
 32 const double PI=acos(-1.0);
 33 const double eps=1e-6;
 34 const LL mod=1e9+7;
 35 LL gcd(LL a,LL b){return b==0?a:gcd(b,a%b);}
 36 LL lcm(LL a,LL b){return a/gcd(a,b)*b;}
 37 LL qpow(LL a,LL b,LL c){LL r=1; for(;b;b>>=1,a=a*a%c)if(b&1)r=r*a%c;return r;}
 38 struct Edge{int v,w,next;};
 39 
 40 template<class T>
 41 ostream & operator<<(ostream &out,vector<T>&v){
 42     for(auto x:v)cout<<x<<' ';
 43     return out;
 44 }
 45 void read(LL &n){
 46     n=0; char c=getchar();
 47     while(c<'0'||c>'9')c=getchar();
 48     while(c>='0'&&c<='9') n=(n<<3)+(n<<1)+(c-'0'),c=getchar();
 49 }
 50 char e[6010][6100];
 51 vector<int>g[1000100];
 52 bool vis[1010000];
 53 int bfs(int S,int T){
 54     memset(vis,0,sizeof(vis));
 55     queue<pii>q;
 56     q.push(mp(S,0));
 57     while(!q.empty()){
 58         pii tmp=q.front();q.pop();
 59         int u=tmp.fi;
 60         if(u==T) {return 1+tmp.se;}
 61         if(vis[u])continue;
 62         vis[u]=1;
 63         for(auto v:g[u]){
 64             q.push(mp(v,tmp.se+1));
 65             }
 66     }
 67     return -1;
 68 }
 69 char centor(int x,int y){
 70     return e[x+2][y+2];
 71 }
 72 int fx[2][6][2]={{-1,0,1,0,0,-1,0,1,1,-1,1,1},{-1,0,1,0,-1,-1,-1,1,0,-1,0,1}};
 73 int zb[6][2]={0,2,4,2,1,-1,1,5,3,-1,3,5};
 74 char ok[10]={'-','-','/','\\','\\','/'};
 75 void AC(){
 76     int r,c,i,j,k,u,v;
 77     int S,T;
 78     char str[1000];
 79     scanf("%d%d",&r,&c); 
 80     getchar();
 81     for(int i=1;i<=4*r+3;++i){
 82     gets(e[i]+1);
 83     }
 84     for(int i=1;i<=r*c;++i)g[i].clear();
 85     for(int i=1;i<=r;++i){
 86         for(int j=1;j<=c;++j){
 87             int id=(i-1)*c+j;
 88             int x1=1+(i-1)*4;
 89             int y1=3;
 90             if(j%2==0) x1+=2;
 91             y1=y1+(j-1)*6;
 92             if(centor(x1,y1)=='S') S=id;
 93             else if (centor(x1,y1)=='T') T=id;
 94             for(int o=0;o<6;++o){
 95                 int dx=i+fx[j%2][o][0];
 96                 int dy=j+fx[j%2][o][1];
 97                 if(dx<1||dy<1||dx>r||dy>c)continue;
 98                 int id2=(dx-1)*c+dy;
 99                 //if(id==7&&id2==12)cout<<o<<endl;
100                 /*if(id==2){
101                     cout<<o<<' '<<x1<<' '<<y1<<' '<<x1+zb[o][0]<<' '
102                     <<y1+zb[o][1]<<' '<<e[x1+zb[o][0]][y1+zb[o][1]]<<endl;
103                 }*/
104                 if(e[x1+zb[o][0]][y1+zb[o][1]]==ok[o])continue;
105                 else {
106                     g[id].pb(id2);
107                     g[id2].pb(id);
108                     //cout<<id<<' '<<id2<<endl;
109                 }
110             }
111         }
112     }
113     
114     printf("%d\n",bfs(S,T));
115 }
116 int main(){
117     int T;
118     cin>>T;
119     while(T--)AC();
120     return 0;
121 }
122 /*
123 1
124 3 4
125   +---+       +---+
126  /     \     /     \
127 +       +---+       +---+
128  \                       \
129   +   +   S   +   +   T   +
130  /                       /
131 +       +   +       +   +
132  \                       \
133   +   +       +   +       +
134  /                       /
135 +       +   +       +   +
136  \                       \
137   +---+       +---+       +
138        \     /     \     /
139         +---+       +---+
140         
141         
142 */

 

转载于:https://www.cnblogs.com/zzqc/p/10092639.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值