2017 Blue Bridge Cup Maze (dfs)

A labyrinth playground on Planet X is built on a hillside.
It is composed of 10x10 interconnected small rooms.

A large letter was written on the floor of the room.
We assume that the player is standing facing uphill, then:
L means walking to the left room,
R means walking to the right room,
U means walking uphill, and
D means walking downhill.

The inhabitants of Planet X are a bit lazy and unwilling 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 a small room.
Players must move according to the letters on the ground.

The map of the maze is as follows:
------------
UDDLUULRUL
UURLLLRRRU
RRUURLDLRD
RUDDDDUUUU
URUDLLRRUU
DURLRLDLRL
ULLURLLRDU
RDLULLRDDD
UUDDUDUDLL
ULRDLUURRR
------------

Please calculate, in the end, how many players will get out of the maze? 
Instead of going around in circles.

 

Note the return value of the operator after overloading.

Answers to this question 31

#include<bits/stdc++.h>


char maze[10][10];

int vis[10][10];
struct walk{
	int a,b;
	walk operator +(const walk& rh)const{
		walk s{0,0};
		s.a=this->a+rh.a;
		s.b=this->b+rh.b;
		return s;
	}
};

walk to_digit(char a){
	if(a=='L')
	return (walk){0,-1};
	if(a=='R')
	return (walk){0,1};
	if(a=='U')
	return (walk){-1,0};
	if(a=='D')
	return (walk){1,0};
}

bool dfs(walk &it){
	while(1){
		if(vis[it.a][it.b]==1)
		return false;
	
		vis[it.a][it.b]=1;
		it=it+to_digit(maze[it.a][it.b]);
  		
		if(it.a>9 || it.b>9 || it.a<0 || it.b<0) 
		return true;
    }
}



int main(){
	for(int i=0;i<10;i++)
	scanf("%s",maze[i]);
	int ans=0;
	int i,j;
	for(i=0;i<10;i++)
		for( j=0;j<10;j++){
		memset(vis,0,sizeof(vis));
		walk p{i,j};
		if(dfs(p))
		ans++;
		}		
	printf("%d",ans);
		
}


 

Published 75 original articles · praised 77 · views 4017

Guess you like

Origin blog.csdn.net/weixin_43568895/article/details/104878671
Recommended