博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
简单回溯,最少步数
阅读量:4630 次
发布时间:2019-06-09

本文共 1279 字,大约阅读时间需要 4 分钟。

题目链接:

 

#include 
#include
#include
#define INF 0x3f3f3f3fusing namespace std;int MAPN[9][9]={ {
1,1,1,1,1,1,1,1,1}, {
1,0,0,1,0,0,1,0,1}, {
1,0,0,1,1,0,0,0,1}, {
1,0,1,0,1,1,0,1,1}, {
1,0,0,0,0,1,0,0,1}, {
1,1,0,1,0,1,0,0,1}, {
1,1,0,1,0,1,0,0,1}, {
1,1,0,1,0,0,0,0,1}, {
1,1,1,1,1,1,1,1,1},};int color[9][9];int mov[4][2]= {
{
1,0},{-1,0},{
0,1},{
0,-1}};int dfs(int sx,int sy,int x,int y){ int step=INF; if(sx==x&&sy==y) return 0; else { for(int k=0; k<4; k++) { int tx=sx+mov[k][0]; int ty=sy+mov[k][1]; if(tx>=0&&tx<=8&&ty>=0&&ty<=8) { if(!color[tx][ty]&&!MAPN[tx][ty]) { color[tx][ty]=1; step=min(step,dfs(tx,ty,x,y)+1); color[tx][ty]=0; } } } return step; }}int main(){ int sx,sy,x,y;///起始位置,目标位置。 int t; scanf("%d",&t); while(t--) { memset(color,0,sizeof(color)); scanf("%d%d%d%d",&sx,&sy,&x,&y); printf("%d\n",dfs(sx,sy,x,y)); } return 0;}

 

转载于:https://www.cnblogs.com/TreeDream/p/5316612.html

你可能感兴趣的文章
腾讯Bugly异常崩溃SDK接入
查看>>
安装centos后无法引导启动windows7的解决方法
查看>>
AutoMapper用法
查看>>
Asterisk安装
查看>>
鄙视题
查看>>
如何在Vue项目中使用vw实现移动端适配(转)
查看>>
Apache Tomcat 7.x 概述
查看>>
as3绕过策略文件给视频截图
查看>>
C语言程序设计第一次作业
查看>>
leetcode网学习笔记(1)
查看>>
自制操作系统Antz(9)——实现内核 (下) 实现图形化界面
查看>>
JavaScript获取当前日期,昨天,今天日期以及任意天数间隔日期
查看>>
电子宠物系统
查看>>
windows远程桌面如果超出最大连接数, 使用命令行mstsc /console登录即可
查看>>
49. Group Anagrams
查看>>
SPOJ ATOMS - Atoms in the Lab
查看>>
关于 ListBox 自动换行
查看>>
postman测试上传文件
查看>>
R. ftp软件
查看>>
List<T>中,Remove和RemoveAt区别
查看>>