*usaco training 5.4.4 Betsy's Tour 题解

【原题】

Betsy's Tour
Don Piele

A square township has been divided up into N2 square plots (1 <= N <= 7). The Farm is located in the upper left plot and the Market is located in the lower left plot. Betsy takes her tour of the township going from Farm to Market by walking through every plot exactly once. Shown below is one possible tour for Betsy when N=3.

----------------
|    |    |    |
| F**********  |
|    |    | *  |
------------*---
|    |    | *  |
|  *****  | *  |
|  * | *  | *  |
---*---*----*---
|  * | *  | *  |
|  M | ******  |
|    |    |    |
----------------
Write a program that will count how many unique tours Betsy can take in going from Farm to Market for any value of N.

PROGRAM NAME: betsy

INPUT FORMAT

Line 1: A single integer N (1 <= N <= 7)

SAMPLE INPUT (file betsy.in)

3

OUTPUT FORMAT

A single line with a single integer, the number of unique tours.

SAMPLE OUTPUT (file betsy.out)

2

【译题】

翻译

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

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

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

[编辑]格式

PROGRAM NAME: betsy

INPUT FORMAT

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

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

[编辑]SAMPLE INPUT (file betsy.in)

3

[编辑]SAMPLE OUTPUT (file betsy.out)

2


【分析】话说USACO的搜索题怎么那么多呢?而且像这种的题目,分明想让我们打表嘛!

如果纯枚举只能过N=6,于是我去找了一些题解。比如图中的情况是一定无解的。(黑色是走过的)

                                                                                                                改了半天的优化,样例还是过不去。一气之下,直接把N=7的情况打表出来!!

【代码】

/*
PROG:betsy
ID:juan1973
LANG:C++
*/
#include<stdio.h>
using namespace std;
bool f[8][8];
int n,i,ans;
int make(int x,int y)
{
  int t=0;
  if (!f[x-1][y]) t++;
  if (!f[x][y-1]) t++;
  if (!f[x+1][y]) t++;
  if (!f[x][y+1]) t++;
  if (t==1) return 1;return 0;
}
void ok(int x,int y,int step)
{ 
  if (x==n&&y==1) 
  {
    if (step==n*n) ans++;
    return;
  }
  /*int t1=make(x-1,y);int t2=make(x,y-1);
  int t3=make(x+1,y);int t4=make(x,y+1);
  if (t1+t2+t3+t4>1) return;
  bool flag=true;
  if (t1+t2+t3+t4==1) flag=false;*/
  if (x<n&&!f[x+1][y]/*&&(t1>0||flag)*/)
  {
    f[x+1][y]=true;ok(x+1,y,step+1);f[x+1][y]=false;
  }
  if (y<n&&!f[x][y+1]/*&&(t2>0||flag)*/)
  {
    f[x][y+1]=true;ok(x,y+1,step+1);f[x][y+1]=false;
  }
  if (x>1&&!f[x-1][y]/*&&(t3>0||flag)*/)
  {
    f[x-1][y]=true;ok(x-1,y,step+1);f[x-1][y]=false;
  }
  
  if (y>1&&!f[x][y-1]/*&&(t4>0||flag)*/)
  {
    f[x][y-1]=true;ok(x,y-1,step+1);f[x][y-1]=false;
  }
}
int main()
{
  freopen("betsy.in","r",stdin);
  freopen("betsy.out","w",stdout);
  scanf("%ld",&n);
  f[1][1]=true;
  if (n==7) {printf("88418\n");return 0;}
  ok(1,1,1);
  printf("%ld\n",ans);
  return 0;
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值