当前位置: 首页 > news >正文

蓝桥杯 路径之谜

路径之谜

题目描述

小明冒充 XX 星球的骑士,进入了一个奇怪的城堡。

城堡里边什么都没有,只有方形石头铺成的地面。

假设城堡地面是 n×nn×n 个方格。如下图所示。

图1

按习俗,骑士要从西北角走到东南角。可以横向或纵向移动,但不能斜着走,也不能跳跃。每走到一个新方格,就要向正北方和正西方各射一箭。(城堡的西墙和北墙内各有 nn 个靶子)同一个方格只允许经过一次。但不必走完所有的方格。如果只给出靶子上箭的数目,你能推断出骑士的行走路线吗?有时是可以的,比如上图中的例子。

本题的要求就是已知箭靶数字,求骑士的行走路径(测试数据保证路径唯一)

输入描述

第一行一个整数 NN (0≤N≤200≤N≤20),表示地面有 N×NN×N 个方格。

第二行 NN 个整数,空格分开,表示北边的箭靶上的数字(自西向东)

第三行 NN 个整数,空格分开,表示西边的箭靶上的数字(自北向南)

输出描述

输出一行若干个整数,表示骑士路径。

为了方便表示,我们约定每个小格子用一个数字代表,从西北角开始编号: 0,1,2,3 ⋯⋯

比如,上图中的方块编号为:

0 1 2 3

4 5 6 7

8 9 10 11

12 13 14 15

输入输出样例

示例

输入

4
2 4 3 4
4 3 3 3

输出

0 4 5 1 2 3 7 11 10 9 13 14 15

好久没写都有点生疏,调试了很久。

#include <iostream>
using namespace std;int n, top[25], left1[25], map[25][25];
int res[800][2], idx = 0, flag = 0, started = 0;
int dir[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};void dfs(int cur_row, int cur_col){//cout<<cur_row<<" "<<cur_col<<endl;if(flag == 1){//cout<< "flag == 1"<<endl;return ;}if(cur_row < 1 || cur_row > n || cur_col < 1 || cur_col > n){//cout<< "out of bound"<<endl;return ; }if(map[cur_row][cur_col] > 1){return ;}int cnt = 0;for(int i=1; i<=n; i++){//cout<<top[i] <<" "<< left1[i]<<endl;if(top[i] < 0 || left1[i] < 0){//cout<< "negative num"<<endl;	return ;}cnt += top[i] + left1[i];}if(cur_row == n && cur_col == n && cnt == 0){ //cout<< "yes"<<endl;flag = 1;return ;}for(int i=0; i<4; i++){res[idx][0] = cur_row;res[idx][1] = cur_col;left1[cur_row + dir[i][0]]--;top[cur_col + dir[i][1]]--;map[cur_row + dir[i][0]][cur_col + dir[i][1]] += 1;idx++;dfs(cur_row + dir[i][0], cur_col + dir[i][1]);if(flag == 1){//cout<<"yes--"<<endl;return ;}left1[cur_row + dir[i][0]]++;top[cur_col + dir[i][1]]++;map[cur_row + dir[i][0]][cur_col + dir[i][1]] -= 1;idx--;}
}int main()
{  cin>>n;for(int i=1; i<=n; i++){cin>>top[i];}for(int i=1; i<=n; i++){cin>>left1[i];}map[1][1] = 1;left1[1]--;top[1]--;dfs(1, 1);for(int i=0; i<idx; i++){int num = ( res[i][0] - 1 ) * n + res[i][1] - 1;cout<<num<<" ";}cout<< n * n - 1;return 0;
}

http://www.lryc.cn/news/545730.html

相关文章:

  • Git操作指南:分支合并、回退及其他重要操作
  • Element Plus中el-tree点击的节点字体变色加粗
  • jenkens使用笔记
  • 腾讯混元文生图大模型(Hunyuan-DiT)与Stable Diffusion(SD)对比分析
  • 深入浅出理解编译器:前端视角
  • Minio搭建并在SpringBoot中使用完成用户头像的上传
  • Ubuntu系统上部署Node.js项目的完整流程
  • DeepSeek效应初现:Grok-3补刀ChatGPT,OpenAI已在ICU?
  • 【知识】torchrun 与 torch.multiprocessing.spawn 的对比
  • 深入了解 K-Means 聚类算法:原理与应用
  • Rust ~ Collect
  • C# 类型转换
  • [IP] DDR_FIFO(DDR3 用户FIFO接口)
  • 第三百七十二节 JavaFX教程 - JavaFX HTMLEditor
  • 蓝桥杯试题:DFS回溯
  • Lua | 每日一练 (4)
  • 每日一题——接雨水
  • java常见面试01
  • 算法-二叉树篇27-把二叉搜索树转换为累加树
  • C语言:51单片机 基础知识
  • olmOCR:使用VLM解析PDF
  • 数据结构(初阶)(七)----树和二叉树(堆,堆排序)
  • 图像分类项目1:基于卷积神经网络的动物图像分类
  • Kali Linux 2024.4版本全局代理(wide Proxy)配置,适用于浏览器、命令行
  • [Windows] 批量为视频或者音频生成字幕 video subtitle master 1.5.2
  • 不要升级,Flutter Debug 在 iOS 18.4 beta 无法运行,提示 mprotect failed: Permission denied
  • 介绍 torch-mlir 从 pytorch 生态到 mlir 生态
  • upload
  • InterHand26M(handposeX-json 格式)数据集-release >> DataBall
  • [Java基础] JVM常量池介绍(BeanUtils.copyProperties(source, target)中的属性值引用的是同一个对象吗)