Blue Bridge Cup---Go Grid (dfs)

Go square

Problem description
There are some two-dimensional lattices on the plane. The numbering of these points is like the numbering of a two-dimensional array, from top to bottom from the 1st to the nth row, from left to right from the 1st to the mth column. Each point can be represented by a row number and a column number. Said. Now someone is standing in row 1 and column 1 and has to go to row n and column m. You can only go to the right or down. Note that if the row number and column number are both even numbers, you cannot enter this grid. Ask how many options are there.

Input format The
input line contains two integers n, m.

Output format
Output an integer to indicate the answer.

Sample input

3 4

Sample output

2

Sample input

6 6

Sample output

0

Test data
For all evaluation cases, 1 ≤ n ≤ 30, 1 ≤ m ≤ 30.
Idea: dfs, dfs belong to the common knowledge points of the Blue Bridge Cup! ! !
code:

#include<stdio.h>
#include<string.h> 
#define   INF    99
int ans=0;
int dir[2][2]={
    
    {
    
    0,1},{
    
    1,0}};
int M[INF][INF];
int n,m;

void dfs(int x,int y){
    
    
	if(x<1||y<1||x>m||y>n){
    
    
		return ;
	}
	if(x==m&&y==n){
    
    
		ans++;
	}
	
	for(int i=0;i<2;i++){
    
    
		int xx=x+dir[i][0];
		int yy=y+dir[i][1];
		
		if(xx%2==0&&yy%2==0){
    
    
			continue;
		}
		
		M[xx][yy]=1;
		dfs(xx,yy);
		M[xx][yy]=0;
	}
}

int main(){
    
    
	
	scanf("%d %d",&n,&m);
	memset(M,0,sizeof M);
	dfs(1,1);
	printf("%d",ans);
	return 0;
}

Guess you like

Origin blog.csdn.net/timelessx_x/article/details/115008083