• UVA 10047 The Monocycle 解题报告


    题意:给定一个轮子,轮子只能朝着一个反向运动。。轮子上有五种不同的颜色,给定一个迷宫,轮子在一个格子转动90度耗费的时间为1,向前的时间也为1 ,求起点和终点轮子覆盖的颜色相同的最短时间:

    解题思路:开始以为visit只能代表迷宫的一个格子,原来它表示的是一种状态,,,用bfs ,,,str【】【】没初始化wrong了几次(注意啊)

    解题代码:

    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    struct node
    {
        int dir,x,y,time,color;
    }list[10000000];
    int visit[30][30][10][10];//状态
    int nextxdir[4] = {-1,0,1,0};
    int nextydir[4] = {0,1,0,-1};//方向
    int main()
    {
        int n,m,CASE = 0;
        while(scanf("%d %d",&n,&m)!= EOF)
        {
            CASE ++;
            if(n == 0 && m == 0 )
                break;
            memset(list,0,sizeof(list));
            memset(visit,0,sizeof(visit));
            char str[50][50];
            memset(str,0,sizeof(str));
            int bx,by,ex,ey;
            for(int i = 1; i <= n; i ++)
            {
                scanf("%s",&str[i][1]);
                for(int j = 1; j <= m; j ++)
                {
                    if(str[i][j] == 'S')
                    {
                        bx = i;
                        by = j;
                        str[i][j] = '.';
                    }
                    if(str[i][j] == 'T')
                    {
                        ex = i ;
                        ey = j;
                        str[i][j] = '.';
                    }
                }
            }
            list[1].dir = 0 ;
            list[1].x = bx;
            list[1].y = by;
            list[1].time = 0;
            list[1].color = 0 ;
            visit[bx][by][0][0]  = 1;
            int low = 1,high = 1 ,ok = 0;
            while(low <= high)
            {
                if(list[low].x == ex && list[low].y == ey && list[low].color == 0)
                {
                    ok = 1;
                    break;
                }
                for(int i = 0 ; i <= 3; i ++)
                {
                    if(!visit[list[low].x][list[low].y][i][list[low].color] && i != (list[low].dir+2)%4)
                    {
                        visit[list[low].x][list[low].y][i][list[low].color] = 1;
                        high++;
                        list[high].dir = i ;
                        list[high].x = list[low].x;
                        list[high].y = list[low].y;
                        list[high].time = list[low].time+1;
                        list[high].color = list[low].color  ;
                    }
                }//原地
                int tx =  list[low].x + nextxdir[list[low].dir];
                int ty =  list[low].y + nextydir[list[low].dir];
                int tc = (list[low].color +1)%5;
                if(!visit[tx][ty][list[low].dir][tc] && str[tx][ty] == '.')
                {
                     visit[tx][ty][list[low].dir][tc] = 1;
                     high ++;
                     list[high].dir = list[low].dir ;
                     list[high].x = tx;
                     list[high].y = ty;
                     list[high].time = list[low].time+1;
                     list[high].color = tc  ;
    
                }//前进
              low++;
            }
            if(CASE != 1)
              printf("
    ");
             printf("Case #%d
    ",CASE);
            if(ok)
             printf("minimum time = %d sec
    ",list[low].time);
            else printf("destination not reachable
    ");
    
        }
      return 0;
    
    }
    View Code
    没有梦想,何谈远方
  • 相关阅读:
    转:测试驱动开发全攻略
    转:如何提高自己的归纳总结能力?
    转:从编译链接过程解析static函数的用法
    C++ 不能在类体外指定关键字static
    转:画图解释 SQL join 语句
    转:[置顶] 从头到尾彻底理解KMP(2014年8月22日版)
    转:The Knuth-Morris-Pratt Algorithm in my own words
    转:数组与指针的区别
    删除单链表中间节点
    如果判断两个单链表有交?第一个交点在哪里?
  • 原文地址:https://www.cnblogs.com/zyue/p/3149052.html
Copyright © 2020-2023  润新知