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的更多相关文章

  1. codeforces A. Rook, Bishop and King 解题报告

    题目链接:http://codeforces.com/problemset/problem/370/A 题目意思:根据rook(每次可以移动垂直或水平的任意步数(>=1)),bishop(每次可 ...

  2. Bishop的大作《模式识别与机器学习》Ready to read!

    久仰Bishop的大作“Pattern Recognition and Machine Learning”已久,在我的硬盘里已经驻扎一年有余,怎奈惧其页数浩瀚,始终未敢入手.近日看文献,屡屡引用之.不 ...

  3. CF Rook, Bishop and King

    http://codeforces.com/contest/370/problem/A 题意:车是走直线的,可以走任意多个格子,象是走对角线的,也可以走任意多个格子,而国王可以走直线也可以走对角线,但 ...

  4. Codeforce 370A Rook, Bishop and King 数学规律

    这个题目挺有意思的,给定 起终点,要你求车,象,王分别最少要走多少步 车横竖都能走,而且每步任意走几格,所以它是最容易处理的,如果在同行或者同列,就是1,否则就是2 象要找下规律,象任意对角线都能走, ...

  5. CodeForces 370A Rook, Bishop and King

    此题看似很简单,但实际上有不少细节,WA点不少.分情况处理即可. #include<cmath> #include<cstdio> #include<string> ...

  6. Day3----《Pattern Recognition and Machine Learning》Christopher M. Bishop

    其实今天只花了一点点时间来学习这本书, 如果模型的参数过多,而训练数据又不足够多的话,就会出现overfitting. overfitting可以通过regularization来解决,贝叶斯方法也可 ...

  7. Day2----《Pattern Recognition and Machine Learning》Christopher M. Bishop

    用一个例子来讲述regression. 采用sin(2*pi*x)加入微弱的正态分布噪声的方式来获得一些数据,然后用多项式模型来进行拟合. 在评价模型的准确性时,采用了误差函数的方式,用根均方误差的方 ...

  8. 学习笔记-----《Pattern Recognition and Machine Learning》Christopher M. Bishop

    Preface 模式识别这个词,以前一直不懂是什么意思,直到今年初,才开始打算读这本广为推荐的书,初步了解到,它的大致意思是从数据中发现特征,规律,属于机器学习的一个分支. 在前言中,阐述了什么是模式 ...

  9. [SQL Server] 特殊字符、上标、下标处理

    今天遇到一个问题是往 SQL Server 中导入像m².m³这样的单位数据,可是在 SQL Server 中查看到的都是 m2.m3,于是在网上查了一下资料,顺便摘录下来供日后查阅. 一  Wind ...

  10. 【机器学习Machine Learning】资料大全

    昨天总结了深度学习的资料,今天把机器学习的资料也总结一下(友情提示:有些网站需要"科学上网"^_^) 推荐几本好书: 1.Pattern Recognition and Machi ...

随机推荐

  1. Linq关联两个DataTable合并为一个DataTable

    DataSet ds ; DataTable dt1= ds.Tables[0]; DataTable dt2= ds.Tables[1]; //关联 var res = from m in dt1. ...

  2. springboot整合feign的接口抽离

    前言 现在很多微服务框架使用feign来进行服务间的调用,需要在服务端和消费端两边分别对接口和请求返回实体进行编码,维护起来也比较麻烦.那有木有一种可能,只用服务端编写接口,客户端像本地方法一样调用, ...

  3. ffmpeg 在xp和server2003/2008/2012上修复无法定位GetNumaNodeProcessorMaskEx的问题

    问题 在给开发一个手机视频网站时需要用到ffmpeg截取视频缩略图, 把项目提交到服务器(server2003/ server2008)上时, 发现在调用命令时会出现错误"无法定位GetNu ...

  4. Unity 游戏开发、02 基础篇 | 知识补充、简单使用动画、动画状态机

    前置笔记(由浅入深) Unity 游戏开发.01 基础篇 2 场景操作 3D场景 Q 手型工具(鼠标中键):上下左右移动场景 ALT + 鼠标左键:以视图为中心旋转 鼠标右键:以观察者为中心旋转 SH ...

  5. 「coci 2021-2022 #1」Logičari

    link. 断环后把断的边所连的两个点特殊标记,作为两个特殊点.这样就是一个树,树的做法很简单吧,把两个特殊点特殊处理带进状态即可. 具体一点就是,设 \(f(x,c_x,c_f,c_{rt_1},c ...

  6. 2023版:深度比较几种.NET Excel导出库的性能差异

    引言 背景和目的 本文介绍了几个常用的电子表格处理库,包括EPPlus.NPOI.Aspose.Cells和DocumentFormat.OpenXml,我们将对这些库进行性能测评,以便为开发人员提供 ...

  7. 一个 println 竟然比 volatile 还好使?

    前两天一个小伙伴突然找我求助,说准备换个坑,最近在系统复习多线程知识,但遇到了一个刷新认知的问题-- 小伙伴:Effective JAVA 里的并发章节里,有一段关于可见性的描述.下面这段代码会出现死 ...

  8. Quantitative Relationship Induction

    数量关系是指事物之间的数值或数量之间的相互关系(+.-.*./). 数量关系描述各种量的变化和相互关系.数量关系可以包括数值的比较.增减.比例.百分比.平均值等方面. 在数学中,数量关系可以通过代数方 ...

  9. LeetCode 周赛上分之旅 #49 再探内向基环树

    ️ 本文已收录到 AndroidFamily,技术和职场问题,请关注公众号 [彭旭锐] 和 BaguTree Pro 知识星球提问. 学习数据结构与算法的关键在于掌握问题背后的算法思维框架,你的思考越 ...

  10. WebKit Insie: Active 样式表

    WebKit Inside: CSS 样式表的匹配时机介绍了当 HTML 页面有不同 CSS 样式表引入时,CSS 样式表开始匹配的时机.后续文章继续介绍 CSS 样式表的匹配过程,但是在匹配之前,首 ...