Untitled
unknown
plain_text
2 years ago
1.6 kB
8
Indexable
#include<iostream>
using namespace std;
int mover[4]={-1,0,1,0};
int movec[4]={0,1,0,-1};
class queue{
int front;
int rear;
int mang[10000];
public:
queue(){
rear=-1;
front=-1;
}
int isempty(){
if(rear==front) return 1;
return 0;
}
int isfull(){
if(rear==9999) return 1;
return 0;
}
void insert(int i){
rear++;
mang[rear]=i;
}
void pop(){
if(isempty()==0) front++;
}
int peek(){
return mang[front+1];;
}
};
int main(){
freopen("input.txt","r",stdin);
int t,m,n,a,b,ans;
int map[105][105];
int check[105][105];
cin >> t;
for(int tc=1;tc<=t;tc++){
queue queue1;
queue queue2;
cin >> n >> m;
for(int i=0;i<m;i++){
for(int j=0;j<n;j++) {
cin >> map[i][j];
check[i][j]=0;
}
}
cin >> a >> b;
check[a][b]=1;
for(int h=0;h<4;h++){
int x=a; int y=b;
x+=mover[h];
y+=movec[h];
if(x>=0 && x<m && y>=0 && y<n && map[x][y]==1) {
map[x][y]=map[a][b]+1;
queue1.insert(x);
queue2.insert(y);
check[x][y]=1;
}
}
while(true){
for(int h=0;h<4;h++){
int x=queue1.peek(); int y=queue2.peek();
x+=mover[h];
y+=movec[h];
if(x>=0 && x<m && y>=0 && y<n && map[x][y]==1 && check[x][y]==0){
map[x][y]=map[queue1.peek()][queue2.peek()]+1;
check[x][y]=1;
queue1.insert(x);
queue2.insert(y);
}
}
queue1.pop();
queue2.pop();
if(queue1.isempty()==1) break;
}
ans=0;
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
if(map[i][j]>ans) ans=map[i][j];
}
}
cout << ans << endl;
}
return 0;
}Editor is loading...
Leave a Comment