Description

一个正方形的镇区分为 N2 个小方块(1 <= N <= 7)。农场位于方格的左上角,集市位于左下角。贝茜穿过小镇,从左上角走到左下角,刚好经过每个方格一次。当 N=3 时,贝茜的漫游路径可能如下图所示:

----------------
| | | |
| F********** |
| | | * |
------------*---
| | | * |
| ***** | * |
| * | * | * |
---*---*----*---
| * | * | * |
| M | ****** |
| | | |
----------------

写一个程序,对于给出的 N 值,计算贝茜从农场走到集市有多少种唯一的路径。

Input

行 1: 一个整数 N (1 <= N <= 7)

Output

只有一行。输出一个整数表示唯一路径的数量。

Sample Input

3

Sample Output

2

HINT

题解:四个方向搜索,用sum记录走过的格子数,当搜索到M时,若sum==m*m-1时ans++;当n==7时,时间比较长,特判一下就ok了,剪枝不会呵呵;

#include<cstdio>
#include<cstring>
#include<stack>
#include<iostream>
#include<queue>
#include<algorithm>
#include<map>
#include<vector>
#define PI acos(-1.0)
using namespace std;
typedef long long ll;
int m,n,cnt=,sum=;
char str[][];
int visit[][];
int dis[][];
int di[][]= {{-,},{,},{,-},{,}};
map<ll,ll>::iterator it;
int Scan()
{
int res = , flag = ;
char ch;
if ((ch = getchar()) == '-')
{
flag = ;
}
else if(ch >= '' && ch <= '')
{
res = ch - '';
}
while ((ch = getchar()) >= '' && ch <= '')
{
res = res * + (ch - '');
}
return flag ? -res : res;
}
void init()
{
for(int i=; i<=m; i++)
{
for(int j=; j<=m; j++)
{
str[i][j]='.';
}
}
}
int judge(int x,int y)
{
if(x>=&&y>=&&x<=m&&y<=m)
{
return ;
}
return ;
}
void dfs(int x,int y)
{
//cout<<sum<<endl;
if(x==m&&y==&&sum==m*m-)
{
cnt++;
return ;
}
if(judge(x+,y))
{
if(str[x+][y]=='.')
{
str[x+][y]='#';
sum++;
dfs(x+,y);
str[x+][y]='.';
sum--;
}
}
if(judge(x-,y))
{
if(str[x-][y]=='.')
{
sum++;
str[x-][y]='#';
dfs(x-,y);
str[x-][y]='.';
sum--;
}
}
if(judge(x,y-))
{
if(str[x][y-]=='.')
{
sum++;
str[x][y-]='#';
dfs(x,y-);
str[x][y-]='.';
sum--;
}
}
if(judge(x,y+))
{
if(str[x][y+]=='.')
{
sum++;
str[x][y+]='#';
dfs(x,y+);
str[x][y+]='.';
sum--; }
}
}
int main()
{
cin>>m;
init();
str[][]='#';
if(m==)
cnt=;
else if(m==)
cnt=;
else dfs(,);
cout<<cnt<<endl;
}

Betsy's Tour 漫游小镇(dfs)的更多相关文章

  1. USACO 6.5 Betsy's Tour (插头dp)

    Betsy's TourDon Piele A square township has been divided up into N2 square plots (1 <= N <= 7) ...

  2. USACO 5.4 Betsy's Tour(暴力)

    水过,水过,这个程序跑7,跑5分钟左右把... /* ID: cuizhe LANG: C++ TASK: betsy */ #include <iostream> #include &l ...

  3. USACO 6.5 章节 世界上本没有龙 屠龙的人多了也便有了

    All Latin Squares 题目大意 n x n矩阵(n=2->7) 第一行1 2 3 4 5 ..N 每行每列,1-N各出现一次,求总方案数 题解 n最大为7 显然打表 写了个先数值后 ...

  4. Codeforces Round #606 (Div. 1) Solution

    从这里开始 比赛目录 我菜爆了. Problem A As Simple as One and Two 我会 AC 自动机上 dp. one 和 two 删掉中间的字符,twone 删掉中间的 o. ...

  5. 【dfs or 最短路】【HDU1224】【Free DIY Tour】

    路径只能由小序号到大序号..(起点可以视为最小的序号和最大的序号) 问怎么走 happy值最大.. DFS N=100 且只能小序号到大序号 显然dfs可以过.. 但是存路径的时候sb了.....应该 ...

  6. soj 1015 Jill's Tour Paths 解题报告

    题目描述: 1015. Jill's Tour Paths Constraints Time Limit: 1 secs, Memory Limit: 32 MB Description Every ...

  7. POJ3621Sightseeing Cows[01分数规划 spfa(dfs)负环 ]

    Sightseeing Cows Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9703   Accepted: 3299 ...

  8. POJ 1637 Sightseeing tour (混合图欧拉回路)

    Sightseeing tour   Description The city executive board in Lund wants to construct a sightseeing tou ...

  9. 【SPOJ】1825. Free tour II(点分治)

    http://www.spoj.com/problems/FTOUR2/ 先前看了一会题解就自己yy出来了...对拍过后交tle.................. 自己造了下大数据........t ...

随机推荐

  1. Node大文件处理

    之前有个需求要将文件解析再处理,当时直接将整个文件内容读到内存中然后解析,也是没有考虑到大文件的问题,那么要如何解析大文件呢? 输入:文件的内容是多个json,按顺序排列 输出:解析后的json数据 ...

  2. css选择器的特殊性值

    今天从前端那拿来写好的页面,就开始动工,首先,照旧处理导航栏高亮的问题, 说到处理高亮的问题,不同的人会有不同的方法,比如: //类名为nav的元素下的第n个a元素 .nav a:nth-of-typ ...

  3. 《Drools7.0.0.Final规则引擎教程》第4章 4.2 lock-on-active

    lock-on-active 当在规则上使用ruleflow-group属性或agenda-group属性的时候,将lock-on-active 属性的值设置为true,可避免因某些Fact对象被修改 ...

  4. 专业工具软件PCB板打印说明

    专业工具软件PCB板打印说明 请注意PCB板打印不要直接截图,如下方式是不正确的: 这样在打印为黑白图片时,元器件之间的连线无法看清. 应采用如下模式: ~End~

  5. 事件穿透父层 直达子层 pointer-events:none

    之前我也做过一些canvas特效,往往在canvas全屏时,canvas下层的div就无法进行dom的事件操作,点击之类的就失灵了.之前我的做法要么就是在canvas上加入点击事件,穿透到下层,或者把 ...

  6. keras中自定义Layer

    最近在学习SSD的源码,其中有两个自定的层,特此学习一下并记录. import keras.backend as K from keras.engine.topology import InputSp ...

  7. C#检测应用程序重复启动----函数检测(可以在多用户登录情况下检测)

    上文是在网上找的检测程序重复运行的类,但是感觉不是很好用,而且还使用了API,似乎完全没有必要,于是晚上自己写了一个函数,经过测试,在多用户下仍然可以检测到程序的多次运行.当然,如果程序改了名字还是可 ...

  8. 笔记:开源协议 Apache 2 和 GPL 兼容

    笔记:开源协议 Apache 2 和 GPL 兼容 Apache 2 和 GPL v3 兼容. GPL 分了很多版本,LGPL 为最宽松的 GPL,而 AGPL 为最严格的 GPL 协议. Linux ...

  9. Excel 2007 打开 UTF-8 编码 CSV 文件的乱码BUG

    http://blog.sina.com.cn/s/blog_6c3b65fd01018dgq.html 打开UTF-8编码的CSV方法: 1) 打开Excel 2007 2) 执行“数据”-> ...

  10. 几个ssh和sftp的命令

    sudo apt-get install openssh-server 装server sudo /etc/init.d/ssh stop sudo /etc/init.d/ssh start sud ...