[ABC246E] Bishop
Problem Statement
We have an $N \times N$ chessboard. Let $(i, j)$ denote the square at the $i$-th row from the top and $j$-th column from the left of this board.
The board is described by $N$ strings $S_i$.
The $j$-th character of the string $S_i$, $S_{i,j}$, means the following.
- If $S_{i,j}=$
., the square $(i, j)$ is empty. - If $S_{i,j}=$
#, the square $(i, j)$ is occupied by a white pawn, which cannot be moved or removed.
We have put a white bishop on the square $(A_x, A_y)$.
Find the minimum number of moves needed to move this bishop from $(A_x, A_y)$ to $(B_x, B_y)$ according to the rules of chess (see Notes).
If it cannot be moved to $(B_x, B_y)$, report -1 instead.
Notes
A white bishop on the square $(i, j)$ can go to the following positions in one move.
For each positive integer $d$, it can go to $(i+d,j+d)$ if all of the conditions are satisfied.
- The square $(i+d,j+d)$ exists in the board.
- For every positive integer $l \le d$, $(i+l,j+l)$ is not occupied by a white pawn.
For each positive integer $d$, it can go to $(i+d,j-d)$ if all of the conditions are satisfied.
- The square $(i+d,j-d)$ exists in the board.
- For every positive integer $l \le d$, $(i+l,j-l)$ is not occupied by a white pawn.
For each positive integer $d$, it can go to $(i-d,j+d)$ if all of the conditions are satisfied.
- The square $(i-d,j+d)$ exists in the board.
- For every positive integer $l \le d$, $(i-l,j+l)$ is not occupied by a white pawn.
For each positive integer $d$, it can go to $(i-d,j-d)$ if all of the conditions are satisfied.
- The square $(i-d,j-d)$ exists in the board.
- For every positive integer $l \le d$, $(i-l,j-l)$ is not occupied by a white pawn.
Constraints
- $2 \le N \le 1500$
- $1 \le A_x,A_y \le N$
- $1 \le B_x,B_y \le N$
- $(A_x,A_y) \neq (B_x,B_y)$
- $S_i$ is a string of length $N$ consisting of
.and#. - $S_{A_x,A_y}=$
. - $S_{B_x,B_y}=$
.
Input
Input is given from Standard Input in the following format:
$N$
$A_x$ $A_y$
$B_x$ $B_y$
$S_1$
$S_2$
$\vdots$
$S_N$
Output
Print the answer.
Sample Input 1
5
1 3
3 5
....#
...#.
.....
.#...
#....
Sample Output 1
3
We can move the bishop from $(1,3)$ to $(3,5)$ in three moves as follows, but not in two or fewer moves.
- $(1,3) \rightarrow (2,2) \rightarrow (4,4) \rightarrow (3,5)$
Sample Input 2
4
3 2
4 2
....
....
....
....
Sample Output 2
-1
There is no way to move the bishop from $(3,2)$ to $(4,2)$.
Sample Input 3
18
18 1
1 18
..................
.####.............
.#..#..####.......
.####..#..#..####.
.#..#..###...#....
.#..#..#..#..#....
.......####..#....
.............####.
..................
..................
.####.............
....#..#..#.......
.####..#..#..####.
.#.....####..#....
.####.....#..####.
..........#..#..#.
.............####.
..................
其实我们可以有另外一种理解方式:代价相当于在移动过程中转向的次数。我们可以记录上一步走的是那个方向,然后这一步的代价就是是否和上一步不等。
发现代价只有 0 或 1,01BFS 即可。
#include<bits/stdc++.h>
using namespace std;
const int N=1505,M=N*N*4,dx[]={1,-1,1,-1},dy[]={1,-1,-1,1};
int dp[N][N][4],n,sx,sy,ex,ey,l=1,r;
char s[N][N];
int mo(int x)
{
return (x%M+M)%M;
}
int ok(int x,int y)
{
return x>0&&y>0&&x<=n&&y<=n&&s[x][y]=='.';
}
struct node{
int x,y,t;
}q[M];
int main()
{
scanf("%d%d%d%d%d",&n,&sx,&sy,&ex,&ey);
for(int i=1;i<=n;i++)
scanf("%s",s[i]+1);
memset(dp,0x7f,sizeof(dp));
dp[sx][sy][0]=dp[sx][sy][2]=dp[sx][sy][3]=dp[sx][sy][1]=1;
for(int i=0;i<4;i++)
q[++r]=(node){sx,sy,i};
while(l<=r)
{
// printf("%d %d\n",l,r);
int x=q[mo(l)].x,y=q[mo(l)].y,t=q[mo(l)].t;
++l;
for(int i=0;i<4;i++)
{
int tx=x+dx[i],ty=y+dy[i];
if(ok(tx,ty))
{
if(dp[x][y][t]+(i!=t)>=dp[tx][ty][i])
continue;
dp[tx][ty][i]=dp[x][y][t]+(i!=t);
if(i==t)
{
--l;
q[mo(l)]=(node){tx,ty,i};
}
else
{
++r;
q[mo(r)]=(node){tx,ty,i};
}
}
}
}
// for(int i=1;i<=n;i++)
// {
// for(int j=1;j<=n;j++)
// printf("%d ",min(min(dp[i][j][0],dp[i][j][1]),min(dp[i][j][2],dp[i][j][3])));
// putchar('\n');
// }
int ans=min(min(dp[ex][ey][0],dp[ex][ey][1]),min(dp[ex][ey][2],dp[ex][ey][3]));
printf("%d",ans>2e9? -1:ans);
}
[ABC246E] Bishop的更多相关文章
- codeforces A. Rook, Bishop and King 解题报告
题目链接:http://codeforces.com/problemset/problem/370/A 题目意思:根据rook(每次可以移动垂直或水平的任意步数(>=1)),bishop(每次可 ...
- Bishop的大作《模式识别与机器学习》Ready to read!
久仰Bishop的大作“Pattern Recognition and Machine Learning”已久,在我的硬盘里已经驻扎一年有余,怎奈惧其页数浩瀚,始终未敢入手.近日看文献,屡屡引用之.不 ...
- CF Rook, Bishop and King
http://codeforces.com/contest/370/problem/A 题意:车是走直线的,可以走任意多个格子,象是走对角线的,也可以走任意多个格子,而国王可以走直线也可以走对角线,但 ...
- Codeforce 370A Rook, Bishop and King 数学规律
这个题目挺有意思的,给定 起终点,要你求车,象,王分别最少要走多少步 车横竖都能走,而且每步任意走几格,所以它是最容易处理的,如果在同行或者同列,就是1,否则就是2 象要找下规律,象任意对角线都能走, ...
- CodeForces 370A Rook, Bishop and King
此题看似很简单,但实际上有不少细节,WA点不少.分情况处理即可. #include<cmath> #include<cstdio> #include<string> ...
- Day3----《Pattern Recognition and Machine Learning》Christopher M. Bishop
其实今天只花了一点点时间来学习这本书, 如果模型的参数过多,而训练数据又不足够多的话,就会出现overfitting. overfitting可以通过regularization来解决,贝叶斯方法也可 ...
- Day2----《Pattern Recognition and Machine Learning》Christopher M. Bishop
用一个例子来讲述regression. 采用sin(2*pi*x)加入微弱的正态分布噪声的方式来获得一些数据,然后用多项式模型来进行拟合. 在评价模型的准确性时,采用了误差函数的方式,用根均方误差的方 ...
- 学习笔记-----《Pattern Recognition and Machine Learning》Christopher M. Bishop
Preface 模式识别这个词,以前一直不懂是什么意思,直到今年初,才开始打算读这本广为推荐的书,初步了解到,它的大致意思是从数据中发现特征,规律,属于机器学习的一个分支. 在前言中,阐述了什么是模式 ...
- [SQL Server] 特殊字符、上标、下标处理
今天遇到一个问题是往 SQL Server 中导入像m².m³这样的单位数据,可是在 SQL Server 中查看到的都是 m2.m3,于是在网上查了一下资料,顺便摘录下来供日后查阅. 一 Wind ...
- 【机器学习Machine Learning】资料大全
昨天总结了深度学习的资料,今天把机器学习的资料也总结一下(友情提示:有些网站需要"科学上网"^_^) 推荐几本好书: 1.Pattern Recognition and Machi ...
随机推荐
- [ABC151E] Max-Min Sums
2023-03-11 题目 题目传送门 翻译 翻译 难度&重要性(1~10):5 题目来源 AtCoder 题目算法 数学 解题思路 对于一个正数 \(x,x\in A\) 一定会有 \(C_ ...
- 淘宝详情api接口的使用说明
淘宝详情API接口是一种可以用来获取淘宝商品详细信息的服务,包括图片.标题.价格.销量.评论等数据.下面是淘宝详情API接口的使用说明: 1.关于申请API接口权限: 在使用淘宝详情API接口前,需要 ...
- Hadoop环境安装与配置
1.基础操作系统环境安装(略) 2.JDK的安装与配置 当前各大数据软件如Hadoop等,仍然停留在Java 8上,在本实验选用的是Java 8.在自己的Linux系统中,jdk可以使用如下命令进行一 ...
- Gradle安装配置教程
一.安装前检查 检查电脑上是否安装JDK,如果没有安装,请查看JDK安装教程:点击查看 如果电脑上已经安装JDK,按Win + R键,输入cmd,然后点击确定 输入java -version,点击回车 ...
- 第2章 Git安装
兄弟,恭喜你,刷到这篇超详细安装GIt教程,就让Codeyang带你一步一步的安装Git!~~ Git官网地址: https://git-scm.com/ 查看 GNU 协议,可以直接点击下一步. 选 ...
- 杰哥教你面试之一百问系列:java集合
目录 1. 什么是Java集合?请简要介绍一下集合框架. 2. Java集合框架主要分为哪几种类型? 3. 什么是迭代器(Iterator)?它的作用是什么? 4. ArrayList和LinkedL ...
- 开源XL-LightHouse与Flink、ClickHouse之类技术相比有什么优势
Flink是一款非常优秀的流式计算框架,而ClickHouse是一款非常优秀的OLAP类引擎,它们是各自所处领域的佼佼者,这一点是毋庸置疑的.Flink除了各种流式计算场景外也必然可以用于流式统计,C ...
- Solution -「洛谷 P4007」小 Y 和恐怖的奴隶主
Description Link. 这道题 的加强版. Solution 题解里面大多数都是概率 DP,或者是期望 DP 然后是逆推.甚至不给 DP 的转移式.机房 yyds Reanap 发了一篇逆 ...
- 2023-09-27:用go语言,在一个 n x n 的国际象棋棋盘上,一个骑士从单元格 (row, column) 开始, 并尝试进行 k 次移动。行和列是 从 0 开始 的,所以左上单元格是 (0
2023-09-27:用go语言,在一个 n x n 的国际象棋棋盘上,一个骑士从单元格 (row, column) 开始, 并尝试进行 k 次移动.行和列是 从 0 开始 的,所以左上单元格是 (0 ...
- VScode+X11支持连接服务器时支持open3d、openCV、matplotlib等可视化
背景 连接服务器以后,想用open3d可视化点云.matplotlib画点图,但是一直不能用,原因也很简单,就是没有配置GUI传输显示,那肯定是要配置X11相关的东西. 过程 服务器 确保服务器下载了 ...