Problem L: The basics of search go away

Problem L: The basics of search go away

Time Limit: 1 Sec  Memory Limit: 128 MB
Submit: 134  Solved: 91
[ Submit ][ Status ][ Web Board ]

Description

 

 

Horses move in Chinese chess rules in Japanese glyphs.

Please write a program, given a chessboard of size n*m, and the initial position of the horse (x, y), requiring that the same point on the chessboard cannot be repeatedly passed, and calculate how many ways the horse can traverse all the points on the chessboard.

 

Input

 

The first row is an integer T (T < 10), which represents the number of test data sets.
Each set of test data contains a row, which is four integers, which are the size of the chessboard and the initial position coordinates n, m, x, y. (0<=x<=n-1,0<=y<=m-1, m < 10, n < 10)

Output

 

Each set of test data contains one row, which is an integer, indicating the total number of ways that the horse can traverse the board, and 0 means that it cannot traverse once.

Sample Input

1
5 4 0 0

Sample Output

32
#include<stdio.h>
#include<string.h>
int dx[9]={0,1,1,-1,-1,2,2,-2,-2};
int dy[9]={0,2,-2,2,-2,-1,1,1,-1};
int a[111][111];
int t,n,m,x,y,step,sum;

int dfs(int x,int y,int step)
{
    int l;
    if(step>=m*n)
    {
        sum++;
        return 0;
    }
    for(l=1;l<=8;l++)
    {
        int x1=x+dx[l];int y1=y+dy[l];
        if(x1<n&&y1<m&&x1>=0&&y1>=0&&a[x1][y1]==0)
        {
            a[x1][y1]=1;
            dfs(x1,y1,step+1);
            a[x1][y1]=0;
        }
    }
}
intmain()
{
    int i;
    scanf("%d",&t);
    for(i=1;i<=t;i++)
    {
        scanf("%d%d%d%d",&n,&m,&x,&y);
        a[x][y]=1;
        dfs(x,y,1);
        printf("%d\n",sum);
        memset(a,0,sizeof(a));
        sum=0;
    }
}

  

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325020843&siteId=291194637