这道题本来没啥好说的,但细节实在比较多,被坑了好多次。
#include <bits/stdc++.h>
#define MAX 55
using namespace std;
int mod(int x){
return (x+4)%4;
}
struct pt{
int x, y, dir, step;
pt(){}
pt(int a, int b, int c, int d):x(a), y(b), dir(c), step(d){}
};
const int movx[] = {0,1,0,-1}, movy[] = {1,0,-1,0};
int a[MAX][MAX];
bool vis[MAX][MAX][5];
int n, m;
pt st, ed;
void bfs(){
queue<pt> q;
bool flag = false;
st.step = 0;
q.push(st);
vis[st.x][st.y][st.dir] = true;
while(!q.empty()){
pt t = q.front();
q.pop();
if(t.x == ed.x && t.y == ed.y){
cout << t.step << endl;
flag = true;
break;
}
for(int i = 1; i <= 3; i++){
int u, v;
u = t.x + i*movx[t.dir];
v = t.y + i*movy[t.dir];
if(u<=0 || u>=n || v<=0 || v>=m || a[u][v] == 1){
break;
}
if(vis[u][v][t.dir]){
continue;
}
vis[u][v][t.dir] = true;
q.push(pt(u, v, t.dir, t.step+1));
}
if(!vis[t.x][t.y][mod(t.dir+1)]){
vis[t.x][t.y][mod(t.dir+1)] = true;
q.push(pt(t.x, t.y, mod(t.dir+1), t.step+1));
}
if(!vis[t.x][t.y][mod(t.dir-1)]){
vis[t.x][t.y][mod(t.dir-1)] = true;
q.push(pt(t.x, t.y, mod(t.dir-1), t.step+1));
}
}
if(!flag){
cout << -1 << endl;
}
}
int main()
{
cin >> n >> m;
for(int i = 1; i <= n; i++){
for(int j = 1; j <= m; j++){
scanf("%d", &a[i][j]);
if(a[i][j] == 1){
a[i-1][j-1] = a[i-1][j] = a[i][j-1] = 1;
}
}
}
cin >> st.x >> st.y >> ed.x >> ed.y;
char c;
cin >> c;
switch(c){
case 'E':
st.dir = 0; break;
case 'S':
st.dir = 1; break;
case 'W':
st.dir = 2; break;
case 'N':
st.dir = 3; break;
}
bfs();
return 0;
}
?
Chrome是目前最流行的浏览器,也是综合体验最好的浏览器之一,但这并不意味着它...
在Linux终端中有两种常用的检查磁盘使用情况的方法:du命令和df命令。du命令更多...
% dim choose,path,mydb,myset,SQL,fs,mp3 ' 获得用户提交表单 (list.htm) 选中...
动机: 最初想起做二叉树是因为需要做一个公司结构图。 以前的做法都是直接用图...
Array () 函数返回一个数组 表达式Array(list) 允许数据类型:字符,数字均可 实...
微软前不久面向Windows 10用户推送的3月累积更新补丁KB5000802惹祸,造成京瓷、...
电脑不想装几十个G的 VS2017,那就用 VS Code 吧 目标: 创建一个类库项目 Skany...
1.开发环境 vue+element 2.电脑系统 windows 10 专业版 3.在开发的过程中,我们总...
百度ueditor组件,使用上传图片后,自动将上传图片显示在编辑器中,也就是插入了...
之前,我们已经写了许多可用于监控Linux系统性能的Linux系统监控工具,但是我们...