Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- int n, m, ans;
- int a[100][100];
- int seen[100][100];
- bool valid(int x, int y){
- return (x >= 0 and x < n and y >= 0 and y<m);
- }
- int right(int x, int y){
- if(valid(x, y)==true){
- int k = a[x][y];
- if(k==1 or k==3 or k==4 or k==5){
- return true;
- }
- }
- return false;
- }
- int left(int x, int y){
- if(valid(x, y)==true){
- int k = a[x][y];
- if(k==1 or k==3 or k==6 or k==7){
- return true;
- }
- }
- return false;
- }
- int up(int x, int y){
- if(valid(x, y)==true){
- int k = a[x][y];
- if(k==1 or k==2 or k==4 or k==7){
- return true;
- }
- }
- return false;
- }
- int down(int x, int y){
- if(valid(x, y)==true){
- int k = a[x][y];
- if(k==1 or k==2 or k==5 or k==6){
- return true;
- }
- }
- return false;
- }
- void solve(int x, int y, int l){
- if(l == 0){
- return;
- }
- if(seen[x][y]==0){
- seen[x][y] = 1;
- ans++;
- }
- if(right(x, y) and left(x, y+1)){
- solve(x, y+1, l-1);
- }
- if(left(x, y) and right(x, y-1)){
- solve(x, y-1, l-1);
- }
- if(up(x, y) and down(x-1, y)){
- solve(x-1, y, l-1);
- }
- if(down(x, y) and up(x+1, y)){
- solve(x+1, y, l-1);
- }
- }
- int main(){
- int t;
- cin >> t;
- while(t--){
- int x, y, l;
- cin >> n >> m >> x >> y >> l;
- for(int i = 0; i < n; i++){
- for(int j = 0; j < m; j++){
- cin >> a[i][j];
- seen[i][j] = 0;
- }
- }
- ans = 0;
- solve(x, y, l);
- cout << ans << endl;
- }
- return 0;
- }
- /*2
- 5 6 2 1 3
- 0 0 5 3 6 0
- 0 0 2 0 2 0
- 3 3 1 3 7 0
- 0 0 0 0 0 0
- 0 0 0 0 0 0
- 5 6 2 2 6
- 3 0 0 0 0 3
- 2 0 0 0 0 6
- 1 3 1 1 3 1
- 2 0 2 0 0 2
- 0 0 4 3 1 1
- 5
- 15
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement