我惠美如画 @ 2019-01-02 18:15:33
#include<cstdio>
#include<iostream>
#include<queue>
#include<cstring>
#include<string>
using namespace std;
struct node{
int x,y,cnt;
};
int fx[8],fy[8];
bool vis[1005][1005];
int map[1005][1005];
int x,y;
int stx,sty,enx,eny;
int n,m,a,b;
int bfs()
{
queue<node>q;
q.push((node){stx,sty,0});
while( ! q.empty()){
int nx = q.front().x;
int ny = q.front().y;
int cnt =q.front().cnt;
if(nx == enx && ny ==eny) return cnt;
q.pop();
for(int i = 0;i < 8;i ++ ){
int tx = x + fx[i];
int ty = y + fx[i];
if(tx < 1||tx > n||ty < 1|| ty > m) continue;
if(vis[tx][ty] == true || map[tx][ty] == 0) continue;
if(tx == nx&& ty == ny) return cnt+1;
q.push((node){tx,ty,cnt+1});
vis[tx][ty]=true;
}
}
}
void start()
{
fx[0]= a,fy[0]=b;
fx[1]= a,fy[1]=-b;
fx[2]=-a,fy[2]=-b;
fx[3]=-a,fy[3]=b;
fx[4]= b,fy[4]=a;
fx[5]=-b,fy[5]=-a;
fx[6]= b,fy[6]=-a;
fx[7]=-b,fy[7]=a;
}
void input()
{
start();
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
scanf("%d",&map[i][j]);
if(map[i][j]==2) map[i][j]=0;
if(map[i][j]==3) stx=i,sty=j;
if(map[i][j]==4) enx=i,eny=j;
}
}
}
int main()
{
// freopen("difsi.in","r",stdin);
// freopen("difsi.out","w",stdout);
scanf("%d%d%d%d",&n,&m,&a,&b);
input();
start();
printf("%d",bfs());
// fclose(stdin); fclose(stdout);
return 0;
}
by __wfx @ 2019-01-02 21:44:03
将
int tx = x + fx[i];
int ty = y + fx[i];
改为
int tx = nx + fx[i];
int ty = ny + fy[i];
就a了 @我惠美如画