Untitled

mail@pastecode.io avatar
unknown
plain_text
7 months ago
3.0 kB
1
Indexable
Never
#include <iostream>
#include<queue>
#include<stdio.h>
 
#define MAX_N 1002
 
using namespace std;
 
 
int n, m;
int a[MAX_N][MAX_N];
int b[MAX_N][MAX_N];
bool visited[MAX_N][MAX_N];
int x_start, y_start;
int k;
 
queue<int> q1, q2;
 
int dx[4] = {-1, 1, 0, 0};
int dy[4] = {0, 0, -1, 1};
 
int main() {
	int t;
	cin >> t;
	while(t--){
		cin >> m >> n >> x_start >> y_start >> k;
		for (int i = 0; i < m; i++){
			for (int j = 0; j < n; j++){
				cin >> a[i][j];
			}
		}
 
		for (int i = 0; i < m; i++){
			for (int j = 0; j < n; j++){
				visited[i][j] = false;
				b[i][j] = -1;
			}
		}
 
		visited[x_start][y_start] = true;
		b[x_start][y_start] = 1;
		q1.push(x_start);
		q2.push(y_start);
 
		while(!q1.empty()){
			int i = q1.front();
			int j = q2.front();
			q1.pop();
			q2.pop();
			for (int k = 0; k < 4; k++){
				int ii = i + dx[k];
				int jj = j + dy[k];
				if (ii >= 0 && ii < m && jj >= 0 && jj < n){
					if (visited[ii][jj] || a[ii][jj] == 0) continue;
					if (a[i][j] == 1){
						
						if (k == 0 && (a[ii][jj] == 3 || a[ii][jj] == 7 || a[ii][jj] == 4)) continue;
						if (k == 1 && (a[ii][jj] == 3 || a[ii][jj] == 5 || a[ii][jj] == 6)) continue;
						if (k == 2 && (a[ii][jj] == 2 || a[ii][jj] == 7 || a[ii][jj] == 6)) continue;
						if (k == 3 && (a[ii][jj] == 2 || a[ii][jj] == 5 || a[ii][jj] == 4)) continue;
					}
					
					if (a[i][j] == 2){
						if (k == 3 || k == 2) continue;
						if (k == 0 && (a[ii][jj] == 3 || a[ii][jj] == 7 || a[ii][jj] == 4)) continue;
						if (k == 1 && (a[ii][jj] == 3 || a[ii][jj] == 5 || a[ii][jj] == 6)) continue;
					}
					if (a[i][j] == 3){
						if (k == 1 || k == 0) continue;
						if (k == 2 && (a[ii][jj] == 2 || a[ii][jj] == 7 || a[ii][jj] == 6)) continue;
						if (k == 3 && (a[ii][jj] == 2 || a[ii][jj] == 5 || a[ii][jj] == 4)) continue;
					}
 
					if (a[i][j] == 4){
						if (k == 1 || k == 2) continue;
						if (k == 0 && (a[ii][jj] == 3 || a[ii][jj] == 7)) continue;
						if (k == 3 && (a[ii][jj] == 2 || a[ii][jj] == 5)) continue;
					}
 
					if (a[i][j] == 5){
						if (k == 0 || k == 2) continue;
						if (k == 1 && (a[ii][jj] == 3 || a[ii][jj] == 6)) continue;
						if (k == 3 && a[ii][jj] == 2 || a[ii][jj] == 4) continue;
					}
 
					if (a[i][j] == 6){
						if (k == 0 || k == 3) continue;
						if (k == 1 && (a[ii][jj] == 3 || a[ii][jj] == 5)) continue;
						if (k == 2 && (a[ii][jj] == 2 || a[ii][jj] == 7)) continue;
					}
 
					if (a[i][j] == 7){
						if (k == 1 || k == 3) continue;
						if (k == 0 && (a[ii][jj] == 3 || a[ii][jj] == 4)) continue;
						if (k == 2 && (a[ii][jj] == 2 || a[ii][jj] == 6)) continue;
					}
 
 
					visited[ii][jj] = true;
					b[ii][jj] = b[i][j] + 1;
					q1.push(ii);
					q2.push(jj);
				}
			}
		}
		int res = 0;
		for (int i = 0; i < m; i++){
			//cout << endl;
			for (int j = 0; j < n; j++){
				//cout << b[i][j] << " ";
				if(b[i][j] > 0 && b[i][j] <= k) res++;
			}
		}
 
		cout << res << endl;
 
 
 
		
 
	}
}
Leave a Comment