题目链接:https://ac.nowcoder.com/acm/contest/924/F
#include <bits/stdc++.h>
using namespace std;
#define debug(x) cerr<<#x<<'='<<(x)<<endl;
#define debugp(x) cerr<<#x<<"= {"<<(x.first)<<", "<<(x.second)<<"}"<<endl;
#define debug2(x, y) cerr<<"{"<<#x<<", "<<#y<<"} = {"<<(x)<<", "<<(y)<<"}"<<endl;
int n, m, M1, M2;
struct node
{
int x, y, step;
}now, nex, st, ed;
int mp[50][50], vis[50][50], dir[8][2];
bool check()
{
if(nex.x >= 0 && nex.x < n && nex.y >= 0 && nex.y < m && !vis[nex.x][nex.y] && mp[nex.x][nex.y] != 0 && mp[nex.x][nex.y] != 2)
return true;
return false;
}
void BFS()
{
queue <node> q;
q.push(st);
vis[st.x][st.y] = 1;
while(!q.empty())
{
now = q.front();
q.pop();
for(int i = 0; i < 8; i++)
{
nex.x = now.x + dir[i][0];
nex.y = now.y + dir[i][1];
nex.step = now.step + 1;
if(check())
{
if(nex.x == ed.x && nex.y == ed.y)
{
cout << nex.step << endl;
return ;
}
vis[nex.x][nex.y] = 1;
q.push(nex);
}
}
}
cout << "0" << endl;
}
int main()
{
scanf("%d%d%d%d",&n, &m, &M1, &M2);
dir[0][0] = M1, dir[0][1] = M2;
dir[1][0] = M2, dir[1][1] = M1;
dir[2][0] = M2, dir[2][1] = -M1;
dir[3][0] = M1, dir[3][1] = -M2;
dir[4][0] = -M1, dir[4][1] = -M2;
dir[5][0] = -M2, dir[5][1] = -M1;
dir[6][0] = -M2, dir[6][1] = M1;
dir[7][0] = -M1, dir[7][1] = M2;
for(int i = 0; i < n; i++)
{
for(int j = 0; j < m; j++)
{
scanf("%d",&mp[i][j]);
if(mp[i][j] == 3)
st.x = i, st.y = j, st.step = 0;
if(mp[i][j] == 4)
ed.x = i, ed.y = j, ed.step = 0;
}
}
BFS();
}