//\\__ hr1212 __//\\
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> pii;
typedef map<int,int> mi;
#define si(a) scanf("%d",&a)
#define sii(a,b) scanf("%d %d",&a,&b)
#define siii(a,b,c) scanf("%d %d %d",&a,&b,&c)
#define pi(a) printf("%d\n",a)
#define nl printf("\n");
#define pb push_back
#define mp make_pair
#define all(c) (c).begin(),(c).end()
#define f(i,a,b) for(i=a;i<b;i++)
#define rf(i,a,b) for(i=a;i>=b;i--)
#define clr(x,a) memset(x,a,sizeof(x))
#define MAX 1000100
#define MOD 1000000007
int n,m,cnt[1005][1005],a[1005][1005];
int dx[]={0,0,1,-1};
int dy[]={1,-1,0,0};
int main(){
int r,k,i,c=0,x=0,y=0,j,t,l,z,x1=0,y1=0,x2,y2;
int ans=0,p,qq;
while(1){
sii(n,m);
if(n==0 && m==0)
break;
clr(a,0);
clr(cnt,0);
si(z);
f(i,0,z){
si(x);
si(k);
f(j,0,k){
si(y);
a[x][y]=1;
}
}
sii(x1,y1);
sii(x2,y2);
ans=0;
queue< pii > q;
if(!a[x1][y1] && !a[x2][y2])
q.push((mp(x1,y1)));
while(!q.empty()){
pii pp=q.front();
q.pop();
x=pp.first;y=pp.second;
if(x==x2 && y==y2){
pi(cnt[x2][y2]);
break;
}
f(i,0,4){
p=x+dx[i];qq=y+dy[i];
if(p>=0 && p<n && qq>=0 && qq<m && !a[p][qq] && !cnt[p][qq]){
cnt[p][qq]=cnt[x][y]+1;
q.push((mp(p,qq)));
}
}
}
}
return 0;
}
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> pii;
typedef map<int,int> mi;
#define si(a) scanf("%d",&a)
#define sii(a,b) scanf("%d %d",&a,&b)
#define siii(a,b,c) scanf("%d %d %d",&a,&b,&c)
#define pi(a) printf("%d\n",a)
#define nl printf("\n");
#define pb push_back
#define mp make_pair
#define all(c) (c).begin(),(c).end()
#define f(i,a,b) for(i=a;i<b;i++)
#define rf(i,a,b) for(i=a;i>=b;i--)
#define clr(x,a) memset(x,a,sizeof(x))
#define MAX 1000100
#define MOD 1000000007
int n,m,cnt[1005][1005],a[1005][1005];
int dx[]={0,0,1,-1};
int dy[]={1,-1,0,0};
int main(){
int r,k,i,c=0,x=0,y=0,j,t,l,z,x1=0,y1=0,x2,y2;
int ans=0,p,qq;
while(1){
sii(n,m);
if(n==0 && m==0)
break;
clr(a,0);
clr(cnt,0);
si(z);
f(i,0,z){
si(x);
si(k);
f(j,0,k){
si(y);
a[x][y]=1;
}
}
sii(x1,y1);
sii(x2,y2);
ans=0;
queue< pii > q;
if(!a[x1][y1] && !a[x2][y2])
q.push((mp(x1,y1)));
while(!q.empty()){
pii pp=q.front();
q.pop();
x=pp.first;y=pp.second;
if(x==x2 && y==y2){
pi(cnt[x2][y2]);
break;
}
f(i,0,4){
p=x+dx[i];qq=y+dy[i];
if(p>=0 && p<n && qq>=0 && qq<m && !a[p][qq] && !cnt[p][qq]){
cnt[p][qq]=cnt[x][y]+1;
q.push((mp(p,qq)));
}
}
}
}
return 0;
}
No comments:
Post a Comment