Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
3.3 kB
1
Indexable
Never
#include <iostream>
using namespace std;
int n, m;
int sx, sy;
int p;
int map[50][50];
int Qx[3000];
int Qy[3000];
int Qd[3000];
int r = -1, f = -1;
int dx[4] = {0,0,1,-1};
int dy[4] = {1,-1,0,0};
bool visit[50][50];
void push(int x, int y, int d){
	r++;
	Qx[r] = x;
	Qx[r] = y;
	Qy[d] = d;
}
void pop(int &x, int &y, int &d){
	f++;
	x = Qx[f];
	y = Qy[f];
	d = Qd[f];
}
bool check(int a, int b, int xa, int ya, int xb, int yb){
	int xx = xa - xb;
	int yy = ya - yb;
	if(a == 1){
		if(xx == 1 && yy == 0) { // tren
			if(b == 2 || b == 5 || b == 6 || b == 1) return true;
		}
		if(xx == -1 && yy == 0){ // duoi
			if(b == 7 || b == 4 || b == 2 || b == 1) return true;
		}
		if(xx == 0 && yy == 1){ // trai 
			if(b == 3 || b == 4 || b == 5 || b == 1) return true;
		}
		if(xx == 0 && yy == -1){ // phai
			if(b == 3 || b == 6 || b == 7 || b == 1) return true;
		}
	}
	 if (a == 2){
		if(xx == 1 && yy == 0) {
			if(b == 1 || b == 5 || b == 6 || b == 2) return true;
		}
		if(xx == -1 && yy == 0){
			if(b == 1 || b == 4 || b ==7 || b == 2) return true;
		}
	}
	 if (a == 3){
		if(xx == 0 && yy == 1){ // trai 
			if(b == 1 || b == 4 || b == 5 || b == 3) return true;
		}
		if(xx == 0 && yy == -1){ // phai
			if(b == 1 || b == 6 || b == 7 || b == 3) return true;
		}
	}
	if (a == 4){
		if(xx == 1 && yy == 0) { // tren
			if(b == 2 || b == 5 || b == 1 || b == 6) return true;
		}
		if(xx == 0 && yy == -1){ // phai
			if(b == 3 || b == 6 || b == 7 || b == 1) return true;
		}
	}
	 if (a == 5){
		if(xx == -1 && yy == 0){ // duoi
			if(b == 7 || b == 4 || b == 2 || b == 1) return true;
		}
		if(xx == 0 && yy == -1){ // phai
			if(b == 3 || b == 6 || b == 7 || b == 1) return true;
		}
	}
	 if (a == 6){
		if(xx == -1 && yy == 0){ // duoi
			if(b == 7 || b == 4 || b == 2 || b == 1) return true;
		}
		if(xx == 0 && yy == 1){ // trai 
			if(b == 3 || b == 4 || b == 5 || b == 1) return true;
		}
	
	}
	 if(a == 7){
		if(xx == 0 && yy == 1){ // trai 
			if(b == 3 || b == 4 || b == 5 || b == 1) return true;
		}
		if(xx == 1 && yy == 0) { // tren
			if(b == 2 || b == 5 || b == 6 || b == 1) return true;
		}
	}
	return false;
}
void BFS(int x, int y, int k){
	r = f = -1;
	push(x,y,k);
	visit[x][y] = true;
	while( r != y){
		pop(x,y,k);
		for(int i = 0; i < 4; i++){
			int xx = x + dx[i];
			int yy = y + dy[i];
			if(xx >= 0 && xx < n && yy >= 0 && yy < m){
				if(visit[xx][yy] == false && k - 1 >= 0){
					if(check(map[x][y], map[xx][yy], x,y,xx,yy)){
						push(xx,yy,k-1);
						visit[xx][yy] = true;
					}
				}
			}
		}
	}
}
int main(){
	freopen("input.txt", "r", stdin);
	int T;
	cin >> T;
	for(int tc = 1; tc <= T; tc++){
		cin >> n >> m >> sx >> sy >> p;
		for(int i = 0; i < n; i++){
			for(int j = 0; j < m; j++){
				cin >> map[i][j];
				visit[i][j] = false;
			}
		}
		for(int i = 0; i < 4; i++){
			int xx = sx + dx[i];
			int yy = sy + dy[i];
			if(xx >= 0 && xx < n && yy >= 0 && yy < m){
				if(map[xx][yy] != 0) BFS(xx,yy,p);
			}
		}
		int dem = 0;
		for(int i = 0; i < n; i++){
			for(int j = 0; j < m; j++){
				if(visit[i][j] == true) dem++;
			}
		}
		cout << dem << endl;
	}
	return 0;
}