結果
| 問題 |
No.124 門松列(3)
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2017-09-12 16:49:39 |
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
| 結果 |
CE
(最新)
AC
(最初)
|
| 実行時間 | - |
| コード長 | 1,669 bytes |
| コンパイル時間 | 930 ms |
| コンパイル使用メモリ | 72,292 KB |
| 最終ジャッジ日時 | 2025-03-14 19:05:26 |
| 合計ジャッジ時間 | 1,747 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge5 |
(要ログイン)
コンパイルエラー時のメッセージ・ソースコードは、提出者また管理者しか表示できないようにしております。(リジャッジ後のコンパイルエラーは公開されます)
ただし、clay言語の場合は開発者のデバッグのため、公開されます。
ただし、clay言語の場合は開発者のデバッグのため、公開されます。
コンパイルメッセージ
main.cpp:8:25: error: variable ‘constexpr const std::array<int, 4> dr’ has initializer but incomplete type
8 | constexpr array<int, 4> dr = {-1, 0, 1, 0},
| ^~
main.cpp:9:25: error: variable ‘constexpr const std::array<int, 4> dc’ has initializer but incomplete type
9 | dc = { 0, 1, 0,-1};
| ^~
main.cpp: In function ‘int main()’:
main.cpp:35:38: error: ‘nc’ was not declared in this scope; did you mean ‘nr’?
35 | if(!(0 <= nr && nr < R && 0 <= nc && nc < C)) { continue; }
| ^~
| nr
main.cpp:36:33: error: ‘nc’ was not declared in this scope; did you mean ‘nr’?
36 | if(!is_kado(b2, b1, G[nr][nc])) { continue; }
| ^~
| nr
main.cpp:37:19: error: ‘nc’ was not declared in this scope; did you mean ‘nr’?
37 | if(cost[nr][nc][b1][G[nr][nc]] > cost[r][c][b2][b1] + 1) {
| ^~
| nr
main.cpp:19:18: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
19 | int C, R; scanf("%d%d", &C, &R);
| ~~~~~^~~~~~~~~~~~~~~~
main.cpp:23:12: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
23 | scanf("%d", &G[r][c]);
| ~~~~~^~~~~~~~~~~~~~~~
ソースコード
#include <iostream>
#include <algorithm>
#include <queue>
#include <tuple>
using namespace std;
// N E S W
constexpr array<int, 4> dr = {-1, 0, 1, 0},
dc = { 0, 1, 0,-1};
constexpr int dr_size = dr.size();
bool is_kado(int a, int b, int c) {
if(a == 0) { return true; }
return a!=c && ((a>b&&b<c) || (a<b&&b>c));
}
int main(void) {
constexpr int inf = 987654321;
int C, R; scanf("%d%d", &C, &R);
vector<vector<int>> G(R, vector<int>(C, 0));
for(int r=0; r<R; ++r) {
for(int c=0; c<C; ++c) {
scanf("%d", &G[r][c]);
}
}
vector<vector<vector<vector<int>>>> cost(R, vector<vector<vector<int>>>(C, vector<vector<int>>(10, vector<int>(10, inf)))); // cost[R][C][10][10]
cost[0][0][0][G[0][0]] = 0;
priority_queue<tuple<int, int, int, int, int>, vector<tuple<int, int, int, int, int>>, greater<tuple<int, int, int, int, int>>> pq;
pq.emplace(0, 0, 0, 0, G[0][0]);
while(!pq.empty()) {
int _, r, c, b2, b1; tie(_, r, c, b2, b1) = pq.top(); pq.pop();
for(int i=0; i<dr_size; ++i) {
int nr = r + dr[i],
nc = c + dc[i];
if(!(0 <= nr && nr < R && 0 <= nc && nc < C)) { continue; }
if(!is_kado(b2, b1, G[nr][nc])) { continue; }
if(cost[nr][nc][b1][G[nr][nc]] > cost[r][c][b2][b1] + 1) {
cost[nr][nc][b1][G[nr][nc]] = cost[r][c][b2][b1] + 1;
pq.emplace(cost[nr][nc][b1][G[nr][nc]], nr, nc, b1, G[nr][nc]);
}
}
}
int res = inf;
for(int b2=0; b2<10; ++b2) {
for(int b1=0; b1<10; ++b1) {
res = min(res, cost[R-1][C-1][b2][b1]);
}
}
if(res == inf) { res = -1; }
printf("%d\n", res);
return 0;
}