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. 多层前馈神经网络及BP算法

    一.多层前馈神经网络 首先说下多层前馈神经网络,BP算法,BP神经网络之间的关系.多层前馈[multilayer feed-forward]神经网络由一个输入层.一个或多个隐藏层和一个输出层组成,后向 ...

  2. 发布策略:蓝绿部署、金丝雀发布(灰度发布)、AB测试、滚动发布、红黑部署的概念与区别

    蓝绿发布(Blue-Green Deployment) 蓝绿发布提供了一种零宕机的部署方式.不停老版本,部署新版本进行测试,确认OK,将流量切到新版本,然后老版本同时也升级到新版本.始终有两个版本同时 ...

  3. Adobe全家桶PS、PR、AU等2022正版永久有效,无需破解直接安装就能用

    [Adobe全家桶]已经亲测绝对好用,下载地址: 关注我的wx公众号"奋斗在IT"回复1013获取下载地址.

  4. Row Major

    Smiling & Weeping ----昨天, 别人在我身旁大声说出你的名字, 这对于我, 像从敞开的窗口扔进了一朵玫瑰花. 思路:不客气地说,这是一道令人费解的题目,要求构造一个字符串, ...

  5. LeetCode 周赛上分之旅 #45 精妙的 O(lgn) 扫描算法与树上 DP 问题

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

  6. Ds100p -「数据结构百题」41~50

    41.P3590 [POI2015]TRZ 给定一个长度为n的仅包含'B'.'C'.'S'三种字符的字符串,请找到最长的一段连续子串,使得这一段要么只有一种字符,要么有多种字符,但是没有任意两种字符出 ...

  7. cmake构建32位应用程序

    1. 背景介绍 2. 工具介绍 3. 环境搭建 4. MinGW编译器版本 1. 背景介绍 最近需要使用第三方动态库文件G33DDCAPI.dll进行二次开发.由于这个动态库文件生成的时间比较早,且只 ...

  8. socket应用的例子

    当使用 C 语言实现 Socket 编程时,可以通过系统提供的网络库来实现网络通信.以下是一个简单的示例,演示了如何创建一个简单的服务器和客户端,实现客户端向服务器发送消息并接收回复的功能. 服务器端 ...

  9. 蓝桥杯真题——第十三届蓝桥杯大赛软件赛省赛 Python 大学 B 组

  10. 其它——paramiko模块的使用

    文章目录 paramiko 一 介绍 二 通过用户名密码方式远程执行命令 三 通过用户名密码方式上传下载文件 四 通过公钥私钥远程执行命令 五 通过公钥私钥远程上传下载文件 六 通过私钥字符串远程连接 ...