传送门:http://acm.tzc.edu.cn/acmhome/problemdetail.do?&method=showdetail&id=2755

思路:对起点到终点进行广搜,由于n特别大,不能用二维数组记录走过的点,可以用STL的map进行记录,map<pair<int,int>,int> v; 如果出现的点可以进行v[point] = 1;

另外可以剪枝  if(abs(sx-ex)/2>m||abs(sy-ey)/2>m)return 0;  其中sx,sy代表起点的横纵坐标,ex,ey代表终点的横纵坐标

AC代码如下:

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<string>
#include<cstdlib>
#include<cmath>
#include<stack>
#include<queue>
#include<set>
#include<map>
#include<vector>
#define LL long long
#include<assert.h>
using namespace std;
int go[][]={{,},{,-},{-,},{-,-},{,},{,-},{-,},{-,-}};
int n;
map<pair<int,int>,int> v;
struct note{
int x,y,step;
}pos,q;
int check(int x,int y){
if(x < ||x > n || y < || y > n)return ;
return ;
}
int bfs(int sx,int sy,int ex,int ey,int m){
pair<int ,int> a;
a.first = sx;a.second = sy;
v[a] = ;
if(abs(sx-ex)/>m||abs(sy-ey)/>m)return ;//剪枝
queue<note>que;
while(!que.empty()) que.pop(); pos.x = sx;pos.y = sy;pos.step = ;
que.push(pos);
while(que.size()){
q = que.front();
que.pop();
if(q.step > m)return ;
if(q.x == ex && q.y == ey && q.step <= m) return ;
for(int i = ; i < ; i ++){
int dx = q.x + go[i][];
int dy = q.y + go[i][];
pair<int,int>b;
b.first = dx;b.second = dy;
if(check(dx,dy) && q.step < m && v[b] == ){
pos.step = q.step + ;
pos.x = dx;pos.y = dy;
v[b] = ;
if(dx == ex && dy == ey && pos.step <= m) return ;
que.push(pos);
}
}
}
return ;
}
int main(){
int m;
while(~scanf("%d %d",&n,&m)){
v.clear();
int sx,sy,ex,ey;
scanf("%d %d %d %d",&sx,&sy,&ex,&ey);
if(!bfs(sx,sy,ex,ey,m)){
printf("Knight cannot reach Queen within %d moves!\n",m);
}
else{
printf("Knight can reach Queen within %d moves!\n",m);
} }
}

TOJ 2755 国际象棋(搜索)的更多相关文章

  1. TZOJ 2755 国际象棋(广搜+哈希)

    描述 在n*n的国际象棋棋盘中,给定一“马(Knight)”和一“后(Queen)”的位置,问“马”能否在m步之内(包括m步)到达“后”的位置?马的走法是:每步棋先横走或直走一格,然后再斜走一格,即走 ...

  2. hdu1372 dfs搜索之国际象棋的马

    原题地址 题意 一个8x8的国际象棋棋盘,你有一个棋子"马".算出棋子"马"从某一格到还有一格子的最少步数. 与普通dfs不同的是,你能走的路线不是上下左右,四 ...

  3. POJ-2488 国际象棋马的走法 (深度优先搜索和回溯)

    #include <stdio.h> #define MAX 27 void dfs(int i, int j); int dx[8] = {-1, 1, -2, 2, -2, 2, -1 ...

  4. JavaScript中国象棋程序(5) - Alpha-Beta搜索

    "JavaScript中国象棋程序" 这一系列教程将带你从头使用JavaScript编写一个中国象棋程序.这是教程的第5节. 这一系列共有9个部分: 0.JavaScript中国象 ...

  5. POJ 2243 简单搜索 (DFS BFS A*)

    题目大意:国际象棋给你一个起点和一个终点,按骑士的走法,从起点到终点的最少移动多少次. 求最少明显用bfs,下面给出三种搜索算法程序: // BFS #include<cstdio> #i ...

  6. 记录----第一次使用BFS(广度搜索)学习经验总结

    学习经验记录与分享—— 最近在学习中接触到了一种解决最短路径的实用方法----BFS(广度搜索),在这里总结并分享一下第一次学习的经验. 首先第一个要了解的是"queue"(队列函 ...

  7. OpenCV 学习笔记 06 图像检索以及基于图像描述符的搜索

    OpenCV 可以检测图像的主要特征,然后提取这些特征,使其成为图像描述符,这些图像特征可作为图像搜索的数据库:此外可以利用关键点将图像拼接 stitch 起来,组成一个更大的图像.如将各照片组成一个 ...

  8. AlphaGo论文的译文,用深度神经网络和树搜索征服围棋:Mastering the game of Go with deep neural networks and tree search

    转载请声明 http://blog.csdn.net/u013390476/article/details/50925347 前言: 围棋的英文是 the game of Go,标题翻译为:<用 ...

  9. 【搜索】还是N皇后

    先看题才是最重要的: 这道题有点难理解,毕竟Code speaks louder than words,所以先亮代码后说话: #include<iostream> using namesp ...

随机推荐

  1. leetcode983

    public class Solution { public int MincostTickets(int[] days, int[] costs) { ; ; ]; dp[] = ; ; i < ...

  2. Linux下源码编译安装PostgreSQL数据库

    我使用的Postgres的源码版本为 postgresql-9.3.5.系统为 CentOS6.5 ,是64位. 下载以后直接阅读其中的 README然后阅读其中的INSTALL,按照其中将的步骤做就 ...

  3. JVM老年代和新生代的比例

    在 Java 中,堆被划分成两个不同的区域:新生代 ( Young ).老年代 ( Old ).新生代 ( Young ) 又被划分为三个区域:Eden.From Survivor.To Surviv ...

  4. Django基础介绍

    1.web应用 Web应用程序是一种可以通过Web访问的应用程序,程序的最大好处是用户很容易访问应用程序,用户只需要有浏览器即可,不需要再安装其他软件. 应用程序有两种模式C/S.B/S.C/S是客户 ...

  5. python之 pendulum讲解

    一,下载地址:https://pypi.python.org/pypi/pendulum 二,pendulum的一大优势是内嵌式取代Python的datetime类,可以轻易地将它整合进已有代码,并且 ...

  6. Servlet基本_オブジェクトのスコープ

    1.スコープ種類Servletには以下のスコープがあります.Request.Session.Applicationの順にスコープは広くなっていきます.・Applicationスコープ:アプリケーション ...

  7. 2:if 语句

    if 语句 语法形式: 第一种,只有两个分支: if 表达式: something else: something 第二种,有多个分支: if 表达式1: do something 1 elif 表达 ...

  8. How to Pronounce the Numbers 1 – 10

    How to Pronounce the Numbers 1 – 10 Share Tweet Share Tagged With: Numbers Numbers are something you ...

  9. Ajax 学习 第三篇

    1.什么是json 第一种方法 第二种方法 比较evar and jsondata 任何时候使用EVAR要特别小心,他不会管输入对象的类型 JSONLint可以在线校验代码的正确性 改写代码

  10. editable : false与 readonly 的区别

    editable : false 不能输入 readonly:不可操作,只能看