zoukankan      html  css  js  c++  java
  • 4123:马走日

    [题目链接]
    总时间限制: 1000ms 内存限制: 1024kB
    描述

    马在中国象棋以日字形规则移动。

    请编写一段程序,给定n*m大小的棋盘,以及马的初始位置(x,y),要求不能重复经过棋盘上的同一个点,计算马可以有多少途径遍历棋盘上的所有点。

    输入
    第一行为整数T(T < 10),表示测试数据组数。
    每一组测试数据包含一行,为四个整数,分别为棋盘的大小以及初始位置坐标n,m,x,y。(0<=x<=n-1,0<=y<=m-1, m < 10, n < 10)
    输出
    每组测试数据包含一行,为一个整数,表示马能遍历棋盘的途径总数,0为无法遍历一次。
    样例输入
    1
    5 4 0 0
    样例输出
    32
     1 #include<iostream>  
     2 #include<cstring>  
     3 #include<cstdio>  
     4 using namespace std;  
     5 int sx[8]={1,1,-1,-1,2,2,-2,-2};  
     6 int sy[8]={2,-2,2,-2,1,-1,1,-1};  
     7 int t,n,m,x,y,ans;  
     8 bool b[20][20];  
     9 void dfs(int dep,int s,int t)
    10 {  
    11     if(dep==n*m)
    12     {  ans++; return; }  
    13     for (int r=0;r<8;++r)
    14     {  
    15         int x=s+sx[r]; int y=t+sy[r];  
    16         if (!b[x][y]&&x>0&&y>0&&x<=n&&y<=m)
    17         {  
    18             b[x][y]=true;  
    19             dfs(dep+1,x,y);  
    20             b[x][y]=false;  
    21         }
    22     }
    23 }
    24 int main()
    25 {  
    26     scanf("%d",&t);  
    27     while (t--)
    28     {  
    29         scanf("%d%d",&n,&m);  
    30         scanf("%d%d",&x,&y); 
    31         ++x; ++y;  
    32         memset(b,0,sizeof(b));
    33         ans=0,b[x][y]=true;   
    34         dfs(1,x,y);  
    35         printf("%d
    ",ans);  
    36     }  
    37 }  
  • 相关阅读:
    hdu1003 最大子串和
    cf339d Xenia and Bit Operations
    A + B Problem II
    中国近代史纲要----王洪兵--2016年春季学期----中国海洋大学
    CodeForces 35D Animals
    CodeForces 558D
    Vanya and Brackets
    spfa
    hdu 1217 Arbitrage
    CodeForces 1A Theatre Square
  • 原文地址:https://www.cnblogs.com/huashanqingzhu/p/7587641.html
Copyright © 2011-2022 走看看