为什么输出超限
查看原帖
为什么输出超限
1231673
NuclearBeet楼主2024/4/12 13:41
/*I lv ya*/
#include <bits/stdc++.h>
using namespace std;
int w,h,tim=0;//w为行数,h为列数
int a,b,c,d,ni,nj,ans,f;
char ch;
int mx[4]={1,0,0,-1},
    my[4]={0,1,-1,0};
bool mp[80][80];
bool vis[80][80];
struct po
{
    int x,y,st,drkt;
};
bool check(int x,int y)
{
    if(x<0||x>h+1||y<0||y>w+1||vis[x][y]||mp[x][y])
        return 0;
    return 1;
}
void bfs(int t,int x,int y)
{
    queue <po> q;
    vis[x][y]=1;
    q.push((po){x,y,0,4});
    while(q.size())
    {
        int xx=q.front().x;int m=q.front().drkt;
        int yy=q.front().y;int s=q.front().st;
        q.pop();
        for(int i=0;i<4;i++)
        {
            int xxx=xx+mx[i];
            int yyy=yy+my[i];
            if(xxx==d&&yyy==c)
            {
                int ss=(i!=m)?s+1:s;
                //cout<<"y:"<<xxx<<' '<<yyy<<endl<<d<<' '<<c<<endl;
                f=1;
                ans=min(ans,ss);
                continue;
            }
            if(check(xxx,yyy))
            {
                //cout<<xxx<<' '<<yyy<<endl;
                int ss=(i!=m)?s+1:s;
                //cout<<s<<endl;
                vis[xxx][yyy]=1;
                q.push((po){xxx,yyy,ss,i});
            }
        }
    }
    if(f)
    {
        printf("Pair %d: %d segments.",t,ans);
        cout<<endl;
    }
    else
    {
        printf("Pair %d: impossible.",t);
        cout<<endl;
    }
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(NULL);cin.tie(NULL);

    while(cin>>w>>h,w)
    {
        int t=0;
        memset(mp,0,sizeof(mp));
        cout<<"Board #:"<<++tim<<endl;
		for(int i=1; i<=h; i++)
        {
            for(int j=1; j<=w; j++)
            {
                char ch = getchar();
                while(ch != ' ' && ch != 'X') {
                    ch = getchar();
                }
                if(ch=='X') mp[i][j]=1;
            }
        }
/*
        for(int i=1; i<=h; i++)
        {
            for(int j=1; j<=w; j++)
            {
                cout<<mp[i][j];
            }
            cout<<endl;
        }
*/
        while(cin>>a>>b>>c>>d,a)
        {
            //cout<<"----------"<<endl<<a<<b<<c<<d<<endl;
            memset(vis,0,sizeof(vis));
            ans=0x3f3f3f3f;f=0;
            bfs(++t,b,a);
        }
        cout<<endl;
    }
}

2024/4/12 13:41
加载中...