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

6300:Grid Path Construction(2418)

时间限制: 1000 ms 内存限制: 524288 KB

提交数: 0 通过数: 0 Special 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

cpp 复制代码
#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;
}
相关推荐
土司大王11 分钟前
LeetCode hot100——合并两个有序链表
算法·leetcode·链表
码行山野赴时序归途14 分钟前
算法效率的度量(上):时间复杂度中的对数思维
c语言·数据结构·算法
算AI1 小时前
基于LLM的无人机仿真测试:新方法竞赛夺佳绩
人工智能·深度学习·算法·机器学习·ai
kaixin_啊啊2 小时前
专用优化算法LKH
算法
_Narcissus_3 小时前
二分算法笔记及例题
数据结构·c++·笔记·算法·蓝桥杯·查找·二分算法
tachibana24 小时前
RAGAS 指标解读
数据库·人工智能·算法·机器学习·架构·大模型·llm
qq_419563094 小时前
ToT 的 BFS/DFS 有个致命缺口:蒙特卡洛树搜索(MCTS)用「随机试错+统计」让大模型想得更深,小模型 + 它竟超过 GPT-4
算法·深度优先·宽度优先
万法若空5 小时前
CSP-J/S 排序算法完整专题训练题单
数据结构·算法·排序算法
凉茶钱5 小时前
【数据结构】排序(快排,选择,直接插入,希尔)
数据结构·算法·排序算法
weixin_446260855 小时前
拆解再复用:大模型智能体的跨任务技能迁移
人工智能·深度学习·算法