[usaco] 5.4.4 Betsy's Tour

简介: <p></p> <p> 好久没作usaco了。过了个年,人都懒了。</p> <p><br> Betsy's Tour<br> Don Piele <br> A square township has been divided up into N2 square plots (1 <= N <= 7). The Farm is located in the upper

 好久没作usaco了。过了个年,人都懒了。


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


这次的主要是减枝。

当到达一个格子后,下一个格子选择哪里呢?答案是,如果这个格子的旁边如果有个格子,该格子周围只有一个未访问格子时,就选择这个格子。
当然终点除外了。


Executing...
   Test 1: TEST OK [0.000 secs, 3180 KB]
   Test 2: TEST OK [0.000 secs, 3180 KB]
   Test 3: TEST OK [0.000 secs, 3180 KB]
   Test 4: TEST OK [0.000 secs, 3180 KB]
   Test 5: TEST OK [0.000 secs, 3180 KB]
   Test 6: TEST OK [0.011 secs, 3180 KB]
   Test 7: TEST OK [0.346 secs, 3180 KB]

/*
ID:yunleis2
PROG:betsy
LANG:C++
*/

#include<iostream>
#include<fstream>
using namespace std;
#define MAXN 10
#define  DEBUG
int N;
bool visited[MAXN][MAXN];
int nabor[MAXN][MAXN];
int total=0;
void travel(int x,int y,int n);
#define minusOne(nx,ny,one) {if(!visited[nx][ny]){	if((--nabor[nx][ny])==1&&!((nx==N&&ny==1)))		++one; }}
#define forwordOne(nx,ny,nth)    if(!visited[nx][ny]&&nabor[nx][ny]==1){ travel(nx,ny,nth+1);}
#define forword(nx,ny,nth)    if(!visited[nx][ny]){ travel(nx,ny,nth+1);}
#define rollback(nx,ny)   if(!visited[nx][ny]) nabor[nx][ny]++;
void print();
int main(){

	ifstream  fin("betsy.in");
	fin>>N;
	for(int i=1+1;i<N;++i){
		for(int j=1+1;j<N;++j){
			nabor[i][j]=4;
		}
	}
	for(int i=0;i<N+2;++i)
		visited[0][i]=visited[N+1][i]=true;
	for(int i=0;i<N+2;++i)
		visited[i][0]=visited[i][N+1]=true;
	for(int i=1+1;i<N;++i)
		nabor[1][i]=nabor[N][i]=3;
	for(int i=1+1;i<N;++i)
		nabor[i][1]=nabor[i][N]=3;
	nabor[1][1]=nabor[N][N]=nabor[N][1]=nabor[1][N]=2;
#ifdef DEBUG

	print();
	
#endif
	travel(1,1,1);
	ofstream fout("betsy.out");
	fout<<total<<endl;
	//system("pause");
}
void print(){
	for(int i=0;i<N+2;++i){
		for(int j=0;j<N+2;++j){
			cout<<nabor[i][j]<<" ";
		}
		cout<<endl;
	}
	cout<<endl;
	for(int i=0;i<N+2;++i){
		for(int j=0;j<N+2;++j){
			cout<<visited[i][j]<<" ";
		}
		cout<<endl;
	}
	
}
void travel(int x,int y,int nth){
	if(y==1&&x==(N)){
		if(nth!=N*N)
			return;
		++total;
		return;
	}
	
	visited[x][y]=true;
	int one=0;
	 minusOne(x-1,y,one);
	 minusOne(x,y-1,one);
	 minusOne(x+1,y,one);
	 minusOne(x,y+1,one);
//	cout<<x<<" ,"<<y<<endl;
//	print();
	if(one==1){		
		forwordOne(x-1,y,nth);
		forwordOne(x,y-1,nth);
		forwordOne(x+1,y,nth);
		forwordOne(x,y+1,nth);
	}
	else if(one==0)
	{
		forword(x-1,y,nth);
		forword(x,y-1,nth);
		forword(x+1,y,nth);
		forword(x,y+1,nth);
	}
	rollback(x-1,y);
	rollback(x,y-1);
	rollback(x+1,y);
	rollback(x,y+1);
	visited[x][y]=false;
	//roolback
}


 

目录
相关文章
|
1天前
|
云安全 人工智能 安全
AI被攻击怎么办?
阿里云提供 AI 全栈安全能力,其中对网络攻击的主动识别、智能阻断与快速响应构成其核心防线,依托原生安全防护为客户筑牢免疫屏障。
|
11天前
|
域名解析 人工智能
【实操攻略】手把手教学,免费领取.CN域名
即日起至2025年12月31日,购买万小智AI建站或云·企业官网,每单可免费领1个.CN域名首年!跟我了解领取攻略吧~
|
5天前
|
安全 Java Android开发
深度解析 Android 崩溃捕获原理及从崩溃到归因的闭环实践
崩溃堆栈全是 a.b.c?Native 错误查不到行号?本文详解 Android 崩溃采集全链路原理,教你如何把“天书”变“说明书”。RUM SDK 已支持一键接入。
449 192
|
3天前
|
数据采集 消息中间件 人工智能
跨系统数据搬运的全方位解析,包括定义、痛点、技术、方法及智能体解决方案
跨系统数据搬运打通企业数据孤岛,实现CRM、ERP等系统高效互通。伴随数字化转型,全球市场规模超150亿美元,中国年增速达30%。本文详解其定义、痛点、技术原理、主流方法及智能体新范式,结合实在Agent等案例,揭示从数据割裂到智能流通的实践路径,助力企业降本增效,释放数据价值。
|
9天前
|
人工智能 自然语言处理 安全
国内主流Agent工具功能全维度对比:从技术内核到场景落地,一篇读懂所有选择
2024年全球AI Agent市场规模达52.9亿美元,预计2030年将增长至471亿美元,亚太地区增速领先。国内Agent工具呈现“百花齐放”格局,涵盖政务、金融、电商等多场景。本文深入解析实在智能实在Agent等主流产品,在技术架构、任务规划、多模态交互、工具集成等方面进行全维度对比,结合市场反馈与行业趋势,为企业及个人用户提供科学选型指南,助力高效落地AI智能体应用。
|
5天前
|
消息中间件 安全 NoSQL
阿里云通过中国信通院首批安全可信中间件评估
近日,由中国信通院主办的 2025(第五届)数字化转型发展大会在京举行。会上,“阿里云应用服务器软件 AliEE”、“消息队列软件 RocketMQ”、“云数据库 Tair”三款产品成功通过中国信通院“安全可信中间件”系列评估,成为首批获此认证的中间件产品。此次评估覆盖安全可信要求、功能完备性、安全防护能力、性能表现、可靠性与可维护性等核心指标,标志着阿里云中间件产品在多架构适配与安全能力上达到行业领先水平。
315 195

热门文章

最新文章