zoukankan      html  css  js  c++  java
  • uva 10047 The Monocycle

      Problem A: The Monocycle 

    A monocycle is a cycle that runs on one wheel and the one we will be considering is a bit more special. It has a solid wheel colored with five different colors as shown in the figure:

    The colored segments make equal angles (72o) at the center. A monocyclist rides this cycle on an $M 	imes N$ grid of square tiles. The tiles have such size that moving forward from the center of one tile to that of the next one makes the wheel rotate exactly 72o around its own center. The effect is shown in the above figure. When the wheel is at the center of square 1, the mid­point of the periphery of its blue segment is in touch with the ground. But when the wheel moves forward to the center of the next square (square 2) the mid­point of its white segment touches the ground.

    Some of the squares of the grid are blocked and hence the cyclist cannot move to them. The cyclist starts from some square and tries to move to a target square in minimum amount of time. From any square either he moves forward to the next square or he remains in the same square but turns 90o left or right. Each of these actions requires exactly 1 second to execute. He always starts his ride facing north and with the mid­point of the green segment of his wheel touching the ground. In the target square, too, the green segment must be touching the ground but he does not care about the direction he will be facing.

    Before he starts his ride, please help him find out whether the destination is reachable and if so the minimum amount of time he will require to reach it.

    Input 

    The input may contain multiple test cases.

    The first line of each test case contains two integers M and N ($1 le M$, $N le 25$) giving the dimensions of the grid. Then follows the description of the grid in M lines of N characters each. The character `#' will indicate a blocked square, all other squares are free. The starting location of the cyclist is marked by `S' and the target is marked by `T'. The input terminates with two zeros for M and N.

    Output 

    For each test case in the input first print the test case number on a separate line as shown in the sample output. If the target location can be reached by the cyclist print the minimum amount of time (in seconds) required to reach it exactly in the format shown in the sample output, otherwise, print ``destination not reachable".

    Print a blank line between two successive test cases.

    Sample Input 

    1 3
    S#T
    10 10
    #S.......#
    #..#.##.##
    #.##.##.##
    .#....##.#
    ##.##..#.#
    #..#.##...
    #......##.
    ..##.##...
    #.###...#.
    #.....###T
    0 0
    

    Sample Output 

    Case #1
    destination not reachable
     
    Case #2
    minimum time = 49 sec
    

    Miguel Revilla
    2000-12-26
    各个点仅用坐标区分很难计算结果,所以可以把各个点扩展为状态(位置,独轮车朝向,轮底颜色)这样状态至多25*25*4*5种,因此可BFS搜
    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #include<vector>
    #include<set>
    #include<queue>
    #include<string>
    #include<cmath>
    #include<fstream>
    #include<iomanip>
    #include<climits>
    #include<cfloat>
    
    using namespace std;
    
    #define MAX_INT 0x7fffffff
    #define MAX_LL 0x7fffffffffffffff
    #define ULL unsigned long long
    #define LL long long
    #define MAX(x,y) ((x) > (y) ? (x) : (y))
    #define MIN(x,y) ((x) < (y) ? (x) : (y))
    
    #define MAXN 26
    int r,c;
    char g[MAXN][MAXN];
    bool vis[MAXN][MAXN][4][5];
    int sr,sc,er,ec;
    const int dr[]={-1,1,0,0},
        dc[]={0,0,-1,1},
        dir[]={0,1,2,3},        //north,south,west,east
        rdr[]={1,0,3,2};
    int dis[MAXN][MAXN][4][5];
    
    struct node{
        int x,y,d,c;
        node(int x0,int y0,int d0,int c0):
            x(x0),y(y0),d(d0),c(c0)     {}
    };
    
    int bfs(){
        memset(vis, 0, sizeof(vis));
        queue<node> q;
        q.push(node(sr,sc,0,0));    vis[sr][sc][0][0]=true;
        dis[sr][sc][0][0]=0;
        while(!q.empty()){
            node t=q.front();   q.pop();
            int td=dis[t.x][t.y][t.d][t.c];
            for(int i=0; i<4; i++){     //take turns
                int x=t.x,y=t.y,c=t.c,d=dir[i];
                bool &v=vis[x][y][d][c];
                if(rdr[t.d]!=d && !v){
                    dis[x][y][d][c]=td+1;
                    v=true;     q.push(node(x,y,d,c));
                }
            }
            int nx=t.x+dr[t.d], ny=t.y+dc[t.d];
            int nc=(t.c+1)%5,nd=t.d;
            if(nx>-1 && nx<r && ny>-1 && ny<c && g[nx][ny]!='#' && !vis[nx][ny][nd][nc]){
                if(g[nx][ny]=='T' && nc==0) return 1+td;
                dis[nx][ny][nd][nc]=td+1;
                q.push(node(nx,ny,nd,nc));
                vis[nx][ny][nd][nc]=true;
            }
        }
        return -1;
    }
    
    int main(){
        //freopen("C:\Users\Administrator\Desktop\in.txt","r",stdin);
        int kase=1;
        while(scanf(" %d %d",&r,&c)==2 && r&&c){        getchar();
            int i,j;
            for(i=0; i<r; i++){
                gets(g[i]);
                for(j=0; j<c; j++) if(g[i][j]=='S'){
                    sr=i;   sc=j;
                }
            }
            int ans=bfs();
            if(kase>1) printf("
    ");
            printf("Case #%d
    ",kase++);
            if(ans==-1) printf("destination not reachable
    ");
            else printf("minimum time = %d sec
    ",ans);
        }
        return 0;
    }
    
  • 相关阅读:
    vue 基础补充
    正则
    vue 指令
    函数式编程FP 初探
    .? ?? es2020
    vue alfont scss
    网络安全靶场通关指南
    Java 程序设计——站内短信系统
    Java 程序设计——登录系统
    动态规划法解找零钱问题
  • 原文地址:https://www.cnblogs.com/ramanujan/p/3271796.html
Copyright © 2011-2022 走看看