Codeforces Round 217 (Div. 2) A. Rook, Bishop and King(BFS)

Rook, Bishop and King

题面翻译

【题目描述】

佩蒂亚正在学习国际象棋。他已经学会如何移动王、车和象。让我们提示你如何移动国象棋子。棋盘有 64 64 64个棋格,呈 8 × 8 8\times8 8×8正方形。一个格子可以用 ( r , c ) (r,c) (r,c)来表示—— r r r指行, c c c指列(虽然在经典棋局中用字母和数字一起表示)。每一个棋子占用一个棋格。一次合法的棋步就是执行如下之一:

  • 车可以横向或纵向移动任意格。
  • 象可以斜着移动任意格。
  • 王可以任意方向移动一格——横着或者斜着。

佩蒂亚在想,从 ( r 1 , c 1 ) (r_1,c_1) (r1,c1)移动到 ( r 2 , c 2 ) (r_2,c_2) (r2,c2)所需的最少步数是多少?我们假设在棋盘上只有一枚棋子。帮他解决问题。

【输入格式】

输入包括四个整数 r 1 , c 1 , r 2 , c 2 ( 1 < = r 1 , c 1 , r 2 , c 2 < = 8 ) r_1,c_1,r_2,c_2(1<=r_1,c_1,r_2,c_2<=8) r1,c1,r2,c2(1<=r1,c1,r2,c2<=8)——分别代表出发的棋格和目的地棋格。数据保证两个格子不相同。你可以假设棋盘的行从上到下是 1 1 1 8 8 8,棋盘从左到右是 1 1 1 8 8 8

【输出格式】

输出三个用空格分隔的整数,分别代表车、象、王从 ( r 1 , c 1 ) (r_1,c_1) (r1,c1)移动到 ( r 2 , c 2 ) (r_2,c_2) (r2,c2)所需的最少步数。如果无法移动到,则输出 0 0 0

题目描述

Little Petya is learning to play chess. He has already learned how to move a king, a rook and a bishop. Let us remind you the rules of moving chess pieces. A chessboard is 64 square fields organized into an $ 8×8 $ table. A field is represented by a pair of integers $ (r,c) $ — the number of the row and the number of the column (in a classical game the columns are traditionally indexed by letters). Each chess piece takes up exactly one field. To make a move is to move a chess piece, the pieces move by the following rules:

  • A rook moves any number of fields horizontally or vertically.
  • A bishop moves any number of fields diagonally.
  • A king moves one field in any direction — horizontally, vertically or diagonally.

The pieces move like thatPetya is thinking about the following problem: what minimum number of moves is needed for each of these pieces to move from field $ (r_{1},c_{1}) $ to field $ (r_{2},c_{2}) $ ? At that, we assume that there are no more pieces besides this one on the board. Help him solve this problem.

输入格式

The input contains four integers $ r_{1},c_{1},r_{2},c_{2} $ ( $ 1<=r_{1},c_{1},r_{2},c_{2}<=8 $ ) — the coordinates of the starting and the final field. The starting field doesn’t coincide with the final one.

You can assume that the chessboard rows are numbered from top to bottom 1 through 8, and the columns are numbered from left to right 1 through 8.

输出格式

Print three space-separated integers: the minimum number of moves the rook, the bishop and the king (in this order) is needed to move from field $ (r_{1},c_{1}) $ to field $ (r_{2},c_{2}) $ . If a piece cannot make such a move, print a 0 instead of the corresponding number.

样例 #1

样例输入 #1

4 3 1 6

样例输出 #1

2 1 3

样例 #2

样例输入 #2

5 5 5 6

样例输出 #2

1 0 1

这道题和普通的BFS并无太大区别,在做的时候我会想到开一个结构体来记录坐标,步数和前一步操作,这个方法对于Rook和King都适用,但是对Bishop不行

Bishop的移动过程是斜着移动多个距离,如果一个一个坐标的进行遍历,就会导致有些坐标会被封堵上,这样就导致有些情况是无法被扫出来的。

所以这道题BFS的正解其实是每次都遍历完路径上能够到达的所有点。

CODE:

#include<bits/stdc++.h>
using namespace std;
const int N = 15;
const int Rook_dx[4] = {1,-1,0,0};
const int Rook_dy[4] = {0,0,1,-1};
const int Bishop_dx[4] = {1,1,-1,-1};
const int Bishop_dy[4] = {1,-1,1,-1};
const int King_dx[8] = {1,0,-1,0,1,1,-1,-1};
const int King_dy[8] = {0,1,0,-1,1,-1,1,-1};
struct Node{
    int x,y;
    int step;
    int pre;
};
int r1,c1,r2,c2;
bool vis[N][N];

