• hdu 1372 & poj 2243(bfs)


    http://poj.org/problem?id=2243 

    擦了个擦的,N久前在杭电上做的这题0ms,到poj上一交直接超时!poj根本不让用stl啊。。

      没办法,只能模拟队列又乱敲了遍。

    code1: 

    #include<cstdio>
    #include<queue>
    using namespace std ;
    char bstr[3], estr[3] ;
    int b[2], e[2] ;
    int tur[8][2] = {-1, -21, -2, -2, -12, -1, -1212, -2121} ;
    struct Point{
        int x ;
        int y ;
        int step ;
    };
    void bfs(){
        Point begin ;
        begin.x = b[0] ;
        begin.y = b[1] ;
        begin.step = 0 ;
        queue<Point> q ;
        q.push(begin) ;
        while(!q.empty()){
            Point p = q.front() ;
            q.pop() ;
            for(int k=0; k<8; k++){
                Point temp = p ;
                temp.x += tur[k][0] ;
                temp.y += tur[k][1] ;
                if(temp.x<0||temp.x>7||temp.y<0||temp.y>7)
                    continue ;
                temp.step ++ ;
                if(temp.x==e[0]&&temp.y==e[1]){
                    printf("To get from %s to %s takes %d knight moves.\n", bstr, estr, temp.step) ;
                    return ;
                }
                q.push(temp) ;
            }
        }
        return ;
    }
    int main(){
        while(~scanf("%s%s", bstr, estr)){
            b[0] = bstr[0] - 'a' ;
            b[1] = bstr[1] - '1' ;
            e[0] = estr[0] - 'a' ;
            e[1] = estr[1] - '1' ;
            bstr[2] = '\0' ;//用%s输出字符数组必须封好
            estr[2] = '\0' ;
            if(b[0]==e[0]&&b[1]==e[1]){
                printf("To get from %s to %s takes %d knight moves.\n", bstr, estr, 0) ;
                continue ;
            }
            bfs() ;
        }
        return 0 ;


    code2:

    #include<cstdio>
    using namespace std ;
    char bstr[3], estr[3] ;
    int b[2], e[2] ;
    int tur[8][2] = {-1, -21, -2, -2, -12, -1, -1212, -2121} ;
    struct Point{
        int x ;
        int y ;
        int step ;
    }q[10000] ;
    void bfs(){
        Point begin ;
        int h, r ;
        h = 0, r = 1 ;
        begin.x = b[0] ;
        begin.y = b[1] ;
        begin.step = 0 ;
        q[0] = begin ;
        while(r>h){
            Point p = q[h++] ;
            for(int k=0; k<8; k++){
                Point temp = p ;
                temp.x += tur[k][0] ;
                temp.y += tur[k][1] ;
                if(temp.x<0||temp.x>7||temp.y<0||temp.y>7)
                    continue ;
                temp.step ++ ;
                if(temp.x==e[0]&&temp.y==e[1]){
                    printf("To get from %s to %s takes %d knight moves.\n", bstr, estr, temp.step) ;
                    return ;
                }
                q[r++] = temp ;
            }
        }
        return ;
    }
    int main(){
        while(~scanf("%s%s", bstr, estr)){
            b[0] = bstr[0] - 'a' ;
            b[1] = bstr[1] - '1' ;
            e[0] = estr[0] - 'a' ;
            e[1] = estr[1] - '1' ;
            bstr[2] = '\0' ;//用%s输出字符数组必须封好
            estr[2] = '\0' ;
            if(b[0]==e[0]&&b[1]==e[1]){
                printf("To get from %s to %s takes %d knight moves.\n", bstr, estr, 0) ;
                continue ;
            }
            bfs() ;
        }
        return 0 ;

    }

  • 相关阅读:
    html5游戏开发1Aptana 3 eclipse 插件安装
    HTML5 game Development Summary
    前端开发必备的工具
    前端优化方案
    JavaScript 事件冒泡简介及应用(转)
    标准W3C盒子模型和IE盒子模型CSS布局经典盒子模型(转)
    理解.NET中的异常(二)
    异常处理准则
    C#通讯编程
    C#读写文件总结
  • 原文地址:https://www.cnblogs.com/xiaolongchase/p/2264485.html
Copyright © 2020-2023  润新知