ACM-ICPC Jiaozuo Onsite 2018 F. Honeycomb /*bfs,建图,构造*/

不堪回首第一次区域赛(真哭JJ)

真心态爆炸一场(密码给错了十分钟 不能复制粘贴case linux用到哭 签到签不出来)

所以试了一下文件读入操作。。

题目真不是很难,就是建图稍微要思路清楚一点。。。

#include<bits/stdc++.h>
using namespace std;
#define FSCI(i,l,r,a) for(int i=l;i<=r;i++) scanf("%d",&a[i])
#define SCI(a) scanf("%d",&a)
#define SCS(a) scanf("%s",a)
#define LL long long
#define mod 1000000007
#define N 1005
/***************************************************************************************************************************************************/
char mp[N*5][N*7];
vector<int>v[N*N];
int r,c;
int S,E;
int ans=INT_MAX;
int dp[N*N];
//ifstream in("C:\\Users\\RSHS\\Desktop\\input case.txt");
struct no{
        int index;
        int step;
        no(){}; //overwrite
        no(int a,int b):index(a),step(b){} //overwrite
}node;

void readmp(){
    cin>>r>>c;
    getchar();
    for(int i=0;i<4*r+3;i++)
        cin.getline(mp[i],7000);
    /*in>>r>>c;
    in.getline(mp[0],7000); //瞎jier吸收回车
    for(int i=0;i<4*r+3;i++){
        in.getline(mp[i],7000);
    }
    for(int i=0;i<4*r+3;i++){
        for(int j=0;j<strlen(mp[i]);j++)
            cout<<mp[i][j];
            cout<<endl;
    }
    cout<<mp[4][10];*/
}
void ST(int h,int l,int index){
    if(mp[h][l]=='S') S=index;
    if(mp[h][l]=='T') E=index;
}
void isarc1(int h,int l,int index){
    if(mp[h-2][l]!='-') {
        v[index].push_back(index-1);
    }
    if(mp[h+2][l]!='-') {
        v[index].push_back(index+1);
    }
    if(mp[h-1][l-3]!='/'){
        v[index].push_back(index-r-1);
    }
    if(mp[h+1][l+3]!='/'){
        v[index].push_back(index+r);
    }
    if(mp[h-1][l+3]!='\\'){
        v[index].push_back(index+r-1);
    }
    if(mp[h+1][l-3]!='\\'){
        v[index].push_back(index-r);
    }
}
void isarc2(int h,int l,int index){
    if(mp[h-2][l]!='-') {
        v[index].push_back(index-1);
    }
    if(mp[h+2][l]!='-') {
        v[index].push_back(index+1);
    }
    if(mp[h-1][l-3]!='/'){
        v[index].push_back(index-r);
    }
    if(mp[h+1][l+3]!='/'){
        v[index].push_back(index+r+1);
    }
    if(mp[h-1][l+3]!='\\'){
        v[index].push_back(index+r);
    }
    if(mp[h+1][l-3]!='\\'){
        v[index].push_back(index-r+1);
    }
}
void initv(){
    for(int i=0;i<=r*c+1;i++)
        v[i].clear();
    int index=0;
    for(int i=1;i<=c;i++){
        for(int j=1;j<=r;j++){
            if(i%2){
                int l=6*(i-1)+5-1;
                int h=4*(j-1)+3-1;
                index++;
                ST(h,l,index);
                isarc1(h,l,index);
            }
            else{
                int l=6*(i-1)+5-1;
                int h=4*(j-1)+5-1;
                index++;
                ST(h,l,index);
                isarc2(h,l,index);
            }
        }
    }
}
void bfs(){
    for(int i=0;i<=r*c+1;i++){
        dp[i]=INT_MAX;
    }
    queue<no>q;
    q.push({S,1});dp[S]=1;
    while(!q.empty()){
        int nowi=q.front().index ,nows=q.front().step;
        if(nowi==E) {ans=min(ans,nows);}
        q.pop();
        for(int i=0;i<(int)v[nowi].size();i++){
            if(nows+1<dp[v[nowi][i]]){
                q.push({v[nowi][i],nows+1});
                dp[v[nowi][i]]=nows+1;
            }
        }
    }
}
int main(){

	int T;//in>>T;
	cin>>T;
	while(T--){
        ans=INT_MAX;
		readmp();
        initv();
        bfs();
        if(ans!=INT_MAX)
            cout<<ans<<endl;
        else {
            cout<<-1<<endl;
        }
	}
    return 0;
}

 

突然可以直接next数组在char数组上bfs。可以减少代码量,算了都一样。

 

想说些什么,还是不奶自己了。

菜就是菜。

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值