#36742: C++解


samlin961112@gmail.com (林哲甫)

學校 : 新北市私立南山高級中學
編號 : 220506
來源 : [219.70.213.92]
最後登入時間 :
2024-04-28 22:43:43
d596. 1. 猜九宮格裡的地雷 -- 98學年度全國資訊學科能力競賽 | From: [219.70.213.92] | 發表日期 : 2023-08-05 12:08

我是用函式,但其實不用也是可以寫出來的

#include <bits/stdc++.h>
using namespace std;
inline void change(int a, vector<vector<int>> &m, int n) {
  int x = (a - 1) / 3, y = (a - 1) % 3;
  vector<pair<int, int>> dir(4);
  dir = {{0, 1}, {1, 0}, {-1, 0}, {0, -1}};
  for (int i = 0; i < 4; i++) {
    int nx = x + dir[i].first, ny = y + dir[i].second;
    if (n == -1) {
      if (nx >= 0 && ny >= 0 && nx < 3 && ny < 3) {
        m[nx][ny] = -1;
      }
    } else {
      if (nx >= 0 && ny >= 0 && nx < 3 && ny < 3 && m[nx][ny] >= 0) {
        m[nx][ny] = 1;
      }
    }
  }
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n;
  cin >> n;
  while (n--) {
    int a, b, c;
    cin >> a >> b >> c;
    vector<vector<int>> m(3, vector<int>(3, 0));
    change(b, m,-1);
    change(c, m,-1);
    change(a, m,1);
    bool f = true;
    for (int i = 0; i < 3; i++) {
      for (int j = 0; j < 3; j++) {
        if (m[i][j] == 1) {
          cout << (i)*3 + j + 1 << ' ';
          f = false;
        }
      }
    }
    if (f) {
      cout << "Empty";
    }
    cout << '\n';
  }
}

 
ZeroJudge Forum