poj1101广搜

我的个神啊,怎么过都过不去,我都疯掉了!第一种是我觉得没有问题,但是怎么过不去的,烦请各位路过大神们帮忙看一下。第二种是一个特暴的广搜,都可以过去,无奈ing
#include <iostream>

using namespace std;
const int Pow2[4]={1,2,4,8};
struct State
{
    int r,c;
};
int w,h;
char board[78][78];
int x1,x2,y1,y2;
int ans,isSol;
int d[78][78];
int states[78][78];
State qu[6000];
int qs,qt;
int qly;
void bfs();
int Gen(int r,int c);
int ToUp(int r,int c);
int ToRight(int r,int c);
int ToDown(int r,int c);
int ToLeft(int r,int c);
int main()
{
    int bId,pId;
    int i,j;
    bId=0;
    while(cin>>w>>h)
    {
        if(w==0&&h==0)  break;
        cin.get();
        for(i=1;i<=h;i++)
            cin.getline(board[i]+1,78);
        for(i=0;i<w+2;i++)
            board[0][i]=board[h+1][i]=' ';
        for(i=0;i<h+2;i++)
        {
            board[i][0]=board[i][w+1]=' ';
            board[i][w+2]='\0';
        }
        bId++;
        cout<<"Board # "<<bId<<":\n";
        pId=0;
        while(cin>>x1>>y1>>x2>>y2)
        {
            if(x1==0&&y1==0&&x2==0&&y2==0)
                break;
            for(i=0;i<h+2;i++)
            {
                for(j=0;j<w+2;j++){
                 states[i][j]=0;
                 d[i][j]=-1;}
            }
            isSol=0;
            bfs();
            pId++;
            cout<<"Pair "<<pId<<": ";
            if(isSol==0)
                cout<<"impossible."<<endl;
            else
                cout<<ans<<" segments.\n";
        }
        cout<<endl;
    }
    return 0;
}
void bfs()
{
    int r,c,i,j;
    qu[0].r=y1;qu[0].c=x1;
    qs=0;qt=1;ans=0;
    r=qu[0].r;c=qu[0].c;qs++;
    if(ToUp(r,c)==1)
        return;
    if(ToRight(r,c)==1)
        return;
    if(ToDown(r,c)==1)
        return;
    if(ToLeft(r,c)==1)
        return;
    for(i=0;i<h+2;i++)
    {
        for(j=0;j<w+2;j++)
        {
            if(d[i][j]==-2) d[i][j]=-1;
        }
    }
    qly=qt-1;ans=1;
    while(qs<qt)
    {
        if(qs>qly)//开始新一层的拓展
        {
            ans++;
            qly=qt-1;
            for(i=0;i<h+2;i++)
            {
                for(j=0;j<w+2;j++)
                {
                    if(d[i][j]==-2) d[i][j]=-1;
                }
            }
        }
        r=qu[qs].r;c=qu[qs].c;qs++;
        if(Gen(r,c)==1)
        return;
    }
}
int Gen(int r,int c)
{
    if(d[r][c]==0)//水平方向拓展
    {
        if(ToLeft(r,c)==1)
            return 1;
        if(ToRight(r,c)==1)
            return 1;
    }
    if(d[r][c]==1)
    {
        if(ToUp(r,c)==1)
            return 1;
        if(ToDown(r,c)==1)
            return 1;
    }
    return 0;
}
int ToUp(int r,int c)
{
    int i=1;
    while(true)
    {
        if(r-i==y2&&c==x2)
        {
            ans++;isSol=1;
            return 1;
        }
        if(r-i>=0&&board[r-i][c]==' '&&(states[r-i][c]&Pow2[0])==0)
        {
            if(d[r-i][c]==-1)
            {
                qu[qt].r=r-i;qu[qt].c=c;qt++;
                states[r-i][c]=Pow2[0];d[r-i][c]=0;
            }
            else
            d[r-i][c]=-2;
        }
        else
        break;
        i++;
    }
    return 0;
}
int ToDown(int r,int c)
{
    int i=1;
    while(true)
    {
        if(r+i==y2&&c==x2)
        {
            ans++;isSol=1;
            return 1;
        }
        if(r+i<h+2&&board[r+i][c]==' '&&(states[r+i][c]&Pow2[2])==0)
        {
            if(d[r+i][c]==-1)
            {
                qu[qt].r=r+i;qu[qt].c=c;qt++;
                states[r+i][c]=Pow2[2];d[r+i][c]=0;
            }
            else
            d[r+i][c]=-2;
        }
        else
        break;
        i++;
    }
    return 0;
}
int ToRight(int r,int c)
{
    int i=1;
    while(true)
    {
        if(r==y2&&c+i==x2)
        {
            ans++;isSol=1;
            return 1;
        }
        if(c+i<w+2&&board[r][c+i]==' '&&(states[r][c+i]&Pow2[1])==0)
        {
            if(d[r][c+i]==-1)
            {
                qu[qt].r=r;qu[qt].c=c+i;qt++;
                states[r][c+i]=Pow2[1];d[r][c+i]=1;
            }
            else
            d[r][c+i]=-2;
        }
        else
        break;
        i++;
    }
    return 0;
}
int ToLeft(int r,int c)
{
    int i=1;
    while(true)
    {
        if(r==y2&&c-i==x2)
        {
            ans++;isSol=1;
            return 1;
        }
        if(r-i>=0&&board[r][c-i]==' '&&(states[r][c-i]&Pow2[3])==0)
        {
            if(d[r][c-i]==-1)
            {
                qu[qt].r=r;qu[qt].c=c-i;qt++;
                states[r][c-i]=Pow2[3];d[r][c-i]=1;
            }
            else
            d[r][c-i]=-2;
        }
        else
        break;
        i++;
    }
    return 0;
}

