uva 10047 bfs 独轮车

#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
using namespace std;
const int INF=0x3f3f3f3f;
const int maxr=25+5;
const int maxc=25+5;
int R,C,sr,sc,tr,tc;
char maze[maxr][maxc];
struct State
{
    int r,c,dir,color;
    State(int r,int c,int dir,int color):r(r),c(c),dir(dir),color(color){}
}; //构造函数
const int dr[]={-1,0,1,0}; //north,west,south,east
const int dc[]={0,-1,0,1};
int d[maxr][maxc][4][5],  vis[maxr][maxc][4][5];
//记录距离(x,y,朝向,颜色) 是否访问
int ans;
queue<State>Q;
void update(int r,int c,int dir,int color,int v)
{
    if(r<0||r>=R||c<0||c>=C) return; //不能越界 最后一个bug在这里,第二个r打成c  ctm mmp
    if(maze[r][c]=='.'&&!vis[r][c][dir][color]){ //可达并且没被访问过
        Q.push(State(r,c,dir,color));//进队
        vis[r][c][dir][color]=1;
        d[r][c][dir][color]=v;
        if(r==tr&&c==tc&&color==0) ans=min(ans,v);//更新答案
    }
}
void bfs(State st)
{
    d[st.r][st.c][st.dir][st.color]=0;
    vis[st.r][st.c][st.dir][st.color]=1;
    Q.push(st);
    while(!Q.empty()){
        st=Q.front();Q.pop();
        int v=d[st.r][st.c][st.dir][st.color]+1;
        update(st.r,st.c,(st.dir+1)%4,st.color,v);//左转
        update(st.r,st.c,(st.dir+3)%4,st.color,v);//右转
        update(st.r+dr[st.dir],st.c+dc[st.dir],st.dir,(st.color+1)%5,v);//前进
    }
}
int main()
{
    int kcase=0;
    while(scanf("%d%d",&R,&C)==2&&R&&C){
        for(int i=0;i<R;i++){
            scanf("%s",maze[i]);
            for(int j=0;j<C;j++)
              if(maze[i][j]=='S'){
                sr=i;sc=j;
              }
              else if(maze[i][j]=='T'){
                tr=i;tc=j;//一开始把tr打成tc一直wa,改完之后直接崩掉ORZ
              }           //终于找到原因,运行的是另一个程序,因为一个工程中只能有一个main
                         //只要右键 激活当前项目就行了,激活后为粗黑体,但是还有bug OTZ
        }                //似乎什么都没动,但之前过不了的样例过了,但是还是wa哈哈哈哈
        maze[sr][sc]=maze[tr][tc]='.';
        ans=INF;
        memset(vis,0,sizeof(vis));
        bfs(State(sr,sc,0,0));
        if(kcase>0) printf("\n");
        printf("Case #%d\n",++kcase);
        if(ans==INF) printf("destination not reachable\n");
        else printf("minimum time = %d sec\n",ans);
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值