你的往上走和往左走呢
by ran_qwq @ 2023-05-27 07:21:22
@[rainygame](/user/804607) 优先化不应该开 3 维记录是否可以魔法吗
by Light_az @ 2023-05-27 07:43:00
@[Lovely_Ran](/user/743048) @[Light_az](/user/654958) 加了,但是依旧 $20$,是我的实现有问题吗?
```cpp
#include <bits/stdc++.h>
using namespace std;
#define MAXN 102
const int movex[] = {0, 1, -1, 0};
const int movey[] = {1, 0, 0, -1};
int n, m, x, y, c, ans;
int gr[MAXN][MAXN], f[MAXN][MAXN][2];
void dfs(int x, int y, int now, bool g=1){
if (f[x][y][g] <= now) return;
f[x][y][g] = now;
int nx, ny;
for (int i(0); i<4; ++i){
nx = x + movex[i];
ny = y + movey[i];
if (nx > n || !nx || ny > n || !ny) continue;
if (!gr[nx][ny]){
if (g){
gr[nx][ny] = 1;
dfs(nx, ny, now+2, 0);
gr[nx][ny] = 2;
dfs(nx, ny, now+2, 0);
gr[nx][ny] = 0;
}
}else if (gr[nx][ny] == gr[x][y]) dfs(nx, ny, now);
else dfs(nx, ny, now+1);
}
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n >> m;
while (m--){
cin >> x >> y >> c;
gr[x][y] = c+1;
}
memset(f, 0x3f, sizeof(f));
dfs(1, 1, 0);
ans = min(f[n][n][0], f[n][n][1]);
cout << (ans == 0x3f3f3f3f ? -1 : ans);
return 0;
}
```
by rainygame @ 2023-05-27 08:10:58
@[rainygame](/user/804607) 你可以看看第一篇题解如何实现方向移动 ![](//图.tk/6)
by Light_az @ 2023-05-27 15:15:51