int bfs(int type){ //1:Rook,2"Bishop,3"King
    memset(vis,0,sizeof vis);
    int res = 2e9;
    queue<Node>q;
    q.push({r1,c1,0,10});

    vis[r1][c1] = 1;

    while(q.size()){
        Node t = q.front();
        q.pop();

        if(t.x == r2 && t.y == c2)return t.step;

        //cout << t.x << " " << t.y << " " << t.step << endl;

        if(type == 1){
            for(int i = 0;i < 4;i++){
                int x = t.x + Rook_dx[i],y = t.y + Rook_dy[i];
                if(x >= 1 && x <= 8 && y >= 1 && y <= 8 && !vis[x][y]){
                    vis[x][y] = 1;
                    if(t.pre == i) q.push({x,y,t.step,i});
                    else q.push({x,y,t.step + 1,i});
                }
            }
        }else if(type == 2){
            for(int i = 0;i < 4;i++){
                int x = t.x,y = t.y;
                while(x <= 8 && x >= 1 && y <= 8 && y >= 1){	//在没有超过边界的时候一直去走
                    if(!vis[x][y]){
                        q.push({x,y,t.step + 1,i});
                        vis[x][y] = 1;
                    }
                    x += Bishop_dx[i],y += Bishop_dy[i];
                }
            }
        }else{
            for(int i = 0;i < 8;i++){
                int x = t.x + King_dx[i],y = t.y + King_dy[i];
                if(x >= 1 && x <= 8 && y >= 1 && y <= 8 && !vis[x][y]){
                    vis[x][y] = 1;
                    q.push({x,y,t.step + 1,i});
                }
            }
        }
    }
    if(res == 2e9)return 0;
    else return res;
}

