package xj;
import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner;
public class WaterPipe1 { static int T,N,M,sx,sy,cl; static int data[][]; static boolean map[][]; static int Dx[]={-1,0,1,0}; static int Dy[]={0,1,0,-1}; static int lian[][]={ {1,1,1,1}, {1,0,1,0}, {0,1,0,1}, {1,1,0,0}, {0,1,1,0}, {0,0,1,1}, {1,0,0,1}, }; static int blian[][]={ {1,1,1,1}, {1,0,1,0}, {0,1,0,1}, {0,0,1,1}, {1,0,0,1}, {1,1,0,0}, {0,1,1,0}, }; static class Node{ int x; int y; int step; } static Node[]queue; static int maxsum;
public static void main(String[] args) throws FileNotFoundException { /*Scanner sc=new Scanner(System.in);*/ Scanner sc=new Scanner(new File("src/WaterPipe")); T=sc.nextInt(); for (int t = 0; t < T; t++) { N=sc.nextInt(); M=sc.nextInt(); sx=sc.nextInt(); sy=sc.nextInt(); cl=sc.nextInt(); data=new int [N][M]; map=new boolean[N][M]; queue=new Node[N*M]; for (int i = 0; i < N; i++) { for (int j = 0; j < M; j++) { data[i][j]=sc.nextInt(); } } maxsum=0; bfs(sx,sy); System.out.println(maxsum); } }
private static void bfs(int sx,int sy) { int head=0; int tail=1; Node n1=new Node(); n1.x=sx; n1.y=sy; n1.step=1; queue[head]=n1; map[sx][sy]=true; maxsum++; while(head<tail){ for (int i = 0; i < 4; i++) { int nx=queue[head].x; int ny=queue[head].y; int dx=nx+Dx[i]; int dy=ny+Dy[i]; if(dx>=0&&dy>=0&&dx<N&&dy<M){ int a=data[nx][ny]-1; int b=data[dx][dy]-1; if(b!=-1&&lian[a][i]==1&&blian[b][i]==1&&!map[dx][dy]){ //这里要注意下一个b的值可能为-1,为-1则blian越界,所以要注明 ,map使用函数要记住了 Node n2=new Node(); n2.x=dx; n2.y=dy; n2.step=queue[head].step+1; if(n2.step>cl){return;}//这里注意当长度大了就立刻退出 queue[tail]=n2; tail++; map[dx][dy]=true; maxsum++; } } } head++; } }
}
//input
3 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 2 2 0 0 4 3 1 1 5 6 2 1 3 0 0 5 3 6 0 1 0 2 0 2 0 3 3 1 3 7 0 0 0 0 0 0 0 0 0 0 0 0 0 10 10 4 3 9 0 0 0 0 0 0 0 0 0 0 0 0 0 7 5 0 5 0 0 0 0 0 3 2 2 6 0 0 0 0 0 4 7 2 2 2 7 0 0 4 0 3 0 1 1 2 2 0 0 5 0 5 6 1 1 1 1 6 2 5 7 4 1 2 0 0 4 6 0 0 5 3 1 7 0 2 2 6 5 7 7 3 2 1 1 7 1 0 2 7 3 4 0 0 4 0 5 1 0 1 //output
16 5 29