結果

問題 No.13 囲みたい!
ユーザー imgry22imgry22
提出日時 2014-12-11 18:22:47
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 4 ms / 5,000 ms
コード長 1,688 bytes
コンパイル時間 1,586 ms
コンパイル使用メモリ 158,052 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-21 12:15:51
合計ジャッジ時間 2,168 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 1 ms
5,376 KB
testcase_02 AC 1 ms
5,376 KB
testcase_03 AC 4 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 4 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 2 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 #

#include<bits/stdc++.h>
using namespace std;

typedef long long int ll;
typedef unsigned long long int llu;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pair<int, int> > vii;

#define rrep(i, m, n) for(int i=m; i<n;  i++)
#define erep(i, n)    for(int i=1; i<=n; i++)
#define  rep(i, n)    for(int i=0; i<n;  i++)
#define rrev(i, m, n) for(int i=n-1; i>=m; i--)
#define erev(i, n)    for(int i=n; i>=1; i--)
#define  rev(i, n)    for(int i=n-1; i>=0; i--)
#define EACH(v)       (v).begin(), (v).end()
#define CNT(a, n, x)  (upper_bound(a, a+n, x)-lower_bound(a, a+n, x))
#define minup(m, x)   (m=min(m, x))
#define maxup(m, x)   (m=max(m, x))
#define mp            make_pair

#define INF 1000000000
#define MOD 1000000009
#define EPS 1E-8

#define check(i, j) (0<=(i)&&(i)<w && 0<=(j)&&(j)<h)

#define MAX_W 100
#define MAX_H 100
#define MAX_N (MAX_W*MAX_H)

int w, h;
int m[MAX_W][MAX_H];
int dx[] = {-1, 0, 1, 0};
int dy[] = {0, -1, 0, 1};
int visit[MAX_W][MAX_H];
bool flag = true;

int dfs(int x, int y, int px, int py, int n);

int main()
{
  cin >> h >> w;
  rep(i, w) rep(j, h) cin >> m[i][j];

  rep(i, w) rep(j, h) if(!visit[i][j] && dfs(i, j, i, j, m[i][j])) return 0;

  puts("impossible");

  return 0;
}

int dfs(int x, int y, int px, int py, int n)
{
  visit[x][y] = 1;
  rep(i, 4) if(check(x+dx[i], y+dy[i])) {
    int nx = x + dx[i];
    int ny = y + dy[i];
    if(m[nx][ny] != n) continue;
    if(mp(nx, ny) == mp(px, py)) continue;
    if(flag && visit[nx][ny]){
      puts("possible");
      flag = false;
    }
    if(!flag) return 1;
    dfs(nx, ny, x, y, n);
  }
  return 0;
}
0