Code: Select all
#include<iostream>
#include<queue>
#include<ctime>
using namespace std;
static int m[1010][1010];
int sx,sy,dx,dy,num_row,num_column;
int dif_x[] = {1,-1,0,0};
int dif_y[] = {0,0,1,-1};
typedef struct
{
int x,y,steps;
} point;
int search_dest(int x,int y)
{
point start;
start.x=x;
start.y=y;
start.steps=0;
queue<point>q;
q.push(start);
m[x][y]=1;
while(!q.empty())
{
point top=q.front();
q.pop();
x=top.x;
y=top.y;
if(x==dx && y==dy) return (top.steps);
for(int i=0;i<3;i++)
{
x=x+dif_x[i];
y=y+dif_y[i];
if(x>=0 && y>=0 && x<=num_column && y<=num_row && m[x][y]==0)
{
point next;
next.steps=top.steps+1;;
next.x=x;
next.y=y;
m[x][y]=1;
q.push(next);
}
}
}
return 0;
}
int main()
{
//int start_s=clock();
// the code you wish to time goes here
int y,x,num_bombs,row;
while(1)
{
cin>>num_row>>num_column;
if(num_row==0 && num_column==0) break;
cin>>row;
for(int i=0;i<row;i++)
{
cin>>x;
cin>>num_bombs;
for(int j=0;j<num_bombs;j++)
{
cin>>y;
m[x][y]=1;
}
}
cin>>sx>>sy;
cin>>dx>>dy;
cout<<search_dest(sx,sy)<<endl;
//int stop_s=clock();
//cout << "time: " << (stop_s-start_s)/double(CLOCKS_PER_SEC)*1000000 << endl;
}
return 0;
}