码迷,mamicode.com
首页 > 其他好文 > 详细

maze

时间:2015-09-23 17:01:44      阅读:200      评论:0      收藏:0      [点我收藏+]

标签:

时限:2000MS     内存:65536KB     64位IO格式:%I64d & %I64u
提交 状态 练习
指定题目版本:

问题描述

Acm, a treasure-explorer, is exploring again. This time he is in a special maze, in which there are some doors (at most 5 doors, represented by ‘A‘, ‘B‘, ‘C‘, ‘D‘, ‘E‘ respectively). In order to find the treasure, Acm may need to open doors. However, to open a door he needs to find all the door‘s keys (at least one) in the maze first. For example, if there are 3 keys of Door A, to open the door he should find all the 3 keys first (that‘s three ‘a‘s which denote the keys of ‘A‘ in the maze). Now make a program to tell Acm whether he can find the treasure or not. Notice that Acm can only go up, down, left and right in the maze.

输入

The input consists of multiple test cases. The first line of each test case contains two integers M and N (1 < N, M < 20), which denote the size of the maze. The next M lines give the maze layout, with each line containing N characters. A character is one of the following: ‘X‘ (a block of wall, which the explorer cannot enter), ‘.‘ (an empty block), ‘S‘ (the start point of Acm), ‘G‘ (the position of treasure), ‘A‘, ‘B‘, ‘C‘, ‘D‘, ‘E‘ (the doors), ‘a‘, ‘b‘, ‘c‘, ‘d‘, ‘e‘ (the keys of the doors). The input is terminated with two 0‘s. This test case should not be processed.

输出

For each test case, in one line output "YES" if Acm can find the treasure, or "NO" otherwise.

样例输入

4 4 
S.X. 
a.X. 
..XG 
.... 
3 4 
S.Xa 
.aXB 
b.AG 
0 0

样例输出

YES 
NO

题意:给你一个row * col的迷宫,‘.’表示可走的空地,‘X’表示不可走的围墙,‘S’表示起点,‘G’表示宝藏的地点(终点),‘A‘~‘E‘分别表示5种类型的门,‘a‘~‘e‘分别对应表示5种类型门的钥匙。要想打开某种类型的门,只有拿到了迷宫内其对应的所有钥匙。问你最终能不能拿到这份宝藏?

 思路:刚开始深搜一直TLE  然后换了个广搜==  悲剧的是广搜又TLE了 技术分享    最后经大神指导发现如果无路可走了而且在门打不开的情况下会有死循环 。。。。

#include <iostream>
#include <queue>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <cstdlib>
using namespace std;
typedef long long LL;
const LL INF = 0xfffffff;
const int maxn = 50;
const LL MOD = 1e9+7;
char str[maxn][maxn];
int dir[4][2] = {{1,0},{-1,0},{0,1},{0,-1}}, ok;
int n, m, count_num[maxn], vis[maxn][maxn], sx, sy;
/**< count_num 钥匙的数量  vis标记 */
struct da
{
    int x, y;
};
int bfs()
{
    queue<da>Q;
    da q, w;
    q.x = sx, q.y = sy;
    Q.push(q);
    int time = 0;/**< n,m 都是小于20的 用time来限制时间 */
    while(!Q.empty()&& time < 1000)
    {
        q = Q.front();
        Q.pop();time++;
        if(str[q.x][q.y] >= ‘A‘ && str[q.x][q.y] <= ‘E‘)
        {
            if(count_num[str[q.x][q.y]-‘A‘+1] == 0)/**< 如果是门并且钥匙够了 可以走 */
                vis[q.x][q.y] = 1;
            else /**< 否则就不能走 */
            {
               // count_num[str[q.x][q.y]-‘A‘+1]--;
                Q.push(q);
                continue;
            }
        }
        for(int i = 0; i < 4; i++)
        {
            w = q;
            w.x += dir[i][0];
            w.y += dir[i][1];
            if(w.x >=1 && w.x <= n && w.y >= 1 && w.y <= m && !vis[w.x][w.y])
            {
                if(str[w.x][w.y] == ‘G‘) return 1;
                if(str[w.x][w.y] >= ‘A‘ && str[w.x][w.y] <= ‘E‘)
                    Q.push(w);
                if(str[w.x][w.y] >= ‘a‘ && str[w.x][w.y] <= ‘e‘)
                {
                    count_num[str[w.x][w.y]-‘a‘+1] --;/**< 找到一个钥匙 开这个门所需要的钥匙就-1 */
                    vis[w.x][w.y] = 1;
                    Q.push(w);
                }
                if(str[w.x][w.y] == ‘.‘)
                {
                    vis[w.x][w.y] = 1;
                    Q.push(w);
                }
            }
        }
    }
    return 0;
}
int main()
{
    int i, j;
    while(scanf("%d %d", &n, &m), n+m)
    {
        ok = 0;
        memset(count_num, 0, sizeof(count_num));
        memset(vis, 0, sizeof(vis));
        for(i = 1; i <= n; i++)
        {
            scanf("%s", str[i]+1);
            for(j = 1; j <= m; j++)
            {
                if(str[i][j] >= ‘a‘ && str[i][j] <= ‘e‘)
                    count_num[str[i][j]-‘a‘+1] ++;
                if(str[i][j] == ‘S‘)
                    sx = i, sy = j;
            }
        }
        vis[sx][sy] = 1;
        ok = bfs();
        if(ok) printf("YES\n");
        else printf("NO\n");
    }
    return 0;
}

  

 

maze

标签:

原文地址:http://www.cnblogs.com/PersistFaith/p/4832417.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!