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

深搜算法 6300:Grid Path Construction(2418)

6300:Grid Path Construction(2418)


时间限制: 1000 ms 内存限制: 524288 KB
提交数: 0 通过数: 0Special Judge

【题目描述】

Given an n×m grid and two squares a=(y1,x1) and b=(y2,x2), create a path from a to b that visits each square exactly once.

For example, here is a path from a=(1,3) to b=(3,6) in a 4×7 grid:

【输入】

The first input line has an integer t: the number of tests.

After this, there are t lines that describe the tests. Each line has six integers n, m, y1, x1, y2 and x2.

In all tests 1≤y1,y2≤n ja 1≤x1,x2≤m. In addition, y1≠y2 or x1≠x2.

【输出】

Print YES, if it is possible to construct a path, and NO otherwise.

If there is a path, also print its description which consists of characters U (up), D (down), L (left) ja R (right). If there are several paths, you can print any of them.

【输入样例】

5 1 3 1 1 1 3 1 3 1 2 1 3 2 2 1 1 2 2 2 2 1 1 2 1 4 7 1 3 3 6

【输出样例】

YES RR NO NO YES RDL YES RRRRDDDLLLLLLUUURDDRURDRURD

【提示】

1≤t≤100

1≤n≤50

1≤m≤50

#include <bits/stdc++.h> using namespace std; int g[51][51]; char str[3000]; void print(int &n,int &m); bool is(int &n,int &m,int x,int y,int &mx,int &my,int len){ if(x>=1 && x<=n && y>=1 && y<=m){ if(len<n*m-1){ if(x==mx && y==my)return false; else return true; } return true; } return false; } bool search(int &n,int &m,int x,int y,int &mx,int &my,int len){ //cout<<str<<endl; //printf("n=%d m=%d x=%d y=%d mx=%d my=%d len=%d\n",n,m,x,y,mx,my,len); //print(n,m); //x,y是当前位置 x是行 y是列 //mx,my是目标位置 //len是已寻找的路径长度 if(x==mx && y==my){ if(len==n*m-1){ str[len+1]='\0'; return true; } } int xx,yy; xx=x-1,yy=y; if(is(n,m,xx,yy,mx,my,len+1) && !g[xx][yy]){ str[len]='U'; g[xx][yy]=len+1;//随便标记 只要不是0就行 if(search(n,m,xx,yy,mx,my,len+1) )return true; else { g[xx][yy]=0; str[len]='\0'; } } xx=x,yy=y+1; if(is(n,m,xx,yy,mx,my,len+1) && !g[xx][yy]){ str[len]='R'; g[xx][yy]=len+1;//随便标记 只要不是0就行 if(search(n,m,xx,yy,mx,my,len+1) )return true; else { g[xx][yy]=0; str[len]='\0'; } } xx=x+1,yy=y; if(is(n,m,xx,yy,mx,my,len+1) && !g[xx][yy]){ str[len]='D'; g[xx][yy]=len+1;//随便标记 只要不是0就行 if(search(n,m,xx,yy,mx,my,len+1) )return true; else { g[xx][yy]=0; str[len]='\0'; } } xx=x,yy=y-1; if(is(n,m,xx,yy,mx,my,len+1) && !g[xx][yy]){ str[len]='L'; g[xx][yy]=len+1;//随便标记 只要不是0就行 if(search(n,m,xx,yy,mx,my,len+1) )return true; else { g[xx][yy]=0; str[len]='\0'; } } return false; } void print(int &n,int &m){ printf("路径展示:99是起点\n"); for(int i=1;i<=n;i++){ for(int j=1;j<=m;j++) printf("%2d ",g[i][j]); printf("\n"); } printf("\n"); } int main(int argc, char** argv) { int t,n,m,x1,y1,x2,y2; cin>>t; for(int i=0;i<t;i++) { cin>>n>>m>>x1>>y1>>x2>>y2; memset(g,0,sizeof(g)); memset(str,'\0',sizeof(str)); g[x1][y1]=99; if(search(n,m,x1,y1,x2,y2,0)) { cout<<"YES\n"<<str<<endl; print(n,m); } else cout<<"NO"<<endl; } return 0; }
http://www.jsqmd.com/news/519500/

相关文章:

  • 从吾爱论坛到开源神器:EternalBlaze作者的技术初心与硬链接工具诞生记
  • Java面上 HashMap Put方法 扩容机制 实现
  • Ubuntu22.04网络图标消失?5分钟快速修复指南(附详细命令)
  • 3DTiles白膜性能优化指南:如何让SHP建筑模型在Cesium中流畅加载
  • 【嵌入式性能生死线】:C语言驱动CAN FD控制器的7步原子操作加固法(ST/Infineon/NXP全平台验证)
  • 【国产单片机】华大HC32L13系列printf调试实战:从半主机模式到MicroLib的深度解析
  • OpenHarmony开发避坑指南:手把手教你写对BUILD.gn,解决90%的编译问题
  • 利用Mermaid在Markdown中高效构建数据库ER图
  • 别再乱用jet了!Matplotlib中5个最值得推荐的科学可视化colormap及使用场景
  • 2025美赛B题实战复盘:从零构建可持续旅游模型,Python代码全解析
  • FreeDOS 技术揭秘:从开源内核到经典DOS应用的全栈解析
  • ESP32驱动OV7670摄像头(无FIFO)保姆级教程:从GitHub克隆到网页实时显示
  • 华为Eth-Trunk链路聚合实战:从原理到配置详解
  • 锂离子电池恒流恒压充电Simulink仿真模型(CC-CV)及其电路结构与充电过程说明
  • nnUNetV2实战:从零构建医学影像2D分割数据集全流程解析
  • AI代写泛滥后,我实测5款论文降AI神器,帮我从80%拉到2%
  • 深入探讨大数据领域Zookeeper的分布式队列实现
  • OpenCV CSRT目标跟踪实战:从摄像头到无人机,5步搞定复杂场景跟踪
  • NLP工程师必看:AI原生语义检索中的Embedding技术深度剖析
  • HarmonyOS APP<玩转React>开源教程二十:收藏功能实现
  • 从SolarWinds事件看二进制SCA的重要性:你的供应链安全还缺这一环
  • Ubuntu20.04下微信中文输入终极解决方案:修改deepin-wine配置全记录
  • ARM64服务器上Docker跑Redis总崩溃?3种配置文件调试方案实测
  • SLAM避坑指南:为什么你的base_footprint总在Rviz里‘飘移‘?(TF树排查手册)
  • 基于虚拟阻抗重塑的构网型VSG变流器SISO序阻抗建模与宽频振荡抑制策略分析(面向高比例新能源并网场景)
  • 联发科MTK Sensor Bring Up避坑指南:以STK3321为例的常见问题解析
  • PyAV实战:如何用TCP协议稳定拉取RTSP视频流(附超时解决方案)
  • Microchip Libero SoC v12.2 Windows版:从官网下载到License激活的保姆级避坑指南
  • 保姆级教程:用FFmpeg+Nginx把监控摄像头RTSP流转成HLS网页播放
  • NRF52系列选型终极指南:从52810到52840,5个关键指标帮你省下30%成本