#include<bits/stdc++.h>
using namespace std;
int n,m;
int x;
int sx,sy;
int ex,ey;
int change_A[5]={0,0,0,-1,1};
int change_B[5]={0,1,-1,0,0};
struct node{
int x;
int y;
int step;
int hp;
}h[1000];
bool vis[100][100];
bool mouse[100][100];
void input()
{
scanf("%d %d",&n,&m);
memset(mouse,false,sizeof(mouse));
memset(vis,false,sizeof(vis));
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
{
scanf("%d",&x);
switch(x)
{
case 0:
break;
case 1:
vis[i][j]=true;
break;
case 2:
vis[i][j]=true;
h[1].x=i;
h[1].y=j;
h[1].step=0;
h[1].hp=6;
sx=i;
sy=j;
break;
case 3:
vis[i][j]=true;
ex=i;
ey=j;
break;
case 4:
vis[i][j]=true;
mouse[i][j]=true;
break;
}
}
}
bool in(int x,int y)
{
return (x>0 and y>0 and x<=n and y<=m);
}
void bfs()
{
int t=0,w=1;
while(t<w)
{
t++;
for(int i=1;i<=4;i++)
{
int x=h[t].x+change_A[i];
int y=h[t].y+change_B[i];
int step=h[t].step+1;
int hp=h[t].hp-1;
if(in(x,y) and hp>=1 and vis[x][y])
{
w++;
h[w].x=x;
h[w].y=y;
h[w].step=step;
h[w].hp=hp;
vis[x][y]=false;
if(x==ex and y==ey)
{
printf("%d\n",h[w].step);
return ;
}
else if(mouse[x][y])
h[w].hp=6,vis[x][y]=true;
}
}
}
printf("-1\n");
return ;
}
int main()
{
input();
bfs();
return 0;
}
84分寄了两个点
寄点:
Sample input:
8 4
3 1 1 1
1 1 1 0
1 1 1 1
0 1 1 2
1 4 4 1
1 4 0 1
1 0 1 1
1 0 1 1
Sample output:
8
My output:
-1