#include <bits/stdc++.h>
#ifdef _WIN32
# define getchar_unlocked getchar
#endif
using namespace std;
inline int read() {
int f = 0, x = 0;
char c = getchar();
while (!isdigit(c)) c ^= c == '-', c = getchar_unlocked();
while (isdigit(c)) x = (x << 3) + (x << 1) + (c ^ 48), c = getchar_unlocked();
return f ? -x : x;
}
int n, m;
int a[110][110];
int f[110][110][2][2];
const int cx[4] = {0, -1, 0, 1};
const int cy[4] = {-1, 0, 1, 0};
const int dx[4] = {1, 1, 0, 0};
const int dv[4] = {0, 0, 1, 1};
const int dy[4] = {0, 1, 0, 1};
const int ex[4] = {0, 0, 1, 3};
int main() {
cin >> m >> n;
memset(a, 0xff, sizeof(a));
memset(f, 0x7f, sizeof(f));
for (int i = 1; i <= n; i++)
a[read()][read()] = read();
f[1][1][a[1][1]][0] = 0;
for (int i = 1; i <= m; i++)
for (int j = 1; j <= m; j++)
for (int k = 1; k <= m; k++)
for (int l = 0; l < 4; l++)
if (a[j][k] == 1)
for (int x = 0; x < 4; x++)
f[j][k][1][0] = min(f[j][k][1][0], f[j + cx[l]][k + cy[l]][dx[x]][dy[x]] + ex[x]);
else if (a[j][k] == 0)
for (int x = 0; x < 4; x++)
f[j][k][0][0] = min(f[j][k][0][0], f[j + cx[l]][k + cy[l]][dv[x]][dy[x]] + ex[x]);
else
for (int x = 0; x < 2; x++)
f[j][k][x][1] = min(min(f[j][k][x][1], f[j][k][x][0] + 2), min(f[j + cx[l]][k + cy[l]][x][0] + 2, f[j + cx[l]][k + cy[l]][!x][0] + 3));
int mni = f[m][m][1][0];
for (int i = 1; i < 4; i++)
mni = min(mni, f[m][m][dx[i]][dy[i]]);
cout << (mni == 0x7f7f7f7f ? -1 : mni);
}
输入:
3 5
1 1 0
1 3 1
2 2 1
2 3 0
3 1 1
本地输出:
6
洛谷 IDE 输出:
6
提交上去:
[Testp#3] Wrong Answer.wrong answer On line 1 column 1, read -, expected 6.
提交输出(理论上):
-1
为甚么呢?