結果

問題 No.13 囲みたい!
ユーザー tnakao0123tnakao0123
提出日時 2016-03-02 12:21:04
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 4 ms / 5,000 ms
コード長 1,598 bytes
コンパイル時間 592 ms
コンパイル使用メモリ 89,592 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-21 12:23:16
合計ジャッジ時間 1,187 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 3 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 3 ms
5,376 KB
testcase_06 AC 3 ms
5,376 KB
testcase_07 AC 3 ms
5,376 KB
testcase_08 AC 4 ms
5,376 KB
testcase_09 AC 4 ms
5,376 KB
testcase_10 AC 2 ms
5,376 KB
testcase_11 AC 3 ms
5,376 KB
testcase_12 AC 1 ms
5,376 KB
testcase_13 AC 2 ms
5,376 KB
testcase_14 AC 2 ms
5,376 KB
testcase_15 AC 1 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

/* -*- coding: utf-8 -*-
 *
 * 13.cc: No.13 囲みたい! - yukicoder
 */

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<list>
#include<queue>
#include<deque>
#include<algorithm>
#include<numeric>
#include<utility>
#include<complex>
#include<functional>
 
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<int> vi;
typedef queue<int> qi;
typedef pair<int,int> 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<Stat> 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;
}
0