Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
2.1 kB
1
Indexable
#include <iostream>
using namespace std;
int main(){
	freopen("input.txt","r",stdin);
	int T,x;
	cin>>T;
	for (x = 0; x < T; x++){
		int N, dem = 0 , huong;
		cin>>N;
		int x0 = 0, y0 = 0;
		int A[100][100];
		int i, j;
		for (i = 0; i < N; i++){
			for (j = 0; j < N; j++){
				cin>> A[i][j];
			}
		}
		huong = 0;
		while ((x0!=0 || dem<=1) && x0 >= 0 && x0 <N && y0>=0 && y0 < N){
			if (huong == 0){
				while (y0 < N-2 && A[x0][y0+1] == 0){
					y0++;
				}
				y0++;
				dem++;
				if (A[x0][y0] == 1){
					huong = 2;
				} else if (A[x0][y0] == 2){
					huong = 3;
				}
			}
			else if (huong == 1){
				while (y0 >= 2 && A[x0][y0-1] == 0){
					y0--;
				}
				y0--;
				dem++;
				if (A[x0][y0] == 1){
					huong = 3;
				} else if (A[x0][y0] == 2){
					huong = 2;
				}
			}
			else if (huong == 2){
				while (x0 >= 2 && A[x0-1][y0] == 0){
					x0--;
				}
				x0--;
				dem++;
				if (A[x0][y0] == 1){
					huong = 0;
				} else if (A[x0][y0] == 2){
					huong = 1;
				}
			}
			else if (huong == 3){
				while (x0 < N-2 && A[x0+1][y0] == 0){
					x0++;
				}
				x0++;
				dem++;
				if (A[x0][y0] == 1){
					huong = 1;
				} else if (A[x0][y0] == 2){
					huong = 0;
				}
			}
		}
		cout<<"#"<<x+1<<" "<<dem-1<<endl;
	}
	return 0;
}
/*
4
10
0 0 0 0 0 0 0 2 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 1 2 0 0 0 1
0 0 0 0 0 0 0 0 0 0
0 0 0 0 1 0 0 1 0 0
1 0 0 0 2 0 0 2 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 2 0 0 0 0 1 0 0
0 0 0 0 0 0 0 0 0 0
5
0 2 0 0 0
0 0 0 0 0
0 2 0 1 0
2 0 0 1 0
0 2 0 0 0
10
0 0 0 0 0 0 2 0 0 0 
0 0 0 0 0 0 0 0 0 0 
0 0 0 2 0 0 0 0 0 0 
0 0 2 1 0 0 0 2 0 0 
0 0 0 0 0 1 0 1 0 0 
0 0 0 0 2 0 0 0 1 0 
0 2 0 0 0 0 1 0 0 1 
0 0 0 0 0 0 2 0 0 0 
0 0 0 0 1 0 0 0 0 0 
0 0 2 0 0 0 0 0 0 1 
10
0 0 0 0 0 0 2 0 0 0 
0 0 0 0 0 0 0 2 0 1 
0 0 0 0 0 0 0 0 0 0 
0 1 1 0 0 0 0 0 0 0 
0 0 0 0 0 2 2 0 0 1 
0 0 0 0 0 0 0 2 0 1 
0 0 0 2 1 0 2 0 2 0 
0 0 0 0 0 1 0 0 0 0 
0 0 0 0 0 0 0 0 0 1 
0 1 0 0 0 0 0 0 0 0

*/