Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- int main()
- {
- int t,caseno=1;;
- cin>> t;
- while(t--){
- int n;
- cin>> n;
- vector <int> v[n+5]; //You can use 2D array instead of vector array
- for(int a=0;a<n;a++){
- for(int b=0;b<n;b++){
- int x;
- cin>> x;
- v[a].push_back(x);
- //taking input
- //If you use 2D array, just replace with cin>> arr[a][b]
- }
- }
- int cnt=0;
- //checking all 2x5 rectangles
- for(int a=0;a<n-1;a++){
- for(int b=0;b<n-4;b++){
- int i=a+2,j=b+5;
- vector <int> arr(12,0);
- for(int p=a;p<i;p++){
- for(int q=b;q<j;q++){
- int z=v[p][q];
- arr[z]++;
- if(arr[z]>1) goto next;
- //Each rectangele should have each number from 1 to 10 exactly once.
- //If any number from 1 to 10 appears more than once and check next rectangle
- //I used goto statement to break two loops at a time.
- }
- }
- cnt++;
- next:
- i=0;
- }
- }
- //checking all 5x2 rectangles
- for(int a=0;a<n-4;a++){
- for(int b=0;b<n-1;b++){
- int i=a+5,j=b+2;
- vector <int> arr(12,0);
- for(int p=a;p<i;p++){
- for(int q=b;q<j;q++){
- int z=v[p][q];
- arr[z]++;
- if(arr[z]>1) goto next1;
- }
- }
- cnt++;
- next1:
- i=0;
- }
- }
- //checking all 1x10 rectangles
- for(int a=0;a<n;a++){
- for(int b=0;b<n-9;b++){
- int i=a+1,j=b+10;
- vector <int> arr(12,0);
- for(int p=a;p<i;p++){
- for(int q=b;q<j;q++){
- int z=v[p][q];
- arr[z]++;
- if(arr[z]>1) goto next2;
- }
- }
- cnt++;
- next2:
- i=0;
- }
- }
- //checking all 10x1 rectangles
- for(int a=0;a<n-9;a++){
- for(int b=0;b<n;b++){
- int i=a+10,j=b+1;
- vector <int> arr(12,0);
- for(int p=a;p<i;p++){
- for(int q=b;q<j;q++){
- int z=v[p][q];
- arr[z]++;
- if(arr[z]>1) goto next3;
- }
- }
- cnt++;
- next3:
- i=0;
- }
- }
- cout<< "Case " << caseno++ << ": " << cnt <<endl;
- }
- return 0;
- }
Add Comment
Please, Sign In to add comment