Blue Bridge Cup Daily One Question 1.3 2017 Provincial Competition Group A 1. Maze [DFS]

Original title:

http://oj.ecustacm.cn/problem.php?id=1317

A maze playground on Planet X is built on a small hillside. It is composed of 10x10 interconnected small rooms. 
There is a big letter written on the floor of the room. We assume that the player is standing facing uphill, then: 
L means going to the room on the left, R means going to the room on the right, U means going to the room on the uphill direction, and D means going to the room on the downhill direction. 
The inhabitants of Planet X are a bit lazy and don't want to think hard. They prefer to play games of luck. The same is true for this game! 
At the beginning, the helicopter put 100 players into small rooms. The player must move according to the letters on the ground. 
The maze map is as follows: 
------------ 
UDDLUULRUL 
UURLLLRRRU 
RRUURLDLRD 
RUDDDDUUUU 
URUDLLRRUU 
DURLRLDLRL 
ULLURLLRDU 
RDLULLRDDD 
UUDDUDUDLL 
ULRDLUURRR 
------------ 
Please calculate it. Finally, how many players will come out Labyrinth? Instead of going around in circles. 

Output

Output an integer to indicate the answer 

prompt

For easy understanding, please refer to this picture 

https://blog.csdn.net/weixin_43914593/article/details/112132315 

(1) It can be counted directly

(2) DFS [recursive writing]

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<set>
#include<queue>
#include<stack>
using namespace std;
typedef long long ll;
const int maxn = 110000;

char map[15][15];
int ans=0;
int vis[15][15];

void dfs(int x,int y)
{
    if(x<0||x>9||y<0||y>9)
    {
        ans++;
        return;
    }
    if(vis[x][y]==0)
    {
        vis[x][y]=1;//!!!!!!!
        if(map[x][y]=='L')
        {
            dfs(x,y-1);
        }
        if(map[x][y]=='R')
        {
            dfs(x,y+1);
        }
        if(map[x][y]=='U')
        {
            dfs(x-1,y);
        }
        if(map[x][y]=='D')
        {
            dfs(x+1,y);
        }
        return ;
    }
    return;
}

int main()
{
    for(int i=0;i<10;i++)
    {
        for(int j=0;j<10;j++)
        {
            cin>>map[i][j];
        }
    }
    for(int i=0;i<10;i++)
    {
        for(int j=0;j<10;j++)
        {
            memset(vis,0,sizeof(vis));
            dfs(i,j);
        }
    }
    cout<<ans;
    return 0;
}

 

Guess you like

Origin blog.csdn.net/qq_43660826/article/details/112978054