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 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 midpoint 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 midpoint 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 midpoint 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 (, ) 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("\n");
printf("Case #%d\n", tt ++);
if (judge)
printf("minimum time = %d sec\n", min);
else
printf("destination not reachable\n");
}
return 0;
}
UVA 10047 The Monocycle (状态记录广搜)的更多相关文章
- UVa 10047 自行车 状态记录广搜
每个格子(x,y,drection,color) #include<iostream> #include<cstdio> #include<cstring> #in ...
- hdu5025 状态压缩广搜
题意: 悟空要救唐僧,中途有最多就把钥匙,和最多五条蛇,要求就得唐僧并且拿到所有种类的钥匙(两个1只拿一个就行),拿钥匙i之前必须拿到钥匙i-1,打蛇多花费一秒,问救出唐僧并且拿到所有种类 ...
- UVA 10047 - The Monocycle BFS
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...
- UVA 10047 The Monocycle
大白图论第二题··· 题意:独轮车的轮子被均分成五块,每块一个颜色,每走过一个格子恰好转过一个颜色. 在一个迷宫中,只能向前走或者左转90度或右转90度(我曾天真的认为是向左走和向右走···),每个操 ...
- uva 10047 The Monocycle(搜索)
好复杂的样子..其实就是纸老虎,多了方向.颜色两个状态罢了,依旧是bfs. 更新的时候注意处理好就行了,vis[][][][]要勇敢地开. 不过这个代码交了十几遍的submission error,手 ...
- POJ1324贪吃蛇(状态压缩广搜)
题意: 给你一个地图,有的地方能走,有的地方不能走,然后给你一条蛇,问你这条蛇的头部走到1,1的位置的最少步数,注意,和贪吃蛇不太一样,就是蛇咬到自己身体的那个地方,具体怎么不一样自己模拟 ...
- hdu 5025 Saving Tang Monk 状态压缩dp+广搜
作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4092939.html 题目链接:hdu 5025 Saving Tang Monk 状态压缩 ...
- hdu 5094 Maze 状态压缩dp+广搜
作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4092176.html 题目链接:hdu 5094 Maze 状态压缩dp+广搜 使用广度优先 ...
- Oj 24260: Lilypad Pond (神奇广搜题,状态搜索)
题目 为了让奶牛们娱乐和锻炼,约翰建造了一个美丽的池塘.这个池塘是矩形的,可以分成M×N个方格.一些格子是坚固得令人惊讶的莲花,还有一些是岩石,其余的只是美丽,纯净,湛蓝的水.贝西正在练习芭蕾舞,她站 ...
随机推荐
- SimpleXML解析xml文件
SimpleXML 扩展提供了一种获取 XML 元素的名称和文本的简单方式. 与 DOM 或 Expat 解析器相比,SimpleXML 仅仅用几行代码就可以从 XML 元素中读取文本数据. Simp ...
- 小笔记(一):ajax传递数组及将ajax返回数据赋值
当使用ajax传递数据时,有可能传递多个数据,这是使用以下方法传递数据就会显得数据过多且混杂 $.ajax({ type:'post', url:url, data:{data:data,conten ...
- mysql统计表的大小
如下是sql语句: SELECT TABLE_NAME as name,DATA_LENGTH+INDEX_LENGTH as len,TABLE_ROWS as rows FROM informat ...
- php提取背景图片
preg_match_all('/background\s*-\s*+image\s*:\s*url\s*\("*([^"]*)"*\)/i', $content,$ma ...
- 查看 MySQL 数据库中每个表占用的空间大小
TABLE_SCHEMA : 数据库名TABLE_NAME:表名ENGINE:所使用的存储引擎TABLES_ROWS:记录数DATA_LENGTH:数据大小INDEX_LENGTH:索引大小 SELE ...
- 黑马程序员-------.net基础知识四
常量(静态常量------compile-time) 语法:const 类型 常量名 =常量值: 例: [csharp] view plaincopyprint? { const double PI= ...
- 【原创】Mvc学习笔记(1)
1.新建MVC4项目 在MVC4中有App_Data文件夹,这个文件夹里可以放一些重要的数据,比如说数据库的mdf文件等等,这个文件夹非常安全,因为这个文件夹不允许被别人下载,不允许被浏览器访问. A ...
- BZOJ2191Splite
Description 给两个多边形,问否在平移旋转不翻转不重叠的情况下拼成一个凸多边形. Input 每组第一行一个数N表示第一个多边形的顶点数,下接N行按顺序(逆/顺时针)给出顶点坐标,再下一行给 ...
- Java2_Java泛型
一. 泛型概念的提出(为什么需要泛型)? 首先,我们看下下面这段简短的代码: 1 public class GenericTest { 2 3 public static void main(Stri ...
- 文件操作 - NSFileManager
iOS的沙盒机制,应用只能访问自己应用目录下的文件.iOS不像android,没有SD卡概念,不能直接访问图像.视频等内容.iOS应用产生的内容,如图像.文件.缓存内容等都必须存储在自己的沙盒内.默认 ...