27 lines of code AC_maze 2017 8th Blue Bridge Cup Group A Question 1 (violence, imitation maze)

Title description

A maze playground on Planet X is built on a small hill.
It is made up 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 uphill,
D means going to the room downhill.

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, in the end, how many players will get out of the maze?
Instead of going around in circles.


Analysis and thinking

At first glance, I thought it was a search. After reading the question, I found it was a violent enumeration question .

Ideas:
first think about, if the size of the maze is n, then only go up to a certain point in the maze n*nstep (serpentine), therefore, when traversing each point, record the number of steps with num, num possible when more than n*n+5after, this road show is no way to go out, on the contrary, if during the traversal ior jcross-border, then you can go out. Finally, the total number can be counted.

Improve:

  1. You can set a two-dimensional int array equal to the size of the maze, set all to 0, if a point can pass, then set 1 , so that before traversing each point, first determine whether the corresponding int array is equal to 1, if it is equal to 1. End the cycle directly, which will greatly improve efficiency.
  2. Use define to define the size of the array, you can easily adjust the size of the array during testing.

Code display

#include<bits/stdc++.h>
#define N 10
using namespace std;
char a[N][N];			//记录迷宫 
int b[N][N];			//如果该点可以走通,则置1。 
int main() {
    
    
	for(int i = 0; i < N; i++)  cin>>a[i]; 	//输入迷宫
	 
	memset(b, 0, sizeof(b));
	int sum1 = 0;		//记录能走出去的次数 
	
	for(int i = 0; i < N; i++) 	//遍历 
		for(int j = 0; j < N; j++) {
    
    
			int sum = 0;
			int i1=i, j1=j;		//代替i,j进行值的变化 
			while(sum < N*N+10) {
    
    	 
				if(b[i1][j1]==1) break;	//如果走到的这个点 b[i][j]=1,则代表能走出去 
				sum++;
				if(a[i1][j1] == 'U') i1--;
				else if(a[i1][j1] == 'D') i1++;
				else if(a[i1][j1] == 'L') j1--;
				else if(a[i1][j1] == 'R') j1++;
				
				if(i1<0 || i1>=N || j1<0 || j1>=N)  break;
			}
			if(sum != N*N+10) {
    
     sum1++; b[i][j]=1; } 	//如果sum遍历到头,则说明走不出去 
		}
	cout << sum1 << endl;
return 0; } 

This world is that some people are always working day and night, while others find that the world has changed when they wake up. Come on, stranger!

Guess you like

Origin blog.csdn.net/weixin_43899069/article/details/108822570