栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

poj 3454 Hypertheseus

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

poj 3454 Hypertheseus

#include <stdio.h>#include <string.h>const int MAXCUBE = 1048576 ;int d ;int n[21] ;int mv[21] ;char space[MAXCUBE] ;int q[MAXCUBE] ;//队列int stp[MAXCUBE] ;//到达space[i]的步数int front , rear ;bool vst[MAXCUBE] ;//判重int p ;int s , m , t , curs;void visit(int pt){    if ( !(pt >= 0 && pt < mv[d]) ) {        return ;    }    if ( vst[pt] || space[pt] ) return ;    vst[pt] = true ;    stp[pt] = curs ;    q[rear] = pt ;    rear ++ ;}int bfs(int from , int to ){    int curp , newp ;    int i ;    memset(vst , false , sizeof(vst)) ;    front = 0 ;    rear = 1 ;    q[0] = from ;    stp[from] = 0 ;    vst[from] = true ;    while ( front < rear ){        curp = q[front ++] ;        curs = stp[curp] + 1;        if ( curp == to ) { front -- ; break ;        }        for ( i = 0 ; i < d ; i ++ ){ if ( ( ( curp ) / mv[i] ) % n[i] != n[i] - 1 ){     newp = curp + mv[i] ;     visit(newp); } if ( ( ( curp ) / mv[i] ) % n[i] != 0 ){     newp = curp - mv[i] ;     visit(newp); }        }    }    if ( front == rear ) return - 1;    else return stp[curp] ;}char nextchar(){    char ch ;    do{        ch = getchar() ;    }while ( ch != '#' && ch != '.' && ch != 'S' && ch != 'M' && ch != 'T' ) ;    if ( ch == 'S' ) s = p ;    if ( ch == 'M' ) m = p ;    if ( ch == 'T' ) t = p ;    if ( ch == '#' ) return 1 ;    else return 0 ;}void readmap(int layer){    int i;    if ( layer == 0 ){        space[p ++ ] = nextchar() ;        return ;    }    for ( i = 0 ; i < n[layer - 1] ; i ++ ) readmap( layer - 1 ) ;}int main(){    int i ;    int ans ;    while (1){        scanf("%d",&d);        if ( d == 0 ) break ;        for ( i = 0 ; i < d ; i ++ ){ scanf("%d",n + i ) ;         }        mv[0] = 1 ;        for ( i = 1 ; i <= d ; i ++ ){ mv[i] = n[i - 1] * mv[i - 1] ;        }        p = 0 ;        readmap(d) ;        ans = 0 ;        space[m] = 1 ;        i = bfs(t,s) ;        if ( i == -1 ){ ans = -1 ;        }else{ space[m] = 0 ; ans += i ; i = bfs(s,m) ; if ( i == -1 ){     ans = -1 ; }else{     ans += i ;     i = bfs(m,t) ;     if ( i == -1 ){         ans = -1 ;     }else{         ans += i ;     } }        }        if ( ans == -1 )puts("No solution. Poor Theseus!");        else{ printf("Theseus needs %d steps.n",ans);        }    }        return 0 ;}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/375860.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号