#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
const int N=80;
struct node
{
    int x;
    int y;
    int time;
}q[N*N];
int dir[4][2]={{-1,0},{1,0},{0,-1},{0,1}};
int vis[N][N];
char mapp[N][N];
int w,h;
int sx,sy,ex,ey;
int bfs()
{
    int f=0,r=0,x,y;
    int tx,ty,ti,i;
    q[r].x=sx;q[r].y=sy;q[r].time=0;
    r++;
    while(f<r)
    {
        x=q[f].x;y=q[f].y;
        ti=q[f].time;ti++;f++;
        for(i=0;i<4;i++)
        {
            tx=x+dir[i][0];
            ty=y+dir[i][1];
            if(tx<0||tx>h+1||ty<0||ty>w+1)
                continue;
            while(1)
            {
                if(tx==ex&&ty==ey)
                    {return ti;}
                if(mapp[tx][ty]=='X'||tx<0||tx>h+1||ty<0||ty>w+1)
                    break;
                if(!vis[tx][ty])
                {
                    vis[tx][ty]=1;
                    q[r].x=tx;
                    q[r].y=ty;
                    q[r].time=ti;
                    r++;
                }
                tx+=dir[i][0];ty+=dir[i][1];
            }
        }
    }
    return -1;
}
int main()
{
    int i,j,ans,board=0,pair;
    while(scanf("%d%d",&w,&h)!=EOF)
    {
        if(!w&&!h)  break;
        getchar();pair=0;
        memset(mapp,0,sizeof(mapp));
        printf("Board #%d:\n",++board);
        for(i=1;i<=h;i++)
        {
            for(j=1;j<=w;j++)
                scanf("%c",&mapp[i][j]);
                getchar();
        }
        while(scanf("%d%d%d%d",&sy,&sx,&ey,&ex)!=EOF)
        {
            if(!sy&&!sx&&!ey&&!ex)  break;
            memset(vis,0,sizeof(vis));
            ans=bfs();
            printf("Pair %d: ",++pair);
            if(ans==-1) puts("impossible.");
            else    printf("%d segments.\n",ans);
        }
        cout<<endl;
    }
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值