Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

bfs 写了127 要疯拉!!!!

Posted by Yt_zp at 2017-03-29 19:33:26 on Problem 3083
#include <iostream>
#include <cstdio>
#include <queue>
#include <cstring>
using namespace std;
char mapp[50][50];
bool vis[50][50];
bool visr[50][50][4];
bool visl[50][50][4];
int sx,sy,loop,n,m;
int dx[]={1,0,-1,0};
int dy[]={0,1,0,-1};
int dxr[]={-1,0,1,0};
int dyr[]={0,-1,0,1};
int dxl[]={-1,0,1,0};
int dyl[]={0,1,0,-1};
struct N{
    int x,y;
    int st;
    int from;///0--向上 1--向左  2--向下 3--向右
    int to;/// 0--向上  1--向右  2--向下 3--向左
};
int main(){
    scanf("%d",&loop);
    while(loop--){
        int ans=0;
        scanf("%d%d",&m,&n);
        for(int i=1;i<=n;++i)
            for(int j=1;j<=m;++j){
                cin>>mapp[i][j];
                if(mapp[i][j]=='S'){
                    sx=i;
                    sy=j;
                }
            }
        memset(visl,0,sizeof(visl));
        queue<N>q;
        N now,next;
        now.x=sx;
        now.y=sy;
        now.st=1;
        now.to=0;
        q.push(now);
        visl[sx][sy][0]=true;
        while(!q.empty()){
            now=q.front();
            q.pop();
            if(mapp[now.x][now.y]=='E'){
                ans=now.st;
                break;
            }
            int be=(now.to-1+4)%4;
            for(int i=0;i<4;++i){
                int nx,ny,k;
                k=(be+i)%4;
                nx=next.x=now.x+dxl[k];
                ny=next.y=now.y+dyl[k];
                next.st=now.st+1;
                next.to=k;
                if(nx>0&&ny>0&&nx<=n&&ny<=m&&!visl[nx][ny][k]&&(mapp[nx][ny]=='.'||mapp[nx][ny]=='E')){
                    q.push(next);
                    visl[nx][ny][k]=1;
                    break;
                }
            }
        }
        cout<<ans<<' ';
        memset(visr,0,sizeof(visr));
        while(!q.empty())q.pop();
        now.x=sx;
        now.y=sy;
        now.st=1;
        now.from=0;
        q.push(now);
        visr[sx][sy][0]=true;
        while(!q.empty()){
            now=q.front();
            q.pop();
            if(mapp[now.x][now.y]=='E'){
                ans=now.st;
                break;
            }
            int be=(now.from-1+4)%4;
            for(int i=0;i<4;++i){
                int nx,ny,k;
                k=(be+i)%4;
                nx=next.x=now.x+dxr[k];
                ny=next.y=now.y+dyr[k];
                next.st=now.st+1;
                next.from=k;
                if(nx>0&&ny>0&&nx<=n&&ny<=m&&!visr[nx][ny][k]&&(mapp[nx][ny]=='.'||mapp[nx][ny]=='E')){
                    q.push(next);
                    visr[nx][ny][k]=1;
                    break;
                }
            }
        }
        cout<<ans<<' ';
        memset(vis,0,sizeof(vis));
        while(!q.empty())q.pop();
        now.x=sx;
        now.y=sy;
        now.st=1;
        q.push(now);
        vis[sx][sy]=true;
        while(!q.empty()){
            now=q.front();
            q.pop();
            if(mapp[now.x][now.y]=='E'){
                ans=now.st;
                break;
            }
            for(int i=0;i<4;++i){
                int nx,ny;
                nx=next.x=now.x+dx[i];
                ny=next.y=now.y+dy[i];
                next.st=now.st+1;
                if(nx>0&&ny>0&&nx<=n&&ny<=m&&!vis[nx][ny]&&(mapp[nx][ny]=='.'||mapp[nx][ny]=='E')){
                    q.push(next);
                    vis[nx][ny]=1;
                }
            }
        }
        cout<<ans<<'\12';
    }
    return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator