zoukankan      html  css  js  c++  java
  • UVa10047-The Monocycle(bfs)

    题目链接

    分析:
    我们把颜色和方向连同坐标一起加入状态
    之后bfs搜索就好了

    注意:
    转向是不会改变颜色的
    相邻两组数据的输出之间应有一个空行

    找到了额外的样例
    (不要直接复制哦,博客上的格式可能有问题)

    Sample Input

    1 3
    S#T
    10 10
    #S…….#
    #..#.##.##
    #.##.##.##
    .#….##.#
    ##.##..#.#
    #..#.##…
    #……##.
    ..##.##…
    #.###…#.
    #…..###T
    5 5
    #S#..
    #.#..
    #.###
    #…T
    #####
    10 10
    S………
    ……….
    ……….
    ……….
    ……….
    ……….
    ……….
    ……….
    ……….
    ……..T.
    3 3
    ST#
    ##.
    .#.
    25 25
    S……………………
    …………………….
    …………………….
    …………………….
    …………………….
    …………………….
    …………………….
    …………………….
    …………………….
    …………………….
    …………………….
    …………………….
    …………………….
    …………………….
    …………………….
    …………………….
    …………………….
    …………………….
    …………………….
    …………………….
    …………………….
    …………………….
    …………………….
    …………………….
    ……………………T
    6 6
    #.#…
    #.S.#.
    #####.
    #..#..
    #T##..
    ……
    0 0

    Sample Output

    Case #1
    destination not reachable

    Case #2
    minimum time = 49 sec

    Case #3
    minimum time = 17 sec

    Case #4
    minimum time = 30 sec

    Case #5
    minimum time = 14 sec

    Case #6
    minimum time = 55 sec

    Case #7
    minimum time = 30 sec

    tip

    不要无脑输出空行
    输出的最后不能有多余空行

    坐标和颜色的计算一定要注意

    这就告诉我们一个真理:
    比赛的时候,一定要用文件操作看一下输出格式是否合法
    再也不要输出多余空格,空行和回车了

    //这里写代码片
    #include<cstdio>
    #include<cstring>
    #include<iostream>
    
    using namespace std;
    
    int n,m;
    struct node{
        int x,y,d,c,t;
    };
    node q[50000];
    char s[30];
    int tou,wei,mp[30][30];
    bool p[30][30][4][5];
    node t;
    
    int pd(int bh)
    {
        if (q[bh].x==t.x&&q[bh].y==t.y&&q[bh].c==0) return 1;
        return 0;
    }
    
    int bfs()
    {
        do
        {
            tou++;
            int xa=q[tou].x;
            int ya=q[tou].y;
            int cc=q[tou].c;
            int da=q[tou].d;
            int xx=xa,yy=ya,dd=da;
            int time=q[tou].t+1;
    
            //  朝向不变
            if (da==0) xx=xa-1;
            else if (da==1) yy=ya+1;
            else if (da==2) xx=xa+1;
            else yy=ya-1;
            if (xx>0&&yy>0&&xx<=n&&yy<=m&&p[xx][yy][dd][(cc+1)%5]&&mp[xx][yy]!=-1)
            {
                wei++;
                p[xx][yy][dd][(cc+1)%5]=0;
                q[wei].x=xx; q[wei].y=yy;
                q[wei].d=dd; q[wei].c=(cc+1)%5;
                q[wei].t=time;
                if (pd(wei)) return time;
            } 
    
            //左转
            xx=xa,yy=ya;
            if (da==0) dd=3;
            else if (da==1) dd=0;
            else if (da==2) dd=1;
            else dd=2;
            if (p[xx][yy][dd][cc]&&mp[xx][yy]!=-1)
            {
                wei++;
                p[xx][yy][dd][cc]=0;
                q[wei].x=xx; q[wei].y=yy;
                q[wei].d=dd; q[wei].c=cc;
                q[wei].t=time;
                if (pd(wei)) return time;
            } 
    
            //右转
            xx=xa,yy=ya;
            if (da==0) dd=1;
            else if (da==1) dd=2;
            else if (da==2) dd=3;
            else dd=0;
            if (p[xx][yy][dd][cc]&&mp[xx][yy]!=-1)
            {
                wei++;
                p[xx][yy][dd][cc]=0;
                q[wei].x=xx; q[wei].y=yy;
                q[wei].d=dd; q[wei].c=cc;
                q[wei].t=time;
                if (pd(wei)) return time;
    
                mp[xx][yy]=time;
    
            }  
        }
        while (tou<wei);
        return -1;
    }
    
    int main()
    {
        int T=0;
        int ans=-2;
        scanf("%d%d",&n,&m);
        while (n!=0&&m!=0)
        {
            if (ans!=-2) puts("");
    
            tou=wei=0;
            memset(p,1,sizeof(p));
            memset(mp,0,sizeof(mp));
            for (int i=1;i<=n;i++)
            {
                scanf("%s",s+1);
                for (int j=1;j<=m;j++)
                    if (s[j]=='#')
                        mp[i][j]=-1;
                    else if (s[j]=='S')
                    {
                        wei++;
                        q[wei].x=i; q[wei].y=j;
                        q[wei].d=0; q[wei].c=0;
                        q[wei].t=0;
                        p[i][j][0][0]=0;
                    }
                    else if (s[j]=='T')
                    {
                        t.x=i;
                        t.y=j;
                        t.c=0;
                    }
            }
            printf("Case #%d
    ",++T);
            ans=bfs();
            if (ans!=-1)
                printf("minimum time = %d sec
    ",ans);
            else
                printf("destination not reachable
    ");
            scanf("%d%d",&n,&m);
        }
        return 0;
    }
  • 相关阅读:
    Web For Pentester 学习笔记
    Vulhub Docker环境部署
    XSS 渗透思路笔记
    文件上传Upload 漏洞挖掘思路
    文件上传Upload 学习笔记
    XCTF-WEB-高手进阶区-upload1-笔记
    XCTF-WEB-高手进阶区-PHP2-笔记
    XCTF-WEB-高手进阶区-NaNNaNaNNaN-Batman-笔记
    XCTF-WEB-高手进阶区-Web_python_template_injection-笔记
    用JavaScript制作简单的计算器
  • 原文地址:https://www.cnblogs.com/wutongtong3117/p/7673014.html
Copyright © 2011-2022 走看看