大白图论第二题···

题意:独轮车的轮子被均分成五块,每块一个颜色,每走过一个格子恰好转过一个颜色。

在一个迷宫中,只能向前走或者左转90度或右转90度(我曾天真的认为是向左走和向右走···),每个操作的时间是1s。

在起点轮子的绿色块着地,方向向北,要求到终点时同样是绿色块着地,方向不限,求最短时间,若走不到输出”destination not reachable“。每个样例中间空一行(没错我又因为这个wa了···uva上竟然没有pe···【捂脸跑开】)

解法:BFS。visit数组多加两维记录方向和颜色,dir数组从北开始按顺时针或逆时针存,当下标的奇偶性和当前方向不同时为向左右转,当到达终点并颜色为起始颜色时得到答案。

代码:

#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string>
#include<string.h>
#include<math.h>
#include<map>
#include<queue>
#define ll long long
using namespace std;
struct node
{
int x,y,d,c,step;//横纵坐标,方向,颜色,时间
node(int x,int y,int d,int c,int step):x(x),y(y),d(d),c(c),step(step) {}
node() {}
};
int dir[4][2]= {-1,0,0,1,1,0,0,-1};//顺时针存,一维下标奇偶性相同的为相对的两个方向
int main()
{
int cnt=1;
int n,m;
while(~scanf("%d%d",&n,&m)&&!(n==0&&m==0))
{
int stx,sty;
string maze[30];
for(int i=0; i<n; i++)
{
cin>>maze[i];
for(int j=0; j<m; j++)
if(maze[i][j]=='S')
{
stx=i;
sty=j;//记录起始点
}
}
int visit[30][30][4][5]= {0};//横纵坐标,方向,颜色
queue<node> q;
q.push(node(stx,sty,0,0,0));
visit[stx][sty][0][0]=1;
int ans=-1;
while(!q.empty())
{
node temp=q.front();
q.pop();
if(maze[temp.x][temp.y]=='T'&&temp.c==0)
{
ans=temp.step;
break;
}
for(int i=0; i<4; i++)
{
if(!((i!=temp.d)&&((i&1)==(temp.d&1))))//判断反向是否合法【看起来写屎了···
{
int tx=temp.x+dir[i][0],ty=temp.y+dir[i][1];
if(i==temp.d)//直走的情况
{
if(tx>=0&&tx<n&&ty>=0&&ty<m&&maze[tx][ty]!='#'&&!visit[tx][ty][i][(temp.c+1)%5])
{
q.push(node(tx,ty,i,(temp.c+1)%5,temp.step+1));
visit[tx][ty][i][(temp.c+1)%5]=1;
}
}
else//转弯的情况
{
if(!visit[temp.x][temp.y][i][temp.c])
{
visit[temp.x][temp.y][i][temp.c]=1;
q.push(node(temp.x,temp.y,i,temp.c,temp.step+1));
}
}
}
}
}
if(cnt!=1)
puts("");//【吐槽】再次被坑
printf("Case #%d\n",cnt++);
if(ans!=-1)
printf("minimum time = %d sec\n",ans);
else
puts("destination not reachable");
}
return 0;
}

代码丑···见谅

UVA 10047 The Monocycle的更多相关文章

  1. UVA 10047 The Monocycle (状态记录广搜)

    Problem A: The Monocycle  A monocycle is a cycle that runs on one wheel and the one we will be consi ...

  2. UVA 10047 - The Monocycle BFS

    http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...

  3. uva 10047 The Monocycle(搜索)

    好复杂的样子..其实就是纸老虎,多了方向.颜色两个状态罢了,依旧是bfs. 更新的时候注意处理好就行了,vis[][][][]要勇敢地开. 不过这个代码交了十几遍的submission error,手 ...

  4. UVA 11624 UVA 10047 两道用 BFS进行最短路搜索的题

    很少用bfs进行最短路搜索,实际BFS有时候挺方便得,省去了建图以及复杂度也降低了O(N*M): UVA 11624 写的比较挫 #include <iostream> #include ...

  5. UVa 10047,独轮车

    题目链接:https://uva.onlinejudge.org/external/100/10047.pdf 题目链接:http://vjudge.net/contest/132239#proble ...

  6. UVa (BFS) The Monocycle

    题目不光要求要到达终点而且要求所走的步数为5的倍数,每个时刻有三个选择,前进,左转弯,右转弯. 所以在vis数组中新增加两个维度即可,vis[x][y][dir][color]表示在(x, y)格子方 ...

  7. uva 10047 the monocyle (四维bfs)

    算法指南白书 维护一个四维数组,走一步更新一步 #include<cstdio> #include<cstring> #include<queue> #includ ...

  8. UVa 10047 自行车 状态记录广搜

    每个格子(x,y,drection,color) #include<iostream> #include<cstdio> #include<cstring> #in ...

  9. UVA题目分类

    题目 Volume 0. Getting Started 开始10055 - Hashmat the Brave Warrior 10071 - Back to High School Physics ...

随机推荐

  1. Ubuntu环境下手动配置ant

    配置ant 1. 下载ant(http://ant.apache.org/bindownload.cgi) 例如我下载的是:apache-ant-1.9.4-bin.tar.gz 解压ant,将文件夹 ...

  2. Bessie的体重问题

    P1028 Bessie的体重问题 时间: 1000ms / 空间: 131072KiB / Java类名: Main 背景 USACO OCT09 8TH  描述 Bessie像她的诸多姊妹一样,因 ...

  3. hdu 1002 java 大数相加

    package Main; //import java.io.InputStream; import java.math.BigDecimal; import java.util.Scanner; p ...

  4. [hackerrank]Closest Number

    https://www.hackerrank.com/contests/w5/challenges/closest-number 简单题. #include <iostream> #inc ...

  5. 【Linux高频命令专题(8)】五大查询命令

    find 格式 find 路径 -命令参数 [输出形式] 路径:告诉find在哪儿去找你要的东西 命令参数:参考下面 输出形式:输出形式很多,-print,-printf,-print,-exec,- ...

  6. POSIX semaphore: sem_open, sem_close, sem_post, sem_wait

    http://www.cnblogs.com/BloodAndBone/archive/2011/01/18/1938552.html 一.Posix有名信号灯 1.posix有名信号灯函数 函数se ...

  7. BS架构与CS架构的区别

    C/S结构,即Client/Server(客户机/服务器)结构,是大家熟知的软件系统体系结构,通过将任务合理分配到Client端和Server端,降低了系统的通讯开销,可以充分利用两端硬件环境的优势. ...

  8. arcgis api for javascript 出现 undefinedModule错误

    一般都是script代码里面语法错误,如. .;:之类的

  9. 代码自动生成工具_java版

    项目结构: 这里要实现的功能是,当我们给出了bean,如:Admin,User,People等实体类后, 我想用代码自动生成我想要的代码,最后生成的效果: 也就是说为每一个bean都生成相应的Dao, ...

  10. lightOJ 1132 Summing up Powers(矩阵 二分)

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1132 题意:给出n和m.求sum(i^m)%2^32.(1<=i<=n) ...