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 72oaround 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

    题意:一辆独轮车,他的车轮每72度变一个颜色(蓝,白,绿,黑,红)。。每秒有3种操作,左转,右转,或者前进,前进的时候车轮的颜色会向前进一色.要求出到终点且车轮触底颜色为蓝色的最小时间。

    初始时间为轮子触底为蓝,朝向北

    思路:

    用一个4维数组来存放状态 vis[x][y][颜色][朝向]..然后用BFS广搜,把每个状态的时间记录下来。。如果颜色和x,y对应上终点。就是最小时间。。

    #include <stdio.h>
    #include <string.h>
    int n, m;
    int judge;
    int tt = 1;
    int min;
    int d[4][2] = {{-1,0},{0,1},{1,0},{0,-1}};
    int vis[30][30][5][4];
    char map[30][30];
    struct Q
    {
        int x;
        int y;
        int color;
        int turn;
        int time;
    } q[15111];
    
    void bfs(int x, int y)
    {
        memset(vis, 0, sizeof(vis));
        int head = 1;
        int rear = 2;
        q[head].x = x;
        q[head].y = y;
        q[head].color = 0;
        q[head].turn = 0;
        q[head].time = 0;
        vis[x][y][0][0] = 1;
        while (head < rear)
        {		
    	int xx = q[head].x;
    	int yy = q[head].y;
    	int xxx = xx + d[q[head].turn][0];
    	int yyy = yy + d[q[head].turn][1];
    	int color = (q[head].color + 1) % 5;
    	int left = (q[head].turn + 3) % 4;
    	int right = (q[head].turn + 1) % 4;
    	if (map[xx][yy] == 'T' && q[head].color == 0)
    	{
    	    judge = 1;
    	    min = q[head].time;
    	    break;
    	}
    	if (xxx >= 0 && xxx < n && yyy >= 0 && yyy < m)
    	{
    	    if (map[xxx][yyy] != '#' && vis[xxx][yyy][color][q[head].turn] == 0)
    	    {
    		vis[xxx][yyy][color][q[head].turn] = 1;
    		q[rear].x = xxx;
    		q[rear].y = yyy;
    		q[rear].time = q[head].time + 1;
    		q[rear].color = color;
    		q[rear].turn = q[head].turn;
    		rear ++;
    	    }
    	}
    	if (vis[xx][yy][q[head].color][right] == 0)
    	{
    	    vis[xx][yy][q[head].color][right] = 1;
    	    q[rear].x = xx;
    	    q[rear].y = yy;
    	    q[rear].color = q[head].color;
    	    q[rear].time = q[head].time + 1;
    	    q[rear].turn = right;
    	    rear ++;
    	}
    	if (vis[xx][yy][q[head].color][left] == 0)
    	{
    	    vis[xx][yy][q[head].color][left] = 1;
    	    q[rear].x = xx;
    	    q[rear].y = yy;
    	    q[rear].color = q[head].color;
    	    q[rear].time = q[head].time + 1;
    	    q[rear].turn = left;
    	    rear ++;
    	}
    	head ++;
        }
    }
    int main()
    {
        while (scanf("%d%d", &n, &m) != EOF && n)
        {
    	judge = 0;
    	memset(q, 0, sizeof(q));
    	memset(map, 0, sizeof(map));
    	for (int i = 0; i < n; i ++)
    	    scanf("%s", map[i]);
    	for (int i = 0; i < n; i ++)
    	{
    	    for (int j = 0; j < m; j ++)
    	    {
    		if (map[i][j] == 'S')
    		{
    		    bfs(i, j);
    		    break;
    		}
    	    }
    	}
    	if (tt != 1)
    	    printf("
    ");
    	printf("Case #%d
    ", tt ++);
    	if (judge)
    	    printf("minimum time = %d sec
    ", min);
    	else
    	    printf("destination not reachable
    "); 
        }
        return 0;
    }


  • 相关阅读:
    Call指令和Ret指令讲解06 零基础入门学习汇编语言53
    【CSDN2012年度博客之星】喜欢本博客的读者,投票赠送《visual C++2010开发权威指南》电子稿感谢支持 ~(截至到2012年12月30日)
    本人新书< Visual C#2010开发权威指南>出版感谢大家一如既往的支持感谢CSDN总裁蒋涛以及他率领的CSDN团队提供的支持!
    在Windows Azure虚拟机上的SQL Server新教程
    Windows Azure Active Directory处理2000 亿个身份验证连接全球的人、 数据和设备
    宣布降低Windows Azure Storage的定价
    本人新书< Windows CE 7开发实战详解>出版感谢大家一如既往的支持感谢CSDN总裁蒋涛以及他率领的CSDN团队提供的支持!
    基于VC++2012图形化解决皇后问题
    3个 Windows Azure SQL Reporting开发的最佳做法
    现实世界的 Windows Azure: IT 公司提高其旗舰产品,为更多客户提供云解决方案
  • 原文地址:https://www.cnblogs.com/riskyer/p/3233626.html
Copyright © 2011-2022 走看看