[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 ...
随机推荐
- Kioptrix: Level 1 (#1) 古老的Apache Samba VULN
0×01 Vulnhub靶机渗透总结之 Kioptrix: Level 1 (#1) 系列专栏:Vulnhub靶机渗透系列 欢迎大佬:点赞️收藏关注 首发时间: 2023年8月20日 如有错误 还望告 ...
- GaussDB技术解读系列:高安全之密态等值
本文分享自华为云社区< DTCC 2023专家解读 | GaussDB技术解读系列:高安全之密态等值>,作者:GaussDB 数据库. 近日,在第14届中国数据库技术大会(DTCC2023 ...
- Code Llama:Llama 2 学会写代码了!
引言 Code Llama 是为代码类任务而生的一组最先进的.开放的 Llama 2 模型,我们很高兴能将其集成入 Hugging Face 生态系统!Code Llama 使用与 Llama 2 相 ...
- Vue源码学习(三):<templete>渲染第二步,创建ast语法树
好家伙,书接上回 在上一篇Vue源码学习(二):<templete>渲染第一步,模板解析中,我们完成了模板解析 现在我们继续,将模板解析的转换为ast语法树 1.前情提要 代码已开 ...
- SK 简化流行编程语言对 生成式AI 应用开发的支持
Semantic Kernel[1] 是一个将大型语言模型(LLM)与流行的编程语言相结合的SDK. Microsoft将Semantic Kernel(简称SK)称为轻量级SDK,支持AI LLM的 ...
- containerd镜像拉取配置
背景: 公司要求部署最一套新版的k8s系统来部署生产应用,说实话很头疼.因为k8s自1.23版本之后就用不docker作为容器的默认运行时了,而是采用的containerd,这就带来了一系列的问题.没 ...
- strimzi实战之一:简介和准备
欢迎访问我的GitHub 这里分类和汇总了欣宸的全部原创(含配套源码):https://github.com/zq2599/blog_demos 关于strimzi strimzi是一个开源项目,已加 ...
- Centos7中防火墙打开3306端口(亲测)
开放3306端口 firewall-cmd --zone=public --add-port=3306/tcp --permanent 开启防火墙 systemctl start firewa ...
- Arduino – Turn LED ON and OFF With Button
In this Arduino tutorial I will show you how to turn an LED on and off with a push button. In fact, ...
- visio 2010 kit tools
Getting Office License Configuration Information.---------------------------------------Backing Up L ...