ny58 最小步数
最少步数
- 描述
-
这有一个迷宫,有0~8行和0~8列:
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,10表示道路,1表示墙。
现在输入一个道路的坐标作为起点,再如输入一个道路的坐标作为终点,问最少走几步才能从起点到达终点?
(注:一步是指从一坐标点走到其上下左右相邻坐标点,如:从(3,1)到(4,1)。)
- 输入
- 第一行输入一个整数n(0<n<=100),表示有n组测试数据;
随后n行,每行有四个整数a,b,c,d(0<=a,b,c,d<=8)分别表示起点的行、列,终点的行、列。 - 输出
- 输出最少走几步。
- 样例输入
-
2
3 1 5 7
3 1 6 7 - 样例输出
-
12
11
代码一:写的复杂了#include<stdio.h>
#include<string.h>
#include<queue> using namespace std; #define N 9 int maze[N][N]={,,,,,,,,,
,,,,,,,,,
,,,,,,,,,
,,,,,,,,,
,,,,,,,,,
,,,,,,,,,
,,,,,,,,,
,,,,,,,,,
,,,,,,,,};
int mazeH[N][N]; struct Node
{
int x,y,cnt; };
int dir[][]={{,},{,-},{,},{-,}};//定义方向 bool Judge(int x,int y)
{
return (x < N && x >= && y < N && y >= && !mazeH[x][y])?true:false;//如果没有超出边界,返回true,否者false } void MakeNode(int x,int y,int cnt,Node &node)
{
node.x=x;
node.y=y;
node.cnt=cnt;
mazeH[x][y]=;
} int bfs(int sX,int sY,int eX,int eY)
{
Node node,tempNode;
MakeNode(sX,sY,,node);
queue<Node> Q;
Q.push (node);
while( !Q.empty() )
{
node=Q.front();
Q.pop ();
if( node.x==eX && node.y==eY )//判断是否找到结束的那个点,如果找到了,将返回最小步数;
{
return node.cnt;
}
for(int i=;i<;++i)
{
if( Judge(node.x+dir[i][],node.y+dir[i][]) )
{
MakeNode(node.x+dir[i][],node.y+dir[i][],node.cnt+,tempNode);
Q.push (tempNode);
}
}
}
} int main()
{
int nCases;
scanf("%d",&nCases);
while( nCases-- )
{
int sX,sY,eX,eY;
scanf("%d%d%d%d",&sX,&sY,&eX,&eY);
memcpy(mazeH,maze,sizeof(maze));//把maze拷贝到mazeH中;
int ans=bfs(sX,sY,eX,eY);//bfs搜索
printf("%d\n",ans);
}
return ;
}代码二:这个简单点
#include<stdio.h>
#define min(x,y) x<y?x:y;
int map[][]={ ,,,,,,,,,
,,,,,,,,,
,,,,,,,,,
,,,,,,,,,
,,,,,,,,,
,,,,,,,,,
,,,,,,,,,
,,,,,,,,,
,,,,,,,, };
int x1,x2,y1,y2,m;
void dfs(int x,int y,int s)
{
if(map[x][y]) return ;
if(x==x2&&y==y2)
{
m=min(s,m);
return ;
}
s++;
map[x][y]=;
dfs(x+,y,s);
dfs(x-,y,s);
dfs(x,y+,s);
dfs(x,y-,s);
map[x][y]=;
}
int main()
{
int n;
scanf("%d",&n);
while(n--)
{
m=;
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
dfs(x1,y1,);
printf("%d\n",m);
}
return ;
}
ny58 最小步数的更多相关文章
- POJ 1753 Flip Game (高斯消元 枚举自由变元求最小步数)
题目链接 题意:4*4的黑白棋,求把棋全变白或者全变黑的最小步数. 分析:以前用状态压缩做过. 和上题差不多,唯一的不同是这个终态是黑棋或者白棋, 但是只需要把给的初态做不同的两次处理就行了. 感觉现 ...
- yzoi2226最小步数的详细解法
Description - 问题描述 在各种棋中,棋子的走法总是一定的,如中国象棋中马走“日”.有一位小学生就想如果马能有两种走法将增加其趣味性,因此,他规定马既能按“日”走,也能如象一样走“田”字. ...
- One Person Game(扩展欧几里德求最小步数)
One Person Game Time Limit: 2 Seconds Memory Limit: 65536 KB There is an interesting and simple ...
- Algorithm --> 棋盘中求出A到B的最小步数
求出A到B的最小步数 给定象棋盘,以及位置A和B, 求出从A到B的最小步数 代码: #include <cstdio> #include <iostream> #include ...
- 1到n的最小步数
1到n的最小步数 Time Limit: 1 Sec Memory Limit: 128 MB 给你一个数n,让你求从1到n的最小步数是多少. 对于当前的数x有三种操作: 1: x+1 2: x ...
- [leetcode]45. Jump Game II青蛙跳(跳到终点最小步数)
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- ZOJ 3593 One Person Game(拓展欧几里得求最小步数)
One Person Game Time Limit: 2 Seconds Memory Limit: 65536 KB There is an interesting and simple ...
- 带你学习BFS最小步数模型
最小步数模型 一.简介 最小步数模型和最短路模型的区别? 最短路模型:某一个点到另一个点的最短距离(坐标与坐标之间) 最小步数模型:不再是点(坐标),而是状态到另一个状态的转变 BFS难点所在(最短路 ...
- yzoi1109&&viojs1042最小步数的一点看法——回文数
Description - 问题描述 有一天,雄霸传授本人风神腿法第一式:捕风捉影..............的步法(弟子一:堂主,你大喘气呀.风:你给我闭嘴.)捕风捉影的关键是换气(换不好就会大喘气 ...
随机推荐
- 谈谈Boost网络编程(2)—— 新系统的设计
写文章之前.我们一般会想要採用何种方式,是"开门见山",还是"疑问式开头".写代码也有些类似.在编码之前我们须要考虑系统总体方案,这也就是各种设计文档的作用.在 ...
- KVC简介 -字典转模型,模型转字典
// 下面两个方法.都属于 KVC 的方法 // KVC 是 cocoa 的大招.间接给对象属性设置数值 // 程序运行过程中,动态给对象属性设置数值.不关心 .h 中是怎样定义的 // 仅 ...
- 'htmlentities(): charset `utf8' not supported, assuming utf-8'
TP5.1框架报错! Fatal error: Uncaught exception 'think\exception\ErrorException' with message 'htmlentit ...
- eclipse 如何修改maven插件本地仓库jar包默认存储位置
eclipse 如何修改maven插件本地仓库jar包默认存储位置 CreateTime--2018年4月18日11:04:47 Author:Marydon 1.更改eclipse的maven本 ...
- java 打包war包
jar -cvf news.war news war包放在Tomcat webApp中可以自动解压.
- shell之“>/dev/null 2>&1”
shell之“>/dev/null 2>&1” http://ixdba.blog.51cto.com/2895551/526442 今天在自己的一个技术群中又被问道了这么一个问题 ...
- Python监控文件变化:watchdog
Python监控文件变化有两种库:pyinotify和watchdog.pyinotify依赖于Linux平台的inotify,后者则对不同平台的的事件都进行了封装.也就是说,watchdog跨平台. ...
- OOAD和UML
ooad: object oriented analysis designer 又有两个分支: ooa(object oriented analysis):what to do ood(object ...
- Android学习系列(5)--App布局初探之简单模型
人类科技的进步源自探索,探索来自于发现本原,当然App布局没这么先进,本文也只是一个归类总结.这篇文章是Android开发人员的必备知识,是我特别为大家整理和总结的,不求完美,但是有用. Androi ...
- jsp地址栏传中文显示乱码解决方法
格式一: 地址栏显示格式:http://localhost:8081/Jsp2/ahref2.jsp?id=32&name=%E7%8E%8B%E4%BA%91%E9%B9%8F 1.修改To ...