Betsy's Tour 漫游小镇(dfs)
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)的更多相关文章
- 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) ...
- USACO 5.4 Betsy's Tour(暴力)
水过,水过,这个程序跑7,跑5分钟左右把... /* ID: cuizhe LANG: C++ TASK: betsy */ #include <iostream> #include &l ...
- USACO 6.5 章节 世界上本没有龙 屠龙的人多了也便有了
All Latin Squares 题目大意 n x n矩阵(n=2->7) 第一行1 2 3 4 5 ..N 每行每列,1-N各出现一次,求总方案数 题解 n最大为7 显然打表 写了个先数值后 ...
- Codeforces Round #606 (Div. 1) Solution
从这里开始 比赛目录 我菜爆了. Problem A As Simple as One and Two 我会 AC 自动机上 dp. one 和 two 删掉中间的字符,twone 删掉中间的 o. ...
- 【dfs or 最短路】【HDU1224】【Free DIY Tour】
路径只能由小序号到大序号..(起点可以视为最小的序号和最大的序号) 问怎么走 happy值最大.. DFS N=100 且只能小序号到大序号 显然dfs可以过.. 但是存路径的时候sb了.....应该 ...
- soj 1015 Jill's Tour Paths 解题报告
题目描述: 1015. Jill's Tour Paths Constraints Time Limit: 1 secs, Memory Limit: 32 MB Description Every ...
- POJ3621Sightseeing Cows[01分数规划 spfa(dfs)负环 ]
Sightseeing Cows Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9703 Accepted: 3299 ...
- POJ 1637 Sightseeing tour (混合图欧拉回路)
Sightseeing tour Description The city executive board in Lund wants to construct a sightseeing tou ...
- 【SPOJ】1825. Free tour II(点分治)
http://www.spoj.com/problems/FTOUR2/ 先前看了一会题解就自己yy出来了...对拍过后交tle.................. 自己造了下大数据........t ...
随机推荐
- 点击bindingNavigatorAddNewItem 关联的dataGridView不会新增一行
方法一. 在设计界面,修改bindingNavigator1的属性AddNewItem 为“(无)”: 方法二. 在设计器自动生成的代码中找到这一行: // bindingNa ...
- 剑指offer--34.数字在排序数组中出现的次数
时间限制:1秒 空间限制:32768K 热度指数:209611 本题知识点: 数组 题目描述 统计一个数字在排序数组中出现的次数. class Solution { public: int GetNu ...
- http keep - alive 与 长连接
http1.0 2.0 1.1区别 你可以把 WebSocket 看成是 HTTP 协议为了支持长连接所打的一个大补丁,它和 HTTP 有一些共性,是为了解决 HTTP 本身无法解决的某些问题而做出的 ...
- Http权威指南(报文)
1.报文的组成 报文由 起始行.首部.主体(可选)组成 请求报文: <method> <request-URL> <version> <headers> ...
- HAWQ取代传统数仓实践(十二)——维度表技术之分段维度
一.分段维度简介 在客户维度中,最具有分析价值的属性就是各种分类,这些属性的变化范围比较大.对某个个体客户来说,可能的分类属性包括:性别.年龄.民族.职业.收入和状态,例如,新客户.活跃客户.不活跃客 ...
- 使用WPScan破解wordpress站点密码
我这里使用的Kali Linux,它默认安装了WPScan. 在使用WPScan之前,先更新它的漏洞数据库: # wpscan –update 扫描wordpress用户 wpscan -–url [ ...
- rem第一天
Rem为单位 CSS3的出现,他同时引进了一些新的单位,包括我们今天所说的rem.在W3C官网上是这样描述rem的——“font size of the root element” .下面我们就一起来 ...
- POJ1251 Jungle Roads
解题思路:看懂题意是关键,Kruskal算法,最小生成树模板. 上代码: #include<cstdio> #include<cstring> #include<algo ...
- 转载的,讲解java.util的集合类
本文是转载的 http://www.ibm.com/developerworks/cn/java/j-lo-set-operation/index.html#ibm-pcon 在实际的项目开发中会有很 ...
- HDU - 6430:TeaTree (线段树合并)
Recently, TeaTree acquire new knoledge gcd (Greatest Common Divisor), now she want to test you. As w ...