/* -*- coding: utf-8 -*- * * 13.cc: No.13 囲みたい! - yukicoder */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; /* constant */ const int MAX_H = 100; const int MAX_W = 100; const int dxs[] = {1, 0, -1, 0}, dys[] = {0, -1, 0, 1}; /* typedef */ typedef vector vi; typedef queue qi; typedef pair pii; struct Stat { int x, y, px, py; Stat() {} Stat(int _x, int _y, int _px, int _py): x(_x), y(_y), px(_px), py(_py) {} }; /* global variables */ int ms[MAX_H][MAX_W]; bool used[MAX_H][MAX_W]; /* subroutines */ /* main */ int main() { int w, h; cin >> w >> h; for (int y = 0; y < h; y++) for (int x = 0; x < w; x++) cin >> ms[y][x]; for (int y0 = 0; y0 < h; y0++) for (int x0 = 0; x0 < w; x0++) if (! used[y0][x0]) { int m = ms[y0][x0]; used[y0][x0] = true; queue q; q.push(Stat(x0, y0, -1, -1)); while (! q.empty()) { Stat u = q.front(); q.pop(); for (int di = 0; di < 4; di++) { int vx = u.x + dxs[di], vy = u.y + dys[di]; if ((vx != u.px || vy != u.py) && vx >= 0 && vx < w && vy >= 0 && vy < h && ms[vy][vx] == m) { if (used[vy][vx]) { puts("possible"); return 0; } used[vy][vx] = true; q.push(Stat(vx, vy, u.x, u.y)); } } } } puts("impossible"); return 0; }