Abbott的复仇 Abbott's Revenge

原题链接

bfs的深度用法。这题最坑的我觉得是输入输出格式的处理(一不小心就容易格式错误)调了好几个小时.....

这里放一组udebug数据

SAMPLE
3 1 N 3 3
1 1 WL NR *
1 2 WL NR ER WF *
1 3 NL ER *
2 1 SL WR NF *
2 2 SL WF ELF *
2 3 SFR EL *
0
NOSOLUTION
3 1 N 3 2
1 1 WL NR *
1 2 NL ER *
2 1 SL WR NFR *
2 2 SR EL *
0
MyMaze1
3 1 N 1 1
0
MyMaze2
3 1 N 3 1
0
MyMaze3
3 1 N 2 1
0
MyMaze4
2 2 W 3 2
1 1 NR *
1 2 ER *
2 1 WR *
2 2 SF *
0
MyMaze5
2 2 N 2 3
1 1 WL *
1 2 NL *
2 1 SF *
2 2 NR *
3 1 SL *
3 2 EL *
0
RobertAbbottAtlanta
4 2 N 4 3
1 1 NR WL *
1 2 NLR WF EFR *
1 3 EFR WFR NL *
1 4 ER NL *
2 1 SFL WL NFR *
2 2 EL SFLR WFRL NFL *
2 3 EFLR SF NF WFRL *
2 4 SR ELR NF *
3 1 WR SL *
3 2 EFL SLR WR NF *
3 3 EFL SLR WL *
3 4 EL SR *
0
Circle
2 1 N 2 1
1 1 NR *
1 2 ER *
2 2 SF *
3 1 WR *
3 2 SR *
0
END

答案

SAMPLE
  (3,1) (2,1) (1,1) (1,2) (2,2) (2,3) (1,3) (1,2) (1,1) (2,1)
  (2,2) (1,2) (1,3) (2,3) (3,3)
NOSOLUTION
  No Solution Possible
MyMaze1
  No Solution Possible
MyMaze2
  No Solution Possible
MyMaze3
  (3,1) (2,1)
MyMaze4
  (2,2) (2,1) (1,1) (1,2) (2,2) (3,2)
MyMaze5
  (2,2) (1,2) (1,1) (2,1) (3,1) (3,2) (2,2) (2,3)
RobertAbbottAtlanta
  (4,2) (3,2) (2,2) (1,2) (1,3) (1,4) (2,4) (2,3) (2,2) (3,2)
  (3,1) (2,1) (1,1) (1,2) (2,2) (2,3) (2,4) (3,4) (3,3) (4,3)
Circle
  (2,1) (1,1) (1,2) (2,2) (3,2) (3,1) (2,1)

code

 

#include<bits/stdc++.h>
using namespace std;
struct Node{
    int dir[4][3];
    bool dir2[4][3];
};
Node a[10][10];
struct G{
    int fx,fy,from,Turn;
};
struct G2{
    int fx,fy;
};
int b[5]={-1,0,1,0,-1};
G Q[1005];
G2 Q2[1005];
void build(){
    for (int fi=1;fi<=9;fi++)
        for (int fj=1;fj<=9;fj++){
            memset(a[fi][fj].dir,0,sizeof(a[fi][fj].dir));
            memset(a[fi][fj].dir2,false,sizeof(a[fi][fj].dir2));
        }
}
int pd2(char turn1){
    if (turn1=='N') return 0;
    else if (turn1=='W') return 3;
    else if (turn1=='E') return 1;
    else return 2;
}
void build_turn(int fx,int fy,string turn1){
    int cnt=turn1.size();
    int x=pd2(turn1[0]);
    for (int i=1;i<cnt;i++){
        if (turn1[i]=='L') a[fx][fy].dir[x][0]=1;
        else if (turn1[i]=='R') a[fx][fy].dir[x][2]=1;
        else a[fx][fy].dir[x][1]=1;
    }
}
int main(){
//    freopen("input.txt","r",stdin);
//    freopen("output1.txt","w",stdout);
    ios::sync_with_stdio(false);
    string s,turn1;
    int turn;
    while (cin>>s && s!="END"){
        build();
        int fromx,fromy,tox,toy;
        cin>>fromx>>fromy>>turn1>>tox>>toy;
        turn=pd2(turn1[0]);
        int fx,fy;
        while (cin>>fx && fx!=0){
            cin>>fy;
            while (cin>>turn1 && turn1!="*"){
                build_turn(fx,fy,turn1);
            }
        }
        int l=1,r=0;
        bool bol=false;
        Q[r].fx=fromx;Q[r].fy=fromy;Q[r++].from=-1;
        Q[r].fx=fromx+b[turn];Q[r].fy=fromy+b[turn+1];Q[r].from=0;
        Q[r++].Turn=turn;
        while (l<r){
            int x=Q[l].fx,y=Q[l].fy;
            if (x==tox && y==toy){
                bol=true;
                break;
            }
            for (int i=0;i<3;i++){
                turn=Q[l].Turn;
                if (a[x][y].dir[turn][i] && !a[x][y].dir2[turn][i]){
                    a[x][y].dir2[turn][i]=true;
                    turn=(turn+i-1+4)%4;
                    Q[r].fx=x+b[turn];Q[r].fy=y+b[turn+1];Q[r].from=l;
                    Q[r++].Turn=turn;
                }
            }
//            printf("%d %d %d\n",x,y,turn);
            l++;
        }
        cout<<s<<"\n";
        int sum=0;
        if (!bol) cout<<"  No Solution Possible\n";
        else {
            int l2=0;
            for (;l!=-1;l=Q[l].from){
                Q2[l2].fx=Q[l].fx;
                Q2[l2++].fy=Q[l].fy;
            }
            l2--;
            for (;l2>=0;l2--){
                if (sum==0) cout<<" ";
                cout<<" ("<<Q2[l2].fx<<","<<Q2[l2].fy<<")";
                sum++;
                if (sum==10){
                    sum=0;
                    cout<<"\n";
                }
            }
        }
        if (sum!=0) cout<<endl;
    }
    return 0;
} 

 

Ps:一道非常适合锻炼bfs的题目

 

posted @ 2024-03-06 22:11  黑屿白  阅读(75)  评论(0)    收藏  举报