Skip to content

Instantly share code, notes, and snippets.

@ctylim
Created September 3, 2015 19:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ctylim/03b68126fa6163009ea5 to your computer and use it in GitHub Desktop.
Save ctylim/03b68126fa6163009ea5 to your computer and use it in GitHub Desktop.
yukicoder No.13
#include <iostream>
#include <iomanip>
#include <vector>
#include <algorithm>
#include <numeric>
#include <functional>
#include <cmath>
#include <queue>
#include <stack>
#define repd(i,a,b) for (int i=(a);i<(b);i++)
#define rep(i,n) repd(i,0,n)
typedef long long ll;
using namespace std;
int inputValue(){
int a;
cin >> a;
return a;
};
void inputArray(int * p, int a){
rep(i, a){
cin >> p[i];
}
};
void inputVector(vector<int> * p, int a){
rep(i, a){
int input;
cin >> input;
p -> push_back(input);
}
}
template <typename T>
void output(T a, int precision) {
if(precision > 0){
cout << setprecision(precision) << a << "\n";
}
else{
cout << a << "\n";
}
}
int M[100][100];
bool V[100][100] = {false};
int vx[4] = {1, -1, 0, 0};
int vy[4] = {0, 0, 1, -1};
int inv[4] = {1, 0, 3, 2};
bool dfs(int x, int y, int dir, int w, int h){
bool ret = false;
V[x][y] = true;
rep(i, 4){
if (inv[i] != dir && x + vx[i] >= 0 && x + vx[i] < w && y + vy[i] >= 0 && y + vy[i] < h) {
if ( M[x][y] == M[x + vx[i]][y + vy[i]] ) {
if ( V[x + vx[i]][y + vy[i]] == true ) {
ret = true;
break;
}
else{
ret = dfs(x + vx[i], y + vy[i], i, w, h);
}
}
}
}
return ret;
}
int main(int argc, const char * argv[]) {
// source code
int W = inputValue();
int H = inputValue();
rep(i, H){
rep(j, W){
M[j][i] = inputValue();
}
}
bool ret = false;
rep(i, H){
rep(j, W){
if ( V[j][i] == false) {
ret = dfs(j, i, 5, W, H);
if (ret == true) {
break;
}
}
}
if (ret == true) {
break;
}
}
output((ret == true) ? "possible" : "impossible", 0);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment