QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#276466#7034. Honeycombzzuqy#TL 7ms27132kbC++204.0kb2023-12-05 21:32:182023-12-05 21:32:19

Judging History

你现在查看的是最新测评结果

  • [2023-12-05 21:32:19]
  • 评测
  • 测评结果:TL
  • 用时:7ms
  • 内存:27132kb
  • [2023-12-05 21:32:18]
  • 提交

answer

#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <stack>
#include <vector>
#include <algorithm>
#include <queue>
#include <bitset>
using namespace std;
#define DEBUG 0
const int N = 4e3 + 9;
typedef long long ll;
int read() {
  int x = 0, s = 1; char ch = getchar();
  for (; !isdigit(ch); ch = getchar()) if (ch == '-') s = -1;
  for (; isdigit(ch); ch = getchar()) x = x * 10 + (ch ^ 48);
  return x * s; 
}

int r, c;
// int d[N][N];

// x -- r ;  y -- c 
// start from up
const int dx[] = {-2, -1, 1, 2, 1, -1}, dy[] = {0, 3, 3, 0, -3, -3};
const int di[] = {-1, -1, 0, 1, 0, -1}, dj[] = {0, 1, 1, 0, -1, -1};
const int di2[] = {-1, 0, 1, 1, 1, 0}, dj2[] = {0, 1, 1, 0, -1, -1};
// 寄 (3, 5)
/*
1
3 4
  +---+       +---+
 /     \     /     \
+       +---+       +---+
 \           \     /     \
  +   +   S   +---+   T   +
 /     \     /           /
+       +---+       +   +
 \           \     /     \
  +---+       +---+       +
 /                       /
+       +---+       +   +
 \                 /     \
  +---+       +---+       +
       \     /     \     /
        +---+       +---+

*/

int _r, _c;

int get(int i, int j) {
  return (i - 1) * c + j;
}

vector<int> G[1000000+9];
void solve() {
  for (int i = 1; i <= r * c; ++i)
    G[i].clear();
    _c = 0;
  _r = 1;
  r = read(), c = read();
  map<pair<int, int>, char> mp;
  while (_r <= 4 * r + 3) {
    char ch = getchar();
    if (ch == EOF)
      break;
    _c++;
    mp[{_r, _c}] = ch;
    if (ch == '\n') {
      _r++;
      _c = 0;
    }
  }
  for (int i = 1; i <= 4 * r + 3; ++i) {
    for (int j = 1; ; ++j) {
      char ch = mp[{i, j}];
      // cout << ch;
      if (ch == '\n')
        break;
    }
  }
  int sx, sy, tx, ty;
  for (int i = 1; i <= r; i++) {
    for (int j = 1; j <= c; j += 2) {
      int x = 3 + (i - 1) * 4, y = 5 + (j - 1) * 6;
      if (mp[{x, y}] == 'S') {
        if (DEBUG) {
          cout << "A: ";
          cout << i << ' ' << j << '\n';
        }
        sx = i, sy = j;
      }
      if (mp[{x, y}] == 'T') {
        if (DEBUG) {
          cout << "B: ";
          cout << i << ' ' << j << '\n';
        }
        tx = i, ty = j;
        
      }
      for (int k = 0; k < 6; ++k) {
        int nx = x + dx[k], ny = y + dy[k];
        int ni = i + di[k], nj = j + dj[k];
        if (mp[{nx, ny}] == ' ') {
          G[get(i, j)].push_back(get(ni, nj));
          // G[get(ni, nj)].push_back(get(i, j));
          // cout << i << ' ' << j << "->" << ni << ' ' << nj << '\n';
        }
      }
    }
    for (int j = 2; j <= c; j += 2) {
      int x = 5 + (i - 1) * 4, y = 11 + (j - 2) * 6;
      if (mp[{x, y}] == 'S') {
        if (DEBUG) {
          cout << "C: ";
          cout << i << ' ' << j << '\n';
        }
        sx = i, sy = j;
      }
      if (mp[{x, y}] == 'T') {
        if (DEBUG) {
          cout << "D: ";
          cout << i << ' ' << j << '\n';
        }
        tx = i, ty = j;
      }
      for (int k = 0; k < 6; ++k) {
        int nx = x + dx[k], ny = y + dy[k];
        int ni = i + di2[k], nj = j + dj2[k];
        if (mp[{nx, ny}] == ' ') {
          G[get(i, j)].push_back(get(ni, nj));
          // G[get(ni, nj)].push_back(get(i, j));
          // cout << i << ' ' << j << "->" << ni << ' ' << nj << '\n';
        }
      }
    }
  }
  if (DEBUG) {
    cout << sx << ' ' << sy << '\n';
    cout << tx << ' ' << ty << '\n';
  }
  queue<int> que;
  map<int, int> dis;
  dis[get(sx, sy)] = 1;
  que.push(get(sx, sy));
  while (que.size()) {
    auto x= que.front();
    que.pop();
    for (auto y : G[x]) {
      if (dis.count(y))
        continue;
      dis[y] = dis[x] + 1;
      que.push(y);
      if (y == get(tx, ty))
        break;
    }
  }
  if (dis.count(get(tx, ty))) {
    printf("%d\n", dis[get(tx, ty)]);
  } else {
    puts("-1");
  }
}

int main() {
  int T = read();
  while (T--) {
    // cout << "Cialo~\n";
    solve();
  }
  return 0;
}

詳細信息

Test #1:

score: 100
Accepted
time: 7ms
memory: 27132kb

input:

1
3 4
  +---+       +---+
 /     \     /     \
+       +---+       +---+
 \           \     /     \
  +   +   S   +---+   T   +
 /     \     /           /
+       +---+       +   +
 \           \     /     \
  +---+       +---+       +
 /                       /
+       +---+       +   +
 \         ...

output:

7

result:

ok single line: '7'

Test #2:

score: -100
Time Limit Exceeded

input:

400
67 89
  +---+       +---+       +---+       +---+       +---+       +---+       +---+       +---+       +---+       +---+       +---+       +---+       +---+       +---+       +---+       +---+       +---+       +---+       +---+       +---+       +---+       +---+       +---+       +---+       ...

output:


result: