UVA 439 Knight Moves --DFS or BFS
简单搜索,我这里用的是dfs,由于棋盘只有8x8这么大,于是想到dfs应该可以过,后来由于边界的问题,TLE了,改了边界才AC。
这道题的收获就是知道了有些时候dfs没有特定的边界的时候要自己设置一个合适的边界。这题推导可知,任意两点之间马踩6步之内一定能够到达,6步之内还未搜到说明绝对不是最优结果,果断退出。所以这里的res开始时最小设定为6即可,随着设的res的增大,运行时间越来越多,因为深搜可以有很多的分支,不采取较小的边界的话,可能会浪费很多时间在无用的搜索上,所以需要如此剪枝。
反复提交验证发现,res设不同值的运行时间如下:
res = 6 102ms
res = 10 222ms
res = 20 429ms
res = 30 929ms (过了30后极速增长)
res = 31 1692ms
res = 32 TLE !!!
代码:
- #include <iostream>
- #include <cstdio>
- #include <cstring>
- #include <cmath>
- #include <algorithm>
- using namespace std;
- #define N 9
- int vis[N][N];
- int res;
- int dx[] = {,,-,-,,,-,-};
- int dy[] = {,-,,-,,-,,-};
- inline bool OK(int nx,int ny)
- {
- if(nx >= && nx <= && ny >= && ny <= && !vis[nx][ny])
- return true;
- return false;
- }
- void dfs(int sx,int sy,int tx,int ty,int cnt)
- {
- if(cnt >= res)
- return;
- if(sx == tx && sy == ty && cnt < res)
- {
- res = cnt;
- return;
- }
- for(int i=;i<;i++)
- {
- int nx = sx + dx[i];
- int ny = sy + dy[i];
- if(!OK(nx,ny))
- continue;
- vis[nx][ny] = ;
- dfs(nx,ny,tx,ty,cnt+);
- vis[nx][ny] = ;
- }
- return;
- }
- int main()
- {
- char s1[],s2[];
- int sx,sy,tx,ty;
- while(scanf("%s%s",s1,s2)!=EOF)
- {
- sx = s1[] - 'a' + ;
- tx = s2[] - 'a' + ;
- sy = s1[] - '';
- ty = s2[] - '';
- memset(vis,,sizeof(vis));
- vis[sx][sy] = ;
- res = ; //·dfs边界,能取到正确的最小值最好,这里设为6或以上即可
- dfs(sx,sy,tx,ty,);
- printf("To get from %s to %s takes %d knight moves.\n",s1,s2,res);
- }
- return ;
- }
UVA 439 Knight Moves --DFS or BFS的更多相关文章
- UVA 439 Knight Moves(BFS)
Knight Moves option=com_onlinejudge&Itemid=8&category=11&page=show_problem&problem=3 ...
- UVA 439 Knight Moves
// 题意:输入标准国际象棋棋盘上的两个格子,求马最少需要多少步从起点跳到终点 BFS求最短路: bfs并维护距离状态cnt, vis记录是否访问过 #include<cstdio> ...
- uva 439 Knight Moves 骑士移动
这道题曾经写过,bfs.用队列,不多说了,上代码: #include<stdio.h> #include<stdlib.h> #include<string.h> ...
- 【UVa】439 Knight Moves(dfs)
题目 题目 分析 没有估价函数的IDA...... 代码 #include <cstdio> #include <cstring> #include <a ...
- UVa 439骑士的移动(BFS)
https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- Knight Moves (双向bfs)
# 10028. 「一本通 1.4 例 3」Knight Moves [题目描述] 编写一个程序,计算一个骑士从棋盘上的一个格子到另一个格子所需的最小步数.骑士一步可以移动到的位置由下图给出. [算法 ...
- H - Knight Moves DFS
A friend of you is doing research on the Traveling Knight Problem (TKP) where you are to find the sh ...
- Knight Moves UVA - 439
A friend of you is doing research on the Traveling Knight Problem (TKP) where you are to find the sh ...
- HDU 1372 Knight Moves (bfs)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1372 Knight Moves Time Limit: 2000/1000 MS (Java/Othe ...
随机推荐
- ASP.NET Web API涉及到的上下文
1.表示请求的上下文:HttpRequestContext; 2.表示HttpController:HttpControllerContext; 3.表示Action方法:HttpActionCont ...
- asp.net 发送邮件
asp.net 发送邮件 System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage(); msg.To. ...
- swift学习笔记之-函数
//函数 import UIKit /*获得系统时间 var date = NSDate() var timeFormatter = NSDateFormatter() timeFormatter.d ...
- mvc项目架构分享系列之架构搭建之Infrastructure
项目架构搭建之Infrastructure的搭建 Contents 系列一[架构概览] 0.项目简介 1.项目解决方案分层方案 2.所用到的技术 3.项目引用关系 系列二[架构搭建初步] 4.项目架构 ...
- MySQL数据库中字符集的问题
今天在做Hibernate案例,往mysql中写记录的时候,出现ERROR: Incorrect string value: '\xE5\x8A\xA0\xE5\x86\x85...' for col ...
- 浅谈ClickableSpan , 实现TextView文本某一部分文字的点击响应
超文本:http://www.baidu.com 这么一个效果:一行文本当中 前面显示黑色颜色的“超文本:”,后面显示红色颜色的“http://www.baidu.com” 并且要求红色字体的部分可以 ...
- java多线程系列1--线程实现与调度
java的重要功能之一就是内部支持多线程,这一系列文章将详细剖析java多线程的基础知识 多线程概述 多线程引入 程序只有一个执行流程,所以这样的程序就是单线程程序. 假如一个程序有多条执行流程,那么 ...
- iOS打印Debug日志的方式
简单介绍以下几个宏: 1) __VA_ARGS__ 是一个可变参数的宏,这个可变参数的宏是新的C99规范中新增的,目前似乎只有gcc支持(VC6.0的编译器不支持).宏前面加上##的作用在于,当可变参 ...
- iOS之 Mac下抓包工具使用wireshark
主要是mac上面网卡的授权 分三个步骤: 1.wireshark安装 wireshark运行需要mac上安装X11,mac 10.8的系统上默认是没有X11的.先去http://x ...
- [VMware]设置VM虚拟机随系统自动启动
设置步骤: 1.找到VM的安装路径,右键vmware发送到桌面快捷方式 2.右键桌面快捷方式的属性,看到目标的属性框 3.找到需要自启动的虚拟机路径,如: D:\QC_VM\Clone of Wind ...