Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
3.3 kB
3
Indexable
package Queue;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class Mario {
	static int T, N, M;
	static int map[][];
	static int startR, startC, endR, endC;
	static int cr, cc, nr, nc;
	static boolean visited[][];
	
	static int dx[] = {0, -1, 0, 1};
	static int dy[] = {-1, 0, 1, 0};
	
	public static class Queue{
		static int front, rear, Data[];
		
		public Queue() {
			this.front = this.rear = -1;
			Data = new int[1000000];
		}
		
		public static void reset(){
			front = rear = -1;
		}
		
		public static void enQueue(int value){
			Data[++rear] = value;
		}
		
		public static int deQueue(){
			return Data[++front];
		}
		
		public static boolean is_Empty(){
			return front == rear;
		}
	}
	
	public static void main(String[] args) throws FileNotFoundException{
		System.setIn(new FileInputStream("src/Queue/Mario"));
		Scanner scanner = new Scanner(System.in);
		T = scanner.nextInt();
		for(int tc = 1; tc <= T; tc++){
			N = scanner.nextInt();
			M = scanner.nextInt();
			map = new int[N][M];
			for(int i = 0; i < N; i++){
				for(int j = 0; j < M; j++){
					map[i][j] = scanner.nextInt();
					if(map[i][j] == 2){
						startR = i;
						startC = j;
					}
					if(map[i][j] == 3){
						endR = i;
						endC = j;
					}
				}
			}
			
			
			Queue rQueue = new Queue();
			Queue cQueue = new Queue();
			boolean check = false;
			int h = 1;
			
			
		while(!check) {
				
			rQueue.reset();
			cQueue.reset();
			visited = new boolean[N][M];
			rQueue.enQueue(startR);
			cQueue.enQueue(startC);
			visited[startR][startC] = true;
			while(!rQueue.is_Empty()) {
				cr = rQueue.deQueue();
				cc = cQueue.deQueue();	
					for(int i = 0; i < 4; i++) {
						for(int k = 1; k <= h; k++) {
							nr = cr + k*dx[i];
							nc = cc + dy[i];
							if(nr < 0 || nr >= N || nc < 0 || nc >= M) continue;
							
							if(map[nr][nc] == 3) {
								check = true;
								k = N;
								break;
							}
							
							if(map[nr][nc] == 1 && !visited[nr][nc]) {
								visited[nr][nc] = true;
								rQueue.enQueue(nr);
								cQueue.enQueue(nc);
							}
							
						}
					}
				
				
					if(check) break;
				}
				if(!check) h++;
				
			}
			
			
//			while(!check){
//				
//				for(int k = 1; k <= h; k++){
//					rQueue.enQueue(startR);
//					cQueue.enQueue(startC);
//					visited[startR][startC] = true;
//					while(!rQueue.is_Empty()){
//						cr = rQueue.deQueue();
//						cc = cQueue.deQueue();
//						
//							for(int i = 0; i < 4; i++){
//									nr = cr + k*dx[i];
//									nc = cc + dy[i];
//									if(nr < 0 || nr >= N || nc < 0 || nc >= N) continue;
//									if(map[nr][nc] == 3){
//										check = true;
//										break;
//									}
//									if(!visited[nr][nc] && map[nr][nc] == 1){
//										visited[nr][nc] = true;
//										rQueue.enQueue(nr);
//										cQueue.enQueue(nc);
//										}
//									}
//							if(check) break;
//						}
//					if(check) break;
//					}
//				if(!check) h++;
//			
//			}
//			
			System.out.println("Case #" + tc);
			System.out.println(h);
			
		}
	
		
		
	}

}
Leave a Comment