zoukankan      html  css  js  c++  java
  • uva 816 BFS求最短路的经典问题……

    一开始情况没有考虑周全,直接WA掉了,

    然后用fgets()出现了WA,给改成scanf就AC了

    题目不是很难,用心就好……

    #include <iostream>
    #include <cstdio>
    #include <cmath>
    #include <cstring>
    #include <algorithm>
    #include <cstdlib>
    #include <stack>
    #include <cctype>
    #include <string>
    #include <malloc.h>
    #include <queue>
    #include <map>
    
    using namespace std;
    const int INF = 0xffffff;
    const double Pi = 4 * atan(1);
    const int Maxn = 200 + 10;
    //int dir2[8][2] = {{-1,0},{0,-1},{-1,1},{1,-1},{-1,-1},{1,0},{0,1},{1,1}};
    int dr[] = {-1,0,1,0};
    int dc[] = {0,1,0,-1};
    
    struct Node{
        int r;
        int c;
        int dir;
        Node(int rr,int cc,int dd){
            r = rr;
            c = cc;
            dir = dd;
        }
        Node(){}
    };
    const char * dirs = "NESW";
    const char * turns = "FLR";
    bool has_edge[10][10][4][4];
    int d[10][10][4];
    int r1,c1,dir;
    int r0,c0;
    int r2,c2;
    bool flag;
    Node p[10][10][4];
    
    int dir_id(char ch){
        return strchr(dirs,ch) - dirs;
    }
    int turn_id(char ch){
        return strchr(turns,ch) - turns;
    }
    
    Node walk(Node & u,int turn){
        int dd = u.dir;
        if(turn == 1){
            dd = (dd + 3) % 4;
        }
        else if(turn == 2){
            dd = (dd + 1) % 4;
        }
        return Node(u.r + dr[dd],u.c + dc[dd],dd);
    }
    
    void print_ans(Node u){
        vector<Node>nodes;
        while(1){
            nodes.push_back(u);
            if(d[u.r][u.c][u.dir] == 0)
                break;
            u = p[u.r][u.c][u.dir];
        }
        nodes.push_back(Node(r0,c0,dir));
        int cnt = 0;
        for(int i = nodes.size()-1;i >= 0;i--){
            if(cnt % 10 == 0)
                cout << " ";
            cout << " (" << nodes[i].r << "," << nodes[i].c << ")";
            if(++cnt % 10 == 0)
                cout << endl;
        }
        if(nodes.size() % 10 != 0)
            cout << endl;
    }
    
    bool inside(int r,int c){
        if(r > 0 && r < 10 && c > 0 && c < 10)
            return true;
        return false;
    }
    
    void solve(){
        queue<Node>q;
        memset(d,-1,sizeof(d));
        memset(p,0,sizeof(p));
        Node u(r1,c1,dir);
        d[u.r][u.c][u.dir] = 0;
        q.push(u);
        while(!q.empty()){
            Node u = q.front();
            q.pop();
            if(u.r == r2 && u.c == c2){
                flag = 0;
                print_ans(u);
                return;
            }
            for(int i = 0;i < 3;i++){
                Node v = walk(u,i);
                if(has_edge[u.r][u.c][u.dir][i] && inside(v.r,v.c) && d[v.r][v.c][v.dir] < 0){
                    d[v.r][v.c][v.dir] = d[u.r][u.c][u.dir] + 1;
                    p[v.r][v.c][v.dir] = u;
                    q.push(v);
                }
            }
        }
    }
    
    int main()
    {
    #ifndef ONLINE_JUDGE
        freopen("inpt.txt","r",stdin);
    #endif
            char str[30];
            while(~scanf("%s",str) && strcmp(str,"END")){
            printf("%s
    ",str);
            flag = 1;
            memset(has_edge,0,sizeof(has_edge));
            char ch;
            cin >> r0 >> c0 >> ch >> r2 >> c2;
            dir = dir_id(ch);
            r1 = r0 + dr[dir];
            c1 = c0 + dc[dir];
            int r,c;
            char str2[30];
    
            while(cin >> r){
                if(r == 0){
                    break;
                }
                cin >> c;
                while(cin >> str2){
                    if(str2[0] == '*')
                        break;
                    int dirID = dir_id(str2[0]);
                    int len = strlen(str2);
                    for(int i = 1;i < len;i++){
                        int turnID = turn_id(str2[i]);
                        has_edge[r][c][dirID][turnID] = 1;
                    }
                }
            }
            solve();
            if(flag){
                cout << "  No Solution Possible" << endl;
            }
            getchar();
        }
        return 0;
    }
    View Code

     测试数据:

    SAMPLE
    3 1 N 3 3
    1 1 WL NR *
    1 2 WLF NR ER *
    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
    MyMaze 1
    3 1 N 1 1
    0
    MyMaze 2
    3 1 N 3 1
    0
    MyMaze 3
    3 1 N 2 1
    0
    MyMaze 4
    2 2 W 3 2
    1 1 NR *
    1 2 ER *
    2 1 WR *
    2 2 SF *
    0
    MyMaze 5
    2 2 N 2 3
    1 1 WL *
    1 2 NL *
    2 1 SF *
    2 2 NR *
    3 1 SL *
    3 2 EL *
    0
    Circle
    2 1 N 2 1
    1 1 NR *
    1 2 ER *
    2 2 SF *
    3 1 WR *
    3 2 SR *
    0
    Robert Abbott's Atlanta Maze
    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
    END
    View Code
    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
    MyMaze 1
      No Solution Possible
    MyMaze 2
      No Solution Possible
    MyMaze 3
      (3,1) (2,1)
    MyMaze 4
      (2,2) (2,1) (1,1) (1,2) (2,2) (3,2)
    MyMaze 5
      (2,2) (1,2) (1,1) (2,1) (3,1) (3,2) (2,2) (2,3)
    Circle
      (2,1) (1,1) (1,2) (2,2) (3,2) (3,1) (2,1)
    Robert Abbott's Atlanta Maze
      (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)
  • 相关阅读:
    Flask正则路由,异常捕获和请求钩子
    Flask的路由,视图和相关配置
    Flask搭建虚拟环境
    Flask框架简介
    Django的类视图和中间件
    Django中的cookies和session
    Django请求与响应
    第6章 服务模式 Service Interface(服务接口)
    第6章 服务模式
    第5章分布式系统模式 Singleton
  • 原文地址:https://www.cnblogs.com/hanbinggan/p/4226342.html
Copyright © 2011-2022 走看看