马的走法

马的走法

Time Limit : 3000/1000ms (Java/Other)   Memory Limit : 65535/32768K (Java/Other)
Total Submission(s) : 40   Accepted Submission(s) : 14
Font: Times New Roman | Verdana | Georgia
Font Size:  

Problem Description

在一个4×5的棋盘上,求马能返回初始位置的所有不同走法的总数(马走过的位置不能重复,马走“日”字)。

Input

从输入文件中读入数据。文件的第一行马的初始位置的个数n,后面n行是初始位置坐标。

Output

对于每个初始位置给出走法总数,如果不能回到初始位置,输出“ERROR”。

Sample Input

1
2 2

Sample Output

4596
#include<string.h>
#include <stdio.h>
#include <iostream>
using namespace std;
int g[5][6];
int d[8][2]={2,1,1,2,-1,2,-2,1,-2,-1,-1,-2,1,-2,2,-1};
int Sx,Sy;
int cnt;
void DFS(int x,int y)
{
    int i,newx,newy;
    for (i=0;i<8;i++)
    {
        newx=x+d[i][0];
        newy=y+d[i][1];
        if (newx>=1&&newx<=4&&newy>=1&&newy<=5)
        {
            if (newx==Sx&&newy==Sy) cnt++;
            else if (g[newx][newy]==0)
            {
               g[newx][newy]=1;
               DFS(newx,newy);
               g[newx][newy]=0;
            }
        }
    }
}
int main()
{ int t;
cin>>t;
while(t--)
{  cin>>Sx>>Sy;
if (Sx>=1&&Sx<=4&&Sy>=1&&Sy<=5)
   {
       memset(g,0,sizeof(g));
    cnt=0;
    g[Sx][Sy]=1;
    DFS(Sx,Sy);
    if (cnt)
        cout<<cnt<<endl;
    else
        cout<<"ERROR"<<endl;
   }
   else
    cout<<"ERROR"<<endl;
}


    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值