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 ...
随机推荐
- MySQL show processlist 执行状态分析
1.Sleep 通常代表资源未释放,如果是通过连接池,sleep状态应该恒定在一定数量范围内 实战范例:因前端数据输出时(特别是输出到用户终端)未及时关闭数据库连接,导致因网络连接速度产 ...
- Struts07---访问servlet的API
01.创建登录界面 <%@ page language="java" import="java.util.*" pageEncoding="UT ...
- 【android】下载文件至本应用程序的file目录或者sdcard
一.判断是否有sdcard卡 //判断是否有SD卡 //ture:有SD卡 //false:没有SD卡 public boolean avaiableMedia(){ String status ...
- Android 进阶15:HandlerThread 使用场景及源码解析
眼睛困得要死,但今天的计划不完成又怎么能睡呢?明日复明日,明日何其多啊! 读完本文你将了解: HandlerThread 简介 HandlerThread 源码 HandlerThread 的使用场景 ...
- 剑指offer-第五章优化时间和空间效率(两个链表的第一个公共节点)
思路1:要求的是两个链表的第一个公共节点,首先想到的是用栈来存放两个链表,然后依次从栈中抛出,直到最后一个相同的节点为止.但是要用到两个栈,空间复杂度为O(n): 思路2:从头到尾分别遍历两个链表得到 ...
- LG3195 [HNOI2008]玩具装箱TOY
题意 P教授要去看奥运,但是他舍不下他的玩具,于是他决定把所有的玩具运到北京.他使用自己的压缩器进行压缩,其可以将任意物品变成一堆,再放到一种特殊的一维容器中.P教授有编号为\(1\cdots N\) ...
- 洛谷 P3015 [USACO11FEB]最好的括号Best Parenthesis
传送门 题目大意:给出括号的得分标准. ()得分为1,如果A的得分为S(A),那么 (A)的得分为2*S(A). 题解:搜索 #include<iostream> #include< ...
- vue 相邻自定义组件渲染错误正确的打开方式
话不多说看问题: 当封装自定义组件时例如(自定义下拉列表)两个相同的组件在多次v-if变化时偶尔会发生渲染错误,明明赋值正确但是组建中的ajax方法可能返回的数据乱掉,或者其他神逻辑错误. 经过查询发 ...
- Linux I/O 映射(ioremap)和writel/readl
在裸奔代码中,如果要控制gpio,直接控制gpio寄存器地址即可: 在linux系统中,所有操作的地址都是虚拟地址,都是由linux内核去管理,所以需要将物理地址转换成内核可识别的虚拟地址. 1.io ...
- MySQL连接查询、联合查询、子查询
参考地址:http://blog.csdn.net/u011277123/article/details/54863371 1.MySQL连接查询 连接查询:将多张表(>=2)进行记录的连接(按 ...