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. listView使用小技巧P66--P76

    1.设置分割线高度和颜色 android:divider="@android:color/darker_gray" android:dividerHeight="10dp ...

  2. LeetCode OJ:Valid Anagram(有效字谜问题)

    Given two strings s and t, write a function to determine if t is an anagram of s. For example,s = &q ...

  3. Application的作用

    Application可实现数据共享 例如: 一.新建一个空的工程,并新建一个App类,继承自Application public class App extends Application { pr ...

  4. react bootstrap-loader

    1.install npm install bootstrap-loader jquery resolve-url-loader 2.webpack.config.js entry: ['bootst ...

  5. CDlinux无线破解系统

    cdlinux是一款功能非常强大的无线密码破解器,cdlinux兼容pin软件,不用抓取握手包.不用跑字典.不用客户端在线就能够破解无线路由器的密码,轻轻松松帮助用户达到蹭网的目的. 基本简介 cdl ...

  6. Spring中的c3p0配置

    转载请注明出处:http://blog.csdn.net/l1028386804/article/details/51162560 今天,我们就来详细谈谈Spring中的c3p0配置问题,好了,不耽搁 ...

  7. C# 操作嵌入的资源

    被嵌入的程序执行文件 namespace AppTest { class Program { static void Main(string[] args) { Console.WriteLine(& ...

  8. C#网络编程(接收文件) - Part.5

    这篇文章将完成 Part.4 中剩余的部分,它们本来是一篇完整的文章,但是因为上一篇比较长,合并起来页数太多,浏览起来可能会比较不方便,我就将它拆为两篇了,本文便是它的后半部分.我们继续进行上一篇没有 ...

  9. [Client] looks like we got no XML document in....

    无wsdl方式应用webservice时,服务端包含了 include_once'inc/utility_all.php '一直报[Client] looks like we got no XML d ...

  10. Dedesql数据库类详解(二次开发必备教程)(转)

    http://www.dedecms.com/help/development/2009/1028/1076.html 织梦DedeCMS的二次开发不仅仅是会写写织梦的标签,会制作织梦的模板.很多时候 ...