TOJ 2755 国际象棋(搜索)
传送门: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 国际象棋(搜索)的更多相关文章
- TZOJ 2755 国际象棋(广搜+哈希)
描述 在n*n的国际象棋棋盘中,给定一“马(Knight)”和一“后(Queen)”的位置,问“马”能否在m步之内(包括m步)到达“后”的位置?马的走法是:每步棋先横走或直走一格,然后再斜走一格,即走 ...
- hdu1372 dfs搜索之国际象棋的马
原题地址 题意 一个8x8的国际象棋棋盘,你有一个棋子"马".算出棋子"马"从某一格到还有一格子的最少步数. 与普通dfs不同的是,你能走的路线不是上下左右,四 ...
- POJ-2488 国际象棋马的走法 (深度优先搜索和回溯)
#include <stdio.h> #define MAX 27 void dfs(int i, int j); int dx[8] = {-1, 1, -2, 2, -2, 2, -1 ...
- JavaScript中国象棋程序(5) - Alpha-Beta搜索
"JavaScript中国象棋程序" 这一系列教程将带你从头使用JavaScript编写一个中国象棋程序.这是教程的第5节. 这一系列共有9个部分: 0.JavaScript中国象 ...
- POJ 2243 简单搜索 (DFS BFS A*)
题目大意:国际象棋给你一个起点和一个终点,按骑士的走法,从起点到终点的最少移动多少次. 求最少明显用bfs,下面给出三种搜索算法程序: // BFS #include<cstdio> #i ...
- 记录----第一次使用BFS(广度搜索)学习经验总结
学习经验记录与分享—— 最近在学习中接触到了一种解决最短路径的实用方法----BFS(广度搜索),在这里总结并分享一下第一次学习的经验. 首先第一个要了解的是"queue"(队列函 ...
- OpenCV 学习笔记 06 图像检索以及基于图像描述符的搜索
OpenCV 可以检测图像的主要特征,然后提取这些特征,使其成为图像描述符,这些图像特征可作为图像搜索的数据库:此外可以利用关键点将图像拼接 stitch 起来,组成一个更大的图像.如将各照片组成一个 ...
- 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,标题翻译为:<用 ...
- 【搜索】还是N皇后
先看题才是最重要的: 这道题有点难理解,毕竟Code speaks louder than words,所以先亮代码后说话: #include<iostream> using namesp ...
随机推荐
- 03.windows系统重新分配ip的cmd命令
网络重启CMD命令 ipconfig /release --- 释放ip搜索 ipconfig /renew --- 重新获得
- Mac安装Python3报错Permission denied @ dir_s_mkdir - /usr/local/Frameworks
brew安装Python3时出现的问题: Error: Permission denied @ dir_s_mkdir - /usr/local/Frameworks /usr/local/Frame ...
- Spring Cloud (5)hystrix 服务熔断
1.pom文件 <dependency> <groupId>org.springframework.cloud</groupId> <artifactId&g ...
- 图文详解AO打印(端桥模式)(转)
一.概述 AO打印是英文Active-Online Print的简称,也称主动在线打印.打印前支持AO通讯协议的AO打印机首先通过普通网络与C-Lodop服务保持在线链接,网页程序利用JavaSc ...
- Matplotlib中文乱码问题
一.找到并修改matplotlibrc文件 进入Python安装目录下的Lib\site-packages\matplotlib\mpl-data目录,用记事本打开matplotlibrc文件:找到f ...
- php 获取文件后缀最简单的方法
1 <?php 2 $recordingname = '通话录音@18502290616(18502290616)_20171103142448.mp3'; 3 $suffix = end(ex ...
- 【365】拉格朗日乘子法与KKT条件说明
参考:知乎回答 - 通过山头形象描述 参考:马同学 - 如何理解拉格朗日乘子法? 参考: 马同学 - 如何理解拉格朗日乘子法和KKT条件? 参考:拉格朗日乘数 - Wikipedia 自己总结的规律 ...
- vmware搭建lnmp环境配置域名
找到nginx配置文件,修改server_name 然后找到/etc/hosts文件 修改成如下 之后在Windows本地的C盘的hosts文件中添加解析 好了,这样就可以访问了 通往牛逼的路上,在意 ...
- Java基本语法知识要点
0x00 一个源文件中有多少个类,在用javac编译后,在同一目录下将产生多少个对应的字节码文件(.class ).类里面不一定要有public static void main(String[] ...
- @JsonInclude注解,RestTemplate传输值为null的属性,利用FastJson将属性中有空值null的对象转化成Json字符串
一个pojo类: import lombok.Data; @Data public class Friend { private String name; private int age; priva ...