AOJ1174 Identically Colored Panels Connection

Identically Colored Panels Connection

塗る色を決めて順番に塗っていく. それぞれ

  • : 始点から探索してと同じパネルが何色あるか
  • : 始点から探索しての色を にする.
  • : 回塗った状態. 最終的に回塗った回数を答えるが,最後に に塗り替えた場合なので,回塗った後に に塗って を呼ぶ.

ただし,電極は左上角のパネルに固定されていることとする.

の文を見逃していて,塗り始める場所を全探索していてSampleがずっと合わなかった…
全体的に辛い

Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#include <iostream>
#include <vector>
#include <string>
#include <cstring>
#include <algorithm>
#include <sstream>
#include <map>
#include <set>

#define REP(i,k,n) for(int i=k;i<n;i++)
#define rep(i,n) for(int i=0;i<n;i++)
#define INF 1<<30
#define pb push_back
#define mp make_pair

using namespace std;
typedef long long ll;
typedef pair<int,int> P;

int h, w, c;
int dx[4] = {1,0,-1,0};
int dy[4] = {0,1,0,-1};

bool can(int y,int x) {
  if(0 <= y && y < h && 0 <= x && x < w) return true;
  return false;
}

int cnt = 0, sy = 0, sx = 0;
bool used[10][10];
vector< vector<int> > v;

void rec(int y, int x) {
  used[y][x] = true;
  cnt++;

  rep(i, 4) {
      int ny = y + dy[i];
      int nx = x + dx[i];

      if(can(ny, nx) && !used[ny][nx] && v[ny][nx] == c) {
          rec(ny, nx);
      }
  }
}

int target, change;

void dfs(int y, int x) {
  v[y][x] = change;
  rep(i, 4) {
      int ny = y + dy[i];
      int nx = x + dx[i];

      if(can(ny, nx) && v[ny][nx] == target) {
          dfs(ny, nx);
      }
  }
}

int ans = 0;
void func(vector< vector<int> > t, int id) {
  // cout << " ------ func ---- :" << id << endl;
  // rep(i, h) {
  //     rep(j, w) cout << t[i][j] << " ";
  //     cout << endl;
  // }
  if(id == 4) {
      v = t;
      target = t[sy][sx];

      if(target == c) return;
      change = c;
      dfs(sy, sx);

      cnt = 0;
      memset(used, 0, sizeof(used));
      rec(sy, sx);
      ans = max(ans, cnt);
      return;
  }

  REP(i, 1, 7) {
      if(i == t[sy][sx]) continue;
      v = t;
      target = t[sy][sx]; change = i;
      dfs(sy, sx);
      func(v, id + 1);
  }
}

int main() {
  while(cin >> h >> w >> c) {
      if(h == 0 && w == 0 && c == 0) break;

      v.resize(h);
      rep(i, h) {
          v[i].resize(w);
          rep(j, w) cin >> v[i][j];
      }

      ans = 0;
      func(v, 0);

      cout << ans << endl;
  }
  return 0;
}
Apr 18th, 2016