AOJ1517 Challenge From Grandfather

Challenge from Grandfather | Aizu Online Judge

Introduction to Programming Introduction to Algorithms and Data Structures Library of Data Structures Library of Graph Algorithms Library of Computational Geometry Library of Dynamic Programming Library of Number Theory

部分行列の回転,反転,左シフト,右シフト,島反転を実装する.
回転は90度回転を回行う.
反転,左シフト,右シフトはその通りにあって,島反転は幅優先で島を発見し反転していった.

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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#include <iostream>
#include <vector>
#include <string>
#include <cstring>
#include <algorithm>
#include <sstream>
#include <map>
#include <set>
#include <queue>

#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 n, m;
int t[15][15];
vector<int> v[15];

void vrotate(int r, int c, int size, int angle) {
  if(angle == 0 || angle == 360) return;

  memset(t, 0, sizeof(t));

  rep(i, angle/90) {
      int col = c;
      for(int i = r + size - 1; i >= r; i--, col++) {
          REP(j, c, c + size) {
              t[r+j-c][col] = v[i][j];
          }
      }

      REP(i, r, r + size) {
          REP(j, c, c + size) {
              v[i][j] = t[i][j];
          }
      }
  }
}

void reversal(int r, int c, int size) {
  REP(i, r, min(n, r + size)) {
      REP(j, c, min(n, c + size)) {
          v[i][j] = !v[i][j];
      }
  }
}

void leftshift(int r) {
  int t = v[r][0];
  rep(i, n) {
      v[r][i] = v[r][i+1];
  }
  v[r][n-1] = t;
}

void rightshift(int r) {
  int t = v[r][n-1];
  for(int i = n-1; i >= 1; i--) {
      v[r][i] = v[r][i-1];
  }
  v[r][0] = t;
}

int dx[4] = {1,0,-1,0};
int dy[4] = {0,1,0,-1};
bool used[15][15];

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

void islandreversal(int r, int c) {
  queue<P> que;
  que.push(mp(r, c));

  memset(used, 0, sizeof(used));
  used[r][c] = true;
  int t = v[r][c];
  while(que.size()) {
      P p = que.front(); que.pop();
      int y = p.first;
      int x = p.second;

      if(v[y][x] == t) {
          v[y][x] = !v[y][x];
      }

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

          if(can(ny, nx) && !used[ny][nx] && v[ny][nx] == t) {
              que.push(mp(ny, nx));
              used[ny][nx] = true;
          }
      }
  }
}

int main() {
  cin >> n >> m;

  memset(v, 0, sizeof(v));
  rep(i, n) {
      v[i].resize(n);
      rep(j, n) cin >> v[i][j];
  }

  rep(i, m) {
      int o;
      cin >> o;

      int r, c, size, angle;

      if(o == 0) { // rotate
          cin >> r >> c >> size >> angle;
          r--; c--;

          vrotate(r, c, size, angle);
      } else if(o == 1) { // reversal
          cin >> r >> c >> size;
          r--; c--;

          reversal(r, c, size);
      } else if(o == 2) { // left shift
          cin >> r;
          r--;

          leftshift(r);
      } else if(o == 3) { // right shift
          cin >> r;
          r--;
          rightshift(r);
      } else if(o == 4) { // Island reversal
          cin >> r >> c;
          r--; c--;
          islandreversal(r, c);
      }
  }

  rep(i, n) {
      rep(j, n) {
          cout << v[i][j];

          if(j == n-1) cout << endl;
          else cout << " ";
      }
  }

  return 0;
}
May 11th, 2016