int main(){
    cin >> r1 >> c1 >> r2 >> c2;
    //bfs(2);
    cout << bfs(1) << " " << bfs(2) << " " << bfs(3) << " ";
    return 0;
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mfbz.cn/a/610362.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

C++与java回调函数用法区别实例(二百七十一)

简介&#xff1a; CSDN博客专家&#xff0c;专注Android/Linux系统&#xff0c;分享多mic语音方案、音视频、编解码等技术&#xff0c;与大家一起成长&#xff01; 优质专栏&#xff1a;Audio工程师进阶系列【原创干货持续更新中……】&#x1f680; 优质专栏&#xff1a;多媒…

【一起深度学习——NIN】

NIN神经网络 原理图&#xff1a;代码实现&#xff1a;输出结果&#xff1a; 原理图&#xff1a; 代码实现&#xff1a; import torch from torch import nn from d2l import torch as d2ldef nin_block(in_channels, out_channels, kernel_size, strides, padding):return nn.…

Linux的命令

&#xff1b; 昨天学习了七个命令&#xff0c;分别是&#xff1a;cd命令&#xff08;切换目录&#xff09;、pwd命令&#xff08;当前目录&#xff09;、mkdir命令&#xff08;创建目录&#xff09;、touch命令&#xff08;创建文件&#xff09;、date命令&#xff08;显…

weditor安装的时候产生的问题

先放出来github的地址https://github.com/alibaba/web-editor&#xff0c;这个上面给了两种安装方式一种是&#xff1a; pip3 install -U weditor 这种方式会报错误&#xff0c; 具体原因我也不知道。那就采用第二种方式 git clone https://github.com/openatx/weditor pip3…

JS执行原理大揭秘:事件循环Event Loop与宏任务、微任务

前言 &#x1f4eb; 大家好&#xff0c;我是南木元元&#xff0c;热爱技术和分享&#xff0c;欢迎大家交流&#xff0c;一起学习进步&#xff01; &#x1f345; 个人主页&#xff1a;南木元元 目录 事件循环概述 异步和单线程 同步任务 异步任务 任务队列 宏任务 微任务…

海云安受邀参加诸子云 4.27南京「金融互联网」私董会

4月27日&#xff0c;“安在新媒体网安用户行业活动”第四期私董会在南京顺利举办。活动以“金融&互联网”为主题&#xff0c;邀请十余位业内资深的甲方用户以及典型厂商代表。摒弃传统的议题分享&#xff0c;采取“随时问答&#xff0c;自由讨论”的形式&#xff0c;提问题…

在做题中学习(57):寻找数组的中心下标

724. 寻找数组的中心下标 - 力扣&#xff08;LeetCode&#xff09; 解法&#xff1a;前缀和后缀和 思路&#xff1a;要看一个数是不是中心下标&#xff0c;就看他前面数的和 与 后面数的和 相不相等。 1.i前面数的和&#xff0c;是[0,i-1] 的前缀和&#xff0c;i后面数的和&am…

LeetCode746:使用最小花费爬楼梯

题目描述 给你一个整数数组 cost &#xff0c;其中 cost[i] 是从楼梯第 i 个台阶向上爬需要支付的费用。一旦你支付此费用&#xff0c;即可选择向上爬一个或者两个台阶。 你可以选择从下标为 0 或下标为 1 的台阶开始爬楼梯。 请你计算并返回达到楼梯顶部的最低花费。 代码 …

323_C++_QT_QProcess执行cmd解压、压缩、删除tar.gz等等其他压缩包文件到指定目录,不需要外部库,QT自带API的就行

// decompressPath : 解压到此目录 // fileName : 解压的tar.gz文件名executeCommand(decompressPath , QString::fromStdString(fileName));// 开始解压 void executeCommand

视频剪辑一键处理技巧:批量分割视频,快速提取m3u8视频

随着网络视频的普及和多样化&#xff0c;视频剪辑和处理成为了很多用户的基本需求。在众多的视频处理技巧中&#xff0c;批量分割视频快速提取m3u8视频是常见的操作。本文将介绍如何利用云炫AI智剪一键处理的技巧&#xff0c;轻松完成这些任务&#xff0c;提高视频剪辑的效率。…

提高岩土工程安全的关键:锚索测力计的应用

岩土工程是土木工程中的一个重要分支&#xff0c;涉及到基础建设、坡面稳定、隧道建设等多个领域。这些工程的安全性对人们的生活和财产安全至关重要。在众多技术和工具中&#xff0c;锚索测力计的应用在提高岩土工程的安全性方面发挥了不可替代的作用。 点击输入图片描述&…

AXI4写时序在AXI Block RAM (BRAM) IP核中的应用

在本文中将展示描述了AXI从设备&#xff08;slave&#xff09;AXI BRAM Controller IP核与Xilinx AXI Interconnect之间的写时序关系。 1 Single Write 图1是一个关于32位宽度的BRAM&#xff08;Block RAM&#xff09;的单次写入操作的例子。这个例子展示了如何向地址0x1000h…

[ 视频号]代替用户发布视频api

使用接口&#xff0c;替代用户使用设备直接发布视频api 接口地址&#xff1a; http://接口地址/api/v2 先调用登录接口&#xff0c;进行账号登录 登录二维码接口入参&#xff1a; {"appId": "","proxyIp": "","regionId"…

springcloud整合网关(springcloud-gateway)

pom引入依赖 <dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-gateway</artifactId></dependency><!-- 服务注册 --><dependency><groupId>com.alibaba.cloud</groupId&…

postman工具使用

一、配置每个接口都有公共的请求头 1.1 新建一个collect集合 my test 1.2 在pre-request script 输入配置 pm.request.addHeader("uid:24011"); pm.request.addHeader("version:2.0.0"); pm.request.addHeader("timezone:8"); pm.request.ad…

Redis 实战之监视器

监视器 成为监视器向监视器发送命令信息总结 成为监视器 发送MONITOR 命令可以让一个普通客户端变为一个监视器&#xff0c; 该命令的实现原理可以用以下伪代码来实现&#xff1a; def MONITOR():# 打开客户端的监视器标志client.flags | REDIS_MONITOR# 将客户端添加到服务器…

基于微信小程序的校园二手交易平台设计与实现(论文+源码)_kaic

基于微信小程序的校园二手交易平台 设计与实现 摘 要 随着绿色低碳消费和循环经济的理念越来越深入人心,大学生二手商品市场发展迅猛&#xff0c;而大部分二手交易平台运输方式与收售方式对于大学生用户群体并不适用&#xff0c;所以急需一款针对大学生二手商品交易的软件&…

【千帆平台】使用AppBuilder零代码创建应用,然后通过OpenAPI方式调用应用

欢迎来到《小5讲堂》 这是《千帆平台》系列文章&#xff0c;每篇文章将以博主理解的角度展开讲解。 温馨提示&#xff1a;博主能力有限&#xff0c;理解水平有限&#xff0c;若有不对之处望指正&#xff01; 目录 前言创建API密钥调用文档调用说明API服务域名通信协议字符编码公…

视频号小店保证金,服务费,手续费是多少?货款结算周期多长?

大家好&#xff0c;我是电商糖果 随着视频号小店越来越火&#xff0c;很多商家都想入驻小店。 入驻之前大家对视频号的收费问题都比较好奇。 糖果2022年就开始做店的了&#xff0c;对小店的保证金&#xff0c;服务费的&#xff0c;手续费&#xff0c;货款结算周期都非常了解…

Windows使用Miniconda3安装python、环境配置以及conda常用命令

Windows使用Miniconda3安装python以及conda常用命令 这是学习使用python的第一篇文章&#xff0c;这将是一个关于python学习和使用的一个系列文章的开始&#xff0c;有兴趣的可以给个关注持续获取更新内容。 Miniconda3是什么&#xff1f; Miniconda3是一个轻量级的Anaconda发…
最新文章