Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
1.8 kB
3
Indexable
Never
#include <iostream>
using namespace std;
int head = -1;
int tail = -1;
int queue[10009];
int queue1[10009];
void Enqueue(int queue[], int queue1[], int element,int element1, int& tail) {
    queue[tail] = element;
	queue1[tail] = element1;
	tail++;
}
void Dequeue(int queue[], int queue1[], int& head) {
    queue[head] = 0; 
	queue1[head] = 0;
    head++;
}
int Front(int queue[], int head) {
    return queue[head];
}
int main(){
	freopen("input.txt","r",stdin);
	int T;
	cin >> T;
	for(int TestCase = 0;TestCase < T;TestCase++){
		int n,m;
		cin >> n >> m;
		int arr[101][101];
		for (int i = 0;i<m;i++){
			for(int j = 0;j<n;j++){
				cin >> arr[i][j];
			}
		}

		int x,y;
		cin >> x >> y;
		head = -1;
		tail = -1;
		int check = 1;
		Enqueue(queue,queue1,x-1,y-1,tail);
		arr[y-1][x-1] = 2;
		while(tail-head > 0){
			int cow = Front(queue, head);
			int row = Front(queue1, head);
			Dequeue(queue,queue1,head);
			if(row > 0){
				if(arr[row-1][cow] == 1) {
					Enqueue(queue,queue1,cow,row-1,tail);
					arr[row-1][cow] = arr[row][cow]+1;
				}
			}
			if(row < m-1){
				if(arr[row+1][cow] == 1) {
					Enqueue(queue,queue1,cow,row+1,tail);
					arr[row+1][cow] = arr[row][cow]+1;
				}
			}
			if(cow > 0){
				if(arr[row][cow-1] == 1) {
					Enqueue(queue,queue1,cow-1,row,tail);
					arr[row][cow-1] = arr[row][cow]+1;
				}
			}
			if(cow < n-1){
				if(arr[row][cow+1] == 1) {
					Enqueue(queue,queue1,cow+1,row,tail);
					arr[row][cow+1] = arr[row][cow]+1;
				}
			}
		}
		int max = 0;
		for (int i = 0;i<m;i++){
			for(int j = 0;j<n;j++){
				if(arr[i][j] >max){
					max = arr[i][j];
				}
			}
		}
		cout << "#" << TestCase +1 << " " << max-1 << endl;
	}
	return 0;
}