zoukankan      html  css  js  c++  java
  • [蓝桥杯2017初赛]迷宫 DFS

    题目描述

    X星球的一处迷宫游乐场建在某个小山坡上。它是由10x10相互连通的小房间组成的。
    房间的地板上写着一个很大的字母。我们假设玩家是面朝上坡的方向站立,则:
    L表示走到左边的房间,R表示走到右边的房间,U表示走到上坡方向的房间,D表示走到下坡方向的房间。
    X星球的居民有点懒,不愿意费力思考。他们更喜欢玩运气类的游戏。这个游戏也是如此!
    开始的时候,直升机把100名玩家放入一个个小房间内。玩家一定要按照地上的字母移动。
    迷宫地图如下:
    ------------
    UDDLUULRUL
    UURLLLRRRU
    RRUURLDLRD
    RUDDDDUUUU
    URUDLLRRUU
    DURLRLDLRL
    ULLURLLRDU
    RDLULLRDDD
    UUDDUDUDLL
    ULRDLUURRR
    ------------
    请你计算一下,最后,有多少玩家会走出迷宫? 而不是在里边兜圈子。

    输出

    输出一个整数表示答案
     

    提示

    为方便理解,可参考此图

    答案:31

    DFS:

    #include<iostream>
    #include<queue>
    #include<algorithm>
    #include<set>
    #include<string.h>
    using namespace std;
    char a[15][15];
    int vis[15][15];
    int ans = 0;
    void dfs(int i, int j)
    {
        if (i < 0 || i>9 || j < 0 || j>9) //迷宫的出口
        {
            ans++;
            return;
        }
        else
        {
            if (vis[i][j])   return; //如果已经搜过,直接返回
    
            vis[i][j] = 1;   //标记已搜索
    
            if (a[i][j] == 'L')
                dfs(i, j - 1); 
            if (a[i][j] == 'R') 
                dfs(i, j + 1); 
            if (a[i][j] == 'U') 
                dfs(i - 1, j); 
            if (a[i][j] == 'D') 
                dfs(i + 1, j); 
        }
    }
    int main()
    {
        int i, j;
        for (i = 0; i < 10; i++)
            for (j = 0; j < 10; j++)
                cin >> a[i][j];
                
        for (i = 0; i < 10; i++)
            for (j = 0; j < 10; j++)
            {
                memset(vis, 0, sizeof(vis)); //搜索每一个房间时,初始化搜搜数组
                dfs(i, j);
            }
        cout << ans << endl;
        return 0;
    }

    暴力:

    #include<iostream>
    #include<queue>
    #include<algorithm>
    #include<set>
    #include<string.h>
    using namespace std;
    char a[15][15],c[15][15];
    int vis[15][15];
    int ans = 0;
    int main()
    {
    
        for (int i = 0; i < 10; i++)
        {
            for(int j=0;j<10;j++)
            {
                cin>>a[i][j];
                c[i][j]=a[i][j];
            }
        }
    
        for (int i = 0; i < 10; i++)
        {
            for (int j = 0; j < 10; j++)
            {
                int x = i, y = j;
                memset(vis, 0, sizeof(vis));
                while (1)
                {
                    
                    if (a[x][y] == 'U')
                    {
                        x = x - 1;
                        if (x < 0 || x>9 || y < 0 || y>9)
                        {
                            ans++;
                            c[i][j]='#';
                            break;
                        }
                        if (vis[x][y] == 0)
                            vis[x][y] = 1;
                        else
                            break;
                    }
                    if (a[x][y] == 'L')
                    {
                        y = y - 1;
                        if (x < 0 || x>9 || y < 0 || y>9)
                        {
                            ans++;
                            c[i][j]='#';
                            break;
                        }
                        if (vis[x][y] == 0)
                            vis[x][y] = 1;
                        else
                            break;
                    }
                    if (a[x][y] == 'R')
                    {
                        y = y + 1;
                        if (x < 0 || x>9 || y < 0 || y>9)
                        {
                            ans++;
                            c[i][j]='#';
                            break;
                        }
                        if (vis[x][y] == 0)
                            vis[x][y] = 1;
                        else
                            break;
                    }
                    if (a[x][y] == 'D')
                    {
                        x = x + 1;
                        if (x < 0 || x>9 || y < 0 || y>9)
                        {
                            ans++;
                            c[i][j]='#';
                            break;
                        }
                        if (vis[x][y] == 0)
                            vis[x][y] = 1;
                        else
                            break;
                    }
                    
                }
            }
        }
        cout << ans << endl;
        // for(int i=0;i<10;i++)
        // {
        //     for(int j=0;j<10;j++)
        //         cout<<c[i][j];
        //     cout<<endl;
        // }
        return 0;
    }
  • 相关阅读:
    第三期 预测——7.思考基于模型的方法
    第三期 预测——6.轨迹聚类2在线预测
    第三期 预测——5.数据驱动示例 轨迹簇
    第三期 预测——4.哪个最好
    第三期 预测——2.输入和输出
    第三期 预测——3 模型和数据驱动方法
    第三期 预测——1.简介
    状态和面向对象编程——7.课程大纲
    状态和面向对象编程——7.状态定量
    状态和面向对象编程——6.运动学
  • 原文地址:https://www.cnblogs.com/-citywall123/p/12316760.html
Copyright © 2011-2022 走看看