【BFS】uva10047The Monocycle

/*
本题的特殊之处,到达一个格子时,因为朝向不同,以及接触地面的颜色不同,
会处于不同的状态;;;;;;;;;
把(x, y, d, c)作为一个结点,表示所在位置(x, y),方向为d,颜色为c;;;;;
------------------------------------------------------------------------
在方向上我们把前,左,右编号为0,1,2;;;;
颜色,从蓝色开始编号为0,1,2,3;;;;;;;;;;
--------------------------------------------------------------------------
vis[x1][y1][0][0] = 1;起点S进入队列
node a, b;
a.x = x1;
a.y = y1;
a.d = 0;
a.c = 0;
a.t = 0;
q.push(a);
-----------------------------------------------------------------------
b.t = a.t + 1;前进方向;;;时间加1
b.c = (a.c + 1)%5;扩展后的颜色
b.x = a.x + d[a.d][0];扩展后的位置
b.y = a.y + d[a.d][1];
b.d = a.d;方向不变
if(judge(b))
{
    if(b.x == x2 && b.y == y2 && b.c == 0)是否到达终点
        return b.t;
    vis[b.x][b.y][b.d][b.c] = 1;
    q.push(b);
}
----------------------------------------------------------------------------------
b = a;向右转
b.t ++;
b.d = (b.d + 1)%4;扩展后的方向
if(judge(b))
{
    vis[b.x][b.y][b.d][b.c] = 1;
    q.push(b);
}
b = a;向左转
b.t ++;
b.d = (b.d - 1 + 4)%4;
if(judge(b))
{
    vis[b.x][b.y][b.d][b.c] = 1;
    q.push(b);
}
-------------------------------------------------------------------
for(int i=0; i<m; i++)寻找始末点S和T点
{
    for(int j=0; j<n; j++)
    {
        if(g[i][j] == 'S')
        {
            x1 = i;
            y1 = j;
            break;
        }
    }
}
for(int i=0; i<m; i++)
{
    for(int j=0; j<n; j++)
    {
        if(g[i][j] == 'T')
        {
            x2 = i;
            y2 = j;
            break;
        }
    }
}
------------------------------------------------------------------------------

*/
#include <iostream>
#include <cstdio>
#include <queue>
#include <cstring>
#include <algorithm>
#define INF 0x3f3f3f3f

using namespace std;

const int MAXN = 30;

struct node
{
    int x, y, d, c, t;
};

int m, n;
char g[MAXN][MAXN];
int vis[MAXN][MAXN][6][6];
int x1, x2, y1, y2;
int d[4][2] = {{-1,0},{0,1},{1,0},{0,-1}};

int judge(node b)
{
    if(b.x < 0 || b.x >= m || b.y < 0 || b.y >= n)
        return false;
    if(g[b.x][b.y] == '#')
        return false;
    if(vis[b.x][b.y][b.d][b.c])
        return false;
    return true;
}

int bfs()
{
    queue<node> q;
    vis[x1][y1][0][0] = 1;
    node a, b;
    a.x = x1;
    a.y = y1;
    a.d = 0;
    a.c = 0;
    a.t = 0;
    q.push(a);
    while(!q.empty())
    {
        a = q.front();
        q.pop();
        b.t = a.t + 1;
        b.c = (a.c + 1)%5;
        b.x = a.x + d[a.d][0];
        b.y = a.y + d[a.d][1];
        b.d = a.d;
        if(judge(b))
        {
            if(b.x == x2 && b.y == y2 && b.c == 0)
                return b.t;
            vis[b.x][b.y][b.d][b.c] = 1;
            q.push(b);
        }
        b = a;
        b.t ++;
        b.d = (b.d + 1)%4;
        if(judge(b))
        {
            vis[b.x][b.y][b.d][b.c] = 1;
            q.push(b);
        }
        b = a;
        b.t ++;
        b.d = (b.d - 1 + 4)%4;
        if(judge(b))
        {
            vis[b.x][b.y][b.d][b.c] = 1;
            q.push(b);
        }
    }
    return -1;
}

int main()
{
    //freopen("input.txt","r",stdin);
    int kase = 1;
    while(scanf("%d%d",&m,&n) != EOF && (n+m))
    {
        if(kase != 1)
            printf("\n");
        printf("Case #%d\n",kase++);
        getchar();
        for(int i=0; i<m; i++)
            gets(g[i]);
        for(int i=0; i<m; i++)
        {
            for(int j=0; j<n; j++)
            {
                if(g[i][j] == 'S')
                {
                    x1 = i;
                    y1 = j;
                    break;
                }
            }
        }
        for(int i=0; i<m; i++)
        {
            for(int j=0; j<n; j++)
            {
                if(g[i][j] == 'T')
                {
                    x2 = i;
                    y2 = j;
                    break;
                }
            }
        }
        memset(vis, 0, sizeof(vis));
        int time = bfs();
        if(time == -1)
            printf("destination not reachable\n");
        else
            printf("minimum time = %d sec\n",time);
    }
    return 0;
}

----------------------------

wuwuwuwuwuwuwuwu...................................

【BFS】uva10047The Monocycle,布布扣,bubuko.com

时间: 2024-10-12 04:17:56

【BFS】uva10047The Monocycle的相关文章

【bfs】【中等难度】tyvj P1234 - bench与奔驰

P1234 - bench与奔驰 From zhangbh001    Normal (OI) 总时限:10s    内存限制:128MB    代码长度 限制:64KB P1234 - bench与奔驰 背景 Background 公园里有个人在练开奔驰 - -!,但是总是撞在bench上 (众人曰:狼来了,快跑啊!) 描述 Description 公园里的bench与奔驰都是无敌的,不会被撞坏.由于开奔驰的人比较"有特点",总是向上下左右四个方向开,而且只会在撞到椅子之后改变方向(

hdoj 1312 Red and Black 【BFS】

题意:一共有四个方向,从'@'出发,找能到达'.'的个数, #是不能通过的. 策略:广搜. 这道题属于最简单的bfs了. 代码: #include<stdio.h> #include<string.h> #include<queue> using std::queue; bool vis[25][25]; char s[25][25]; int n, m; int ans = 0; struct node{ int x, y; }; node st; const int

HDU1242 Rescue 【BFS】

Rescue Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 16314    Accepted Submission(s): 5926 Problem Description Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The prison is

HDU 1253 胜利大逃亡 NYOJ 523【BFS】

胜利大逃亡 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 24608    Accepted Submission(s): 9427 Problem Description Ignatius被魔王抓走了,有一天魔王出差去了,这可是Ignatius逃亡的好机会. 魔王住在一个城堡里,城堡是一个A*B*C的立方体,可以被表示成A个B*C的

NYOJ 284 坦克大战 【BFS】+【优先队列】

坦克大战 时间限制:1000 ms  |  内存限制:65535 KB 难度:3 描述 Many of us had played the game "Battle city" in our childhood, and some people (like me) even often play it on computer now. What we are discussing is a simple edition of this game. Given a map that co

【BFS】uva11624Fire!

/* bfs宽度遍历 -------------------------------------------------------------------------- 对人和火同时进行bfs,,注意应该先火后人,即如果在人到达该格子前,格子已经着火 则不应该走,最后人走到边界无路可走,则IMPOSSIBLE!!!!!!!!!!!! --------------------------------------------------------------------------- 两次bfs

【bfs】hdu 1104 Remainder

[bfs]hdu 1104 Remainder 题目链接:hdu 1104 Remainder 很不错的一道搜索题目,但是有几个关键问题要注意. 最短路径,果断bfs+Queue 路径的存储问题,之前只想把每一步的计算结果存储到queue(int)Q中,后来发现路径无法记录,就选择存储节点的方式并用string保存路径,queue(node)Q,开一个临时的节点node p,每进行一次运算就更新它的路径string+'op',最终输出的一定是完整路径!! 但最关键的是取模!!!!! discus

Poj 3087 Shuffle&#39;m Up 【BFS】

Shuffle'm Up Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6556 Accepted: 3077 Description A common pastime for poker players at a poker table is to shuffle stacks of chips. Shuffling chips is performed by starting with two stacks of pok

【bfs】【中等难度】wikioi3055 青铜莲花池

3055 青铜莲花池 题目描述 Description 为了让奶牛们娱乐和锻炼,农夫约翰建造了一个美丽的池塘.这个长方形的池子被分成了M 行N 列个方格(1 ≤ M, N ≤ 30).一些格子是坚固得令人惊讶的莲花,还有一些格子是岩石,其余的只是美丽.纯净.湛蓝的水.贝西正在练习芭蕾舞,她站在一朵莲花上,想跳到另一朵莲 花上去,她只能从一朵莲花跳到另一朵莲花上,既不能跳到水里,也不能跳到岩石上.      贝西的舞步很像象棋中的马步:每次总是先横向移动M1 (1 ≤ M1 ≤ 30)格,再纵向移