版本比较
P3956 [NOIP 2017 普及组] 棋盘代码
正文
增加 8 行删除 6 行
5 行未改动67891011121314157 行未改动232425262728293031323334353637383940414243444546474812 行未改动6162636465666768696 行未改动5 行未改动using namespace std;const int N = 110;int n, m, a[N][N], d[N][N][3];bool vis[N][N][3];int fx[4] = {0, 0, 1, -1};int fy[4] = {1, -1, 0, 0};struct node { int x, y; bool c; int w; };7 行未改动for (int i = 1; i <= n; ++i)for (int j = 1; j <= n; ++j) a[i][j] = -1;for (int i = 1; i <= m; ++i) {int x, y; cin >> x >> y;cin >> a[x][y];}memset(d, 0x3f, sizeof d);d[1][1][a[1][1]] = 0, D = 0;q1.push_front({1, 1, (bool)a[1][1], 0});while (!q1.empty()) {auto u = q1.front(); q1.pop_front();if (u.x == n && u.y == n) {cout << u.w << '\n';return 0;}if (u.w > D) {D = u.w;while (!q2.empty())q1.push_back(q2.front()), q2.pop_front();}if (vis[u.x][u.y][u.c]) continue;vis[u.x][u.y][u.c] = 1;12 行未改动}} else {if (a[u.x][u.y] == -1) continue;if (u.w + 2 < d[x][y][u.c])d[x][y][u.c] = u.w + 2, q2.push_back({x, y, u.c, u.w + 2});if (u.w + 3 < d[x][y][!u.c])d[x][y][!u.c] = u.w + 3, q2.push_back({x, y, !u.c, u.w + 3});}}6 行未改动5 行未改动67891011121314157 行未改动2324252627282930313233343536373839404142434445464748495012 行未改动6364656667686970716 行未改动5 行未改动using namespace std;const int N = 110;int n, m, a[N][N], d[N][N][2];bool vis[N][N][2];int fx[4] = {0, 0, 1, -1};int fy[4] = {1, -1, 0, 0};struct node { int x, y; bool c; int w; };7 行未改动for (int i = 1; i <= n; ++i)for (int j = 1; j <= n; ++j) a[i][j] = -1;for (int i = 1; i <= m; ++i) {int x, y;cin >> x >> y;cin >> a[x][y];}memset(d, 0x3f, sizeof d);d[1][1][a[1][1]] = 0, D = 0;q1.push_front({1, 1, (bool)a[1][1], 0});while (!q1.empty() || !q2.empty()) {if (q1.empty()) swap(q1, q2);auto u = q1.front(); q1.pop_front();if (u.x == n && u.y == n) {cout << u.w << '\n';return 0;}if (u.w > D) {D = u.w;while (!q2.empty() && q2.front().w == D + 1)q1.push_back(q2.front()), q2.pop_front();}if (vis[u.x][u.y][u.c]) continue;vis[u.x][u.y][u.c] = 1;12 行未改动}} else {if (a[u.x][u.y] == -1) continue;if (u.w + 2 < d[x][y][u.c])d[x][y][u.c] = u.w + 2, q2.push_front({x, y, u.c, u.w + 2});if (u.w + 3 < d[x][y][!u.c])d[x][y][!u.c] = u.w + 3, q2.push_back({x, y, !u.c, u.w + 3});}}6 行未改动