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 ...
随机推荐
- Ubuntu下Python使用MySQLdb远程连接数据库的常见问题及解决方案
本文基于http://www.cnblogs.com/fnng/p/3565912.html这篇博文,学习使用MySQLdb过程中遇到下面这些问题. 1. 安装MySQLdb时运行sudo pytho ...
- flash代码
Flash常用的动作命令一.Flash中的常用命令:1.在当前帧停止播放 on(release){ stop();} 2.从当前帧开始播放 on(release){ play();} 3.跳到第 10 ...
- 理解 EventLoop
链接 链接 node 浏览器 执行顺序有差异 macrotask microtask 一个线程会有 堆 栈 消息队列; 栈函数执行是用的, 堆用了存放定义的对象, 消息队列来处理异步的操作 a() ...
- linux下json库的编译及例程
.下载JsonCpp http://sourceforge.net/projects/jsoncpp/files/ .下载scons http://sourceforge.net/projects/s ...
- Golang Printf、Sprintf 、Fprintf 格式化
/* %v 输出结构体 {10 30} %+v 输出结构体显示字段名 {one:10 tow:30} %#v 输出结构体源代码片段 main.Point{one:10, tow:30} %T 输出值的 ...
- Oracle新用户以及授权的若干问题
Database 实验4 问题: 授权语句 grant create table to user_name; 收回授权语句 revoke create table from user_name; 注意 ...
- (十一)java循环结构
while(循环的条件) {循环的语句} int a = 1; while(a < 5) { System.out.println(a);//1,2,3,4 a++; } System.out. ...
- wlan接收器如何共享网络
无线局域网络(Wireless Local Area Networks: WLAN)是相当便利的数据传输系统,它利用射频(Radio Frequency: RF)的技术,取代旧式碍手碍脚的双绞铜线(C ...
- weblogic控制台的启动与禁用
在一些安全漏洞扫描中,经常会扫描发现,使用weblogic管理控制台,会有个中危的漏洞. http://192.168.10.46:7001/console/login/LoginForm.jsp W ...
- HTTP协议-简介
1.什么是http协议? 百度百科上的解释:超文本传输协议(HTTP,HyperText Transfer Protocol)是互联网上应用最为广泛的一种网络协议.所有的WWW文件都必须遵守这个标准. ...