Skip to content

Instantly share code, notes, and snippets.

@abdullah-alnahas
Last active May 13, 2016 14:59
Show Gist options
  • Save abdullah-alnahas/9ca6347623e7c834176e to your computer and use it in GitHub Desktop.
Save abdullah-alnahas/9ca6347623e7c834176e to your computer and use it in GitHub Desktop.
6326 - Contest Hall Preparation, Regionals 2012 >> Africa/Middle East - Arab Contest, ACM
/*
maybe there is better solutions using bitmask, but this one still working!
*/
#include <iostream>
using namespace std;
int main()
{
int t;//test cases
int n,m;//hall dimensions
int temp;
cin>>t;
cin.get();
for(int cases=0;cases<t;cases++){
cin>>n>>m;
cin.get();
int *hall;
hall = new int[n*m];
for(int u = 0; u<n; u++){
for(int k = 0; k<m; k++){
cin>>temp;
hall[u*m+k] = temp;
}
cin.get();
}
bool isAdjacent[100] = {false};
int adjacents = 0;
for(int i = 0; i<n; i++){
for(int j = 0; j<m; j++){
if(hall[i*m+j]!=-1 && !isAdjacent[hall[i*m+j]]){
if(i!=0&&j!=0&&j!=m-1&&i!=n-1){
if(hall[i*m+j]==hall[i*m+j+1]
|| hall[i*m+j]==hall[i*m+j-1]
|| hall[i*m+j]==hall[(i+1)*m+j]
|| hall[i*m+j]==hall[(i-1)*m+j]
|| hall[i*m+j]==hall[(i+1)*m+j+1]
|| hall[i*m+j]==hall[(i+1)*m+j-1]
|| hall[i*m+j]==hall[(i-1)*m+j+1]
|| hall[i*m+j]==hall[(i-1)*m+j-1]
){
adjacents++;
isAdjacent[hall[i*m+j]] = true;
}
}
else if(j==0&&i!=0&&i!=n-1)
{
if(hall[i*m+j]==hall[i*m+j+1]
|| hall[i*m+j]==hall[(i+1)*m+j]
|| hall[i*m+j]==hall[(i-1)*m+j]
|| hall[i*m+j]==hall[(i+1)*m+j+1]
|| hall[i*m+j]==hall[(i-1)*m+j+1]
){
adjacents++;
isAdjacent[hall[i*m+j]] = true;
}
}
else if(j==m-1&&i!=0&&i!=n-1)
{
if(hall[i*m+j]==hall[i*m+j-1]
|| hall[i*m+j]==hall[(i+1)*m+j]
|| hall[i*m+j]==hall[(i-1)*m+j]
|| hall[i*m+j]==hall[(i+1)*m+j-1]
|| hall[i*m+j]==hall[(i-1)*m+j-1]
){
adjacents++;
isAdjacent[hall[i*m+j]] = true;
}
}
else if(i==n-1&&j!=0&&j!=m-1)
{
if(hall[i*m+j]==hall[i*m+j+1]
|| hall[i*m+j]==hall[i*m+j-1]
|| hall[i*m+j]==hall[(i-1)*m+j]
|| hall[i*m+j]==hall[(i-1)*m+j+1]
|| hall[i*m+j]==hall[(i-1)*m+j-1]
){
adjacents++;
isAdjacent[hall[i*m+j]] = true;
}
}
else if(i==0&&j!=0&&j!=m-1)
{
if(hall[i*m+j]==hall[i*m+j+1]
|| hall[i*m+j]==hall[i*m+j-1]
|| hall[i*m+j]==hall[(i+1)*m+j]
|| hall[i*m+j]==hall[(i+1)*m+j+1]
|| hall[i*m+j]==hall[(i+1)*m+j-1]
){
adjacents++;
isAdjacent[hall[i*m+j]] = true;
}
}
else if(i==0&&j==0){
if(hall[i*m+j]==hall[i*m+j+1]
|| hall[i*m+j]==hall[(i+1)*m+j]
|| hall[i*m+j]==hall[(i+1)*m+j+1]
){
adjacents++;
isAdjacent[hall[i*m+j]] = true;
}
}
else if(i==n-1&&j==m-1){
if(hall[i*m+j]==hall[i*m+j-1]
|| hall[i*m+j]==hall[(i-1)*m+j]
|| hall[i*m+j]==hall[(i-1)*m+j-1]
){
adjacents++;
isAdjacent[hall[i*m+j]] = true;
}
}
else if(i==n-1&&j==0){
if(hall[i*m+j]==hall[i*m+j+1]
|| hall[i*m+j]==hall[(i-1)*m+j]
|| hall[i*m+j]==hall[(i-1)*m+j+1]
){
adjacents++;
isAdjacent[hall[i*m+j]] = true;
}
}
else if(i==0&&j==m-1){
if(hall[i*m+j]==hall[i*m+j-1]
|| hall[i*m+j]==hall[(i+1)*m+j]
|| hall[i*m+j]==hall[(i+1)*m+j-1]
){
adjacents++;
isAdjacent[hall[i*m+j]] = true;
}
}
}
}
}
cout<<adjacents<<endl;
}
return 0;
}
@omar-sh
Copy link

omar-sh commented May 13, 2016

I sent it and code forces didn't accept it, here is my code it's also not working (a2oj & code forces doesn't accept it) 👍
// Contest Hall Preparation.cpp : Defines the entry point for the console application.
//

include "stdafx.h"

include"iostream"

using namespace std;
int main()
{
int a[101][101];
int s[101];
int t, n, m;
bool b = 0;
int counter = 0;
int e = 0;
int d;
cin >> t;
while (t--) {
for (int i = 0; i < 101;i++)
s[i] = 201;
e = 0;
counter = 0;
b = 0;
cin >> n >> m;
for (int i = 0; i < n;i++)
for (int j = 0; j < m;j++)
cin >> a[i][j];
for (int i = 0;i < n;i++) {
for (int j = 0; j < m;j++) {
d = 0;
while (d < e) {
b = 0;
if (a[i][j] == s[d]) {
b = 1;
break;
}
d++;
}
if (i - 1 >= 0 && j - 1 >= 0&&i+1<=100&&j+1<=100)
if (a[i][j] == a[i - 1][j - 1] && a[i][j] != -1 && b!=1) {
s[e] = a[i][j];
counter++;
e++;
continue;
}
if (i - 1 >= 0)
if (a[i][j] == a[i - 1][j] && a[i][j] != -1 && b != 1) {

                        s[e] = a[i][j];
                        counter++;
                        e++;
                        continue;
                    }

                if (i + 1 < n && j - 1 >= 0)
                    if (a[i][j] == a[i + 1][j - 1] && a[i][j] !=-1  && b != 1){
                        s[e] = a[i][j];
                        counter++;
                        e++;
                        continue;
                    }
                if (i - 1 >= 0 && j + 1 < n)
                    if (a[i][j] == a[i - 1][j + 1] && a[i][j]!=-1 && b != 1) {
                        s[e] = a[i][j];
                        e++;
                        counter++;
                        continue;
                    }
                if (j + 1 < m)
                    if (a[i][j + 1] == a[i][j] && a[i][j]!=-1 && b != 1) {
                        s[e] = a[i][j];
                        counter++;
                        e++;
                        continue;
                    }
                if (i + 1 < n)
                    if (a[i][j] == a[i + 1][j] && a[i][j]!=-1 && b != 1) {
                        s[e] = a[i][j];
                        e++;
                        counter++;
                        continue;
                    }
                if (i + 1 < n && j + 1 < m) {
                    if (a[i][j] == a[i + 1][j + 1] && a[i][j]!=-1 && b != 1) {
                        s[e] = a[i][j];
                        counter++;
                        e++;
                        continue;
                    }
                }
            }

        }
        cout << counter << endl;
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment