提出 #70786809
ソースコード 拡げる
#include <bits/stdc++.h>
//#include <atcoder/all>
using namespace std;
//using namespace atcoder;
//using mint = modint1000000007;
//const int mod = 1000000007;
//using mint = modint998244353;
//const int mod = 998244353;
const int INF = 1e9;
//const long long LINF = 1e18;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rep2(i,l,r)for(int i=(l);i<(r);++i)
#define rrep(i, n) for (int i = (n) - 1; i >= 0; --i)
#define rrep2(i,l,r)for(int i=(r) - 1;i>=(l);--i)
#define all(x) (x).begin(),(x).end()
#define allR(x) (x).rbegin(),(x).rend()
#define P pair<int,int>
template<typename A, typename B> inline bool chmax(A & a, const B & b) { if (a < b) { a = b; return true; } return false; }
template<typename A, typename B> inline bool chmin(A & a, const B & b) { if (a > b) { a = b; return true; } return false; }
// from:左上下右
const int dxA[4] = { 0,1,-1,0 };
const int dyA[4] = { 1,0,0,-1 };
const int dzA[4] = { 0,1,2,3 };//どこから?
const int dxB[4] = { 1,0,0,-1 };
const int dyB[4] = { 0,1,-1,0 };
const int dzB[4] = { 1,0,3,2 };//どこから?
const int dxC[4] = { -1,0,0,1 };
const int dyC[4] = { 0,-1,1,0 };
const int dzC[4] = { 2,3,0,1 };//どこから?
void solve() {
int h, w; cin >> h >> w;
vector<string>s(h);
rep(i, h)cin >> s[i];
vector dp(h, vector(w, vector<int>(4, INF)));
dp[0][0][0] = 0;
deque<array<int, 3>>que;
que.push_back({ 0,0,0 });
int ans = INF;
while (!que.empty()) {
auto[x, y, t] = que.front();
//cout << x << " " << y << " " << t << " " << dp[x][y][t] << endl;
que.pop_front();
{//A
int nx = x + dxA[t];
int ny = y + dyA[t];
int nt = dzA[t];
int add = s[x][y] == 'A' ? 0 : 1;
if (nx == h - 1 && ny == w)chmin(ans, dp[x][y][t] + add);
if (0 <= nx && nx < h && 0 <= ny && ny < w)
{
if (chmin(dp[nx][ny][nt], dp[x][y][t] + add)) {
if (add == 0) {
que.push_front({ nx,ny,nt });
}
else {
que.push_back({ nx,ny,nt });
}
}
}
}
{//B
int nx = x + dxB[t];
int ny = y + dyB[t];
int nt = dzB[t];
int add = s[x][y] == 'B' ? 0 : 1;
if (nx == h - 1 && ny == w)chmin(ans, dp[x][y][t] + add);
if (0 <= nx && nx < h && 0 <= ny && ny < w)
{
if (chmin(dp[nx][ny][nt], dp[x][y][t] + add)) {
if (add == 0) {
que.push_front({ nx,ny,nt });
}
else {
que.push_back({ nx,ny,nt });
}
}
}
}
{//C
int nx = x + dxC[t];
int ny = y + dyC[t];
int nt = dzC[t];
int add = s[x][y] == 'C' ? 0 : 1;
if (nx == h - 1 && ny == w)chmin(ans, dp[x][y][t] + add);
if (0 <= nx && nx < h && 0 <= ny && ny < w)
{
if (chmin(dp[nx][ny][nt], dp[x][y][t] + add)) {
if (add == 0) {
que.push_front({ nx,ny,nt });
}
else {
que.push_back({ nx,ny,nt });
}
}
}
}
}
//int ans = INF;
//rep(i, 4)chmin(ans, dp[h - 1][w - 1][i]);
cout << ans << endl;
//cout << endl;
}
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
int t; cin >> t;
while (t--)solve();
return 0;
}
提出情報
| 提出日時 |
|
| 問題 |
E - Reflection on Grid |
| ユーザ |
kwm_t |
| 言語 |
C++23 (GCC 15.2.0) |
| 得点 |
500 |
| コード長 |
3121 Byte |
| 結果 |
AC |
| 実行時間 |
62 ms |
| メモリ |
26816 KiB |
ジャッジ結果
| セット名 |
Sample |
All |
| 得点 / 配点 |
0 / 0 |
500 / 500 |
| 結果 |
|
|
| セット名 |
テストケース |
| Sample |
00_sample_00.txt |
| All |
00_sample_00.txt, 01_test_00.txt, 01_test_01.txt, 01_test_02.txt, 01_test_03.txt, 01_test_04.txt, 01_test_05.txt, 01_test_06.txt, 01_test_07.txt, 01_test_08.txt, 01_test_09.txt, 01_test_10.txt, 01_test_11.txt, 01_test_12.txt, 01_test_13.txt, 01_test_14.txt, 01_test_15.txt, 01_test_16.txt, 01_test_17.txt, 01_test_18.txt, 01_test_19.txt, 01_test_20.txt, 01_test_21.txt, 01_test_22.txt, 01_test_23.txt, 01_test_24.txt, 01_test_25.txt, 01_test_26.txt, 01_test_27.txt, 01_test_28.txt, 01_test_29.txt, 01_test_30.txt, 01_test_31.txt, 01_test_32.txt, 01_test_33.txt, 01_test_34.txt |
| ケース名 |
結果 |
実行時間 |
メモリ |
| 00_sample_00.txt |
AC |
1 ms |
3600 KiB |
| 01_test_00.txt |
AC |
62 ms |
3540 KiB |
| 01_test_01.txt |
AC |
38 ms |
3508 KiB |
| 01_test_02.txt |
AC |
32 ms |
3728 KiB |
| 01_test_03.txt |
AC |
31 ms |
3676 KiB |
| 01_test_04.txt |
AC |
29 ms |
3608 KiB |
| 01_test_05.txt |
AC |
32 ms |
3892 KiB |
| 01_test_06.txt |
AC |
31 ms |
4232 KiB |
| 01_test_07.txt |
AC |
31 ms |
4324 KiB |
| 01_test_08.txt |
AC |
21 ms |
25260 KiB |
| 01_test_09.txt |
AC |
24 ms |
26736 KiB |
| 01_test_10.txt |
AC |
29 ms |
19836 KiB |
| 01_test_11.txt |
AC |
31 ms |
21232 KiB |
| 01_test_12.txt |
AC |
34 ms |
17964 KiB |
| 01_test_13.txt |
AC |
33 ms |
18308 KiB |
| 01_test_14.txt |
AC |
34 ms |
15484 KiB |
| 01_test_15.txt |
AC |
33 ms |
15600 KiB |
| 01_test_16.txt |
AC |
37 ms |
15284 KiB |
| 01_test_17.txt |
AC |
36 ms |
15236 KiB |
| 01_test_18.txt |
AC |
40 ms |
18412 KiB |
| 01_test_19.txt |
AC |
39 ms |
17512 KiB |
| 01_test_20.txt |
AC |
19 ms |
25260 KiB |
| 01_test_21.txt |
AC |
19 ms |
25228 KiB |
| 01_test_22.txt |
AC |
24 ms |
26764 KiB |
| 01_test_23.txt |
AC |
24 ms |
26816 KiB |
| 01_test_24.txt |
AC |
21 ms |
19808 KiB |
| 01_test_25.txt |
AC |
23 ms |
21252 KiB |
| 01_test_26.txt |
AC |
21 ms |
15424 KiB |
| 01_test_27.txt |
AC |
22 ms |
15680 KiB |
| 01_test_28.txt |
AC |
34 ms |
19416 KiB |
| 01_test_29.txt |
AC |
25 ms |
14808 KiB |
| 01_test_30.txt |
AC |
27 ms |
14788 KiB |
| 01_test_31.txt |
AC |
22 ms |
19824 KiB |
| 01_test_32.txt |
AC |
22 ms |
19840 KiB |
| 01_test_33.txt |
AC |
32 ms |
19600 KiB |
| 01_test_34.txt |
AC |
30 ms |
19564 KiB |