結果
| 問題 |
No.3199 Key-Door Grid
|
| コンテスト | |
| ユーザー |
回転
|
| 提出日時 | 2025-07-11 21:45:40 |
| 言語 | C++23 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 2,629 bytes |
| コンパイル時間 | 3,358 ms |
| コンパイル使用メモリ | 290,296 KB |
| 実行使用メモリ | 144,768 KB |
| 最終ジャッジ日時 | 2025-07-11 21:46:07 |
| 合計ジャッジ時間 | 22,819 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 18 WA * 19 |
ソースコード
/*
from collections import deque
H,W,M = list(map(int,input().split()))
S = [input() for _ in range(H)]
for i in range(H):
for j in range(W):
if(S[i][j] == "S"):
sy,sx = i,j
elif(S[i][j] == "G"):
gy,gx = i,j
dy = [-1,1,0,0]
dx = [0,0,-1,1]
visited = set()
q = deque([(0,sy,sx,-1)])
while(q):
v,y,x,key = q.popleft()
if(y == gy and x == gx):
print(v)
exit()
if((y,x,key) in visited):continue
visited.add((y,x,key))
for i in range(4):
next_y = y + dy[i]
next_x = x + dx[i]
if(next_y < 0 or H <= next_y):continue
if(next_x < 0 or W <= next_x):continue
if(S[next_y][next_x] == "#"):continue
if(S[next_y][next_x].isnumeric()):
q.appendleft((v+1,next_y,next_x,int(S[next_y][next_x])-1))
elif(S[next_y][next_x] == "." or S[next_y][next_x] == "S" or S[next_y][next_x] == "G"):
q.append((v+1,next_y,next_x,key))
else:
if(key == ord(S[next_y][next_x]) - ord("a")):
q.append((v+1,next_y,next_x,key))
print(-1)
*/
#include <bits/stdc++.h>
using namespace std;
int main() {
int H, W, M;
cin >> H >> W >> M;
vector<string> S(H);
for (int i = 0; i < H; ++i) cin >> S[i];
int sy = -1, sx = -1, gy = -1, gx = -1;
for (int i = 0; i < H; ++i) {
for (int j = 0; j < W; ++j) {
if (S[i][j] == 'S') sy = i, sx = j;
else if (S[i][j] == 'G') gy = i, gx = j;
}
}
const int dy[4] = {-1, 1, 0, 0};
const int dx[4] = {0, 0, -1, 1};
set<tuple<int, int, int>> visited;
deque<tuple<int, int, int, int>> q;
q.emplace_back(0, sy, sx, -1);
while (!q.empty()) {
auto [v, y, x, key] = q.front();
q.pop_front();
if (y == gy && x == gx) {
cout << v << endl;
return 0;
}
if (visited.count({y, x, key})) continue;
visited.insert({y, x, key});
for (int i = 0; i < 4; ++i) {
int ny = y + dy[i];
int nx = x + dx[i];
if (ny < 0 || ny >= H || nx < 0 || nx >= W) continue;
char ch = S[ny][nx];
if (ch == '#') continue;
if (isdigit(ch)) {
q.emplace_front(v + 1, ny, nx, ch - '0' - 1);
} else if (ch == '.' || ch == 'S' || ch == 'G') {
q.emplace_back(v + 1, ny, nx, key);
} else {
if (key == ch - 'a') {
q.emplace_back(v + 1, ny, nx, key);
}
}
}
}
cout << -1 << endl;
return 0;
}
回転