标签:垂直 测试 step www second oid poj cin main
2 8 5 RRRUULLURRRRDDRRUUU 19 0 0 0 1 1 0 1 1 2 0 2 1 2 1 3 1 3 0 4 0 3 1 4 1 3 2 4 2 3 2 3 3 2 2 2 3 4 2 4 3 0 3 0 4 1 3 1 4 2 3 2 4 3 3 3 4 4 3 4 4 5 3 5 4 5 3 6 3 5 2 6 2 6 1 6 2 4 3 RRRUU 2 2 2 3 2 2 2 2 1样例输出
CORRECT INCORRECT
////////////////////////////////////////////////////////////////////////// // POJ1024 Tester Program // Memory: 368K Time: 16MS // Language: C++ Result: Accepted ////////////////////////////////////////////////////////////////////////// #include <iostream> #include <string> #include <queue> using namespace std; struct Grid { bool inpath; // 是否是路径方格 bool uwal; // 是否有上墙 bool rwal; // 是否有右墙 int scnt; // 到源点步数 int dcnt; // 到终点步数 }; int main(void) { bool ok; int w, h, cnt, steps; // 1 <= w, h <= 100 string path; Grid grid[100][100]; queue<pair<int, int> > q; int t, x, y, desx, desy, x2, y2, i; for (cin >> t; t > 0; --t) { //初始化数据 cin >> w >> h; for (y = 0; y < h; ++y) { for (x = 0; x < w; ++x) { grid[y][x].inpath = false; grid[y][x].uwal = false; grid[y][x].rwal = false; grid[y][x].scnt = -1; grid[y][x].dcnt = -1; } } cin >> path; x = 0, y = 0; grid[0][0].inpath = true; steps = path.size(); for (i = 0; i < steps; ++i) { switch (path[i]) { case ‘U‘: ++y; break; case ‘D‘: --y; break; case ‘L‘: --x; break; case ‘R‘: ++x; break; } grid[y][x].inpath = true; } desx = x, desy = y; cin >> cnt; for (i = 0; i < cnt; ++i) { cin >> x >> y >> x2 >> y2; if (x == x2) if (y + 1 == y2) grid[y][x].uwal = true; else grid[y2][x].uwal = true; else if (x + 1 == x2) grid[y][x].rwal = true; else grid[y][x2].rwal = true; } //求各点到源点的最小步数(BFS) q.push(make_pair(0, 0)); grid[0][0].scnt = 0; while (!q.empty()) { y = q.front().first, x = q.front().second; if (y < h - 1 && grid[y][x].uwal == false && grid[y + 1][x].scnt == -1) { grid[y + 1][x].scnt = grid[y][x].scnt + 1; q.push(make_pair(y + 1, x)); } if (0 < y && grid[y - 1][x].uwal == false && grid[y - 1][x].scnt == -1) { grid[y - 1][x].scnt = grid[y][x].scnt + 1; q.push(make_pair(y - 1, x)); } if (0 < x && grid[y][x - 1].rwal == false && grid[y][x - 1].scnt == -1) { grid[y][x - 1].scnt = grid[y][x].scnt + 1; q.push(make_pair(y, x - 1)); } if (x < w - 1 && grid[y][x].rwal == false && grid[y][x + 1].scnt == -1) { grid[y][x + 1].scnt = grid[y][x].scnt + 1; q.push(make_pair(y, x + 1)); } q.pop(); } //求各点到终点的最小步数(BFS) q.push(make_pair(desy, desx)); grid[desy][desx].dcnt = 0; while (!q.empty()) { y = q.front().first, x = q.front().second; if (y < h - 1 && grid[y][x].uwal == false && grid[y + 1][x].dcnt == -1) { grid[y + 1][x].dcnt = grid[y][x].dcnt + 1; q.push(make_pair(y + 1, x)); } if (0 < y && grid[y - 1][x].uwal == false && grid[y - 1][x].dcnt == -1) { grid[y - 1][x].dcnt = grid[y][x].dcnt + 1; q.push(make_pair(y - 1, x)); } if (0 < x && grid[y][x - 1].rwal == false && grid[y][x - 1].dcnt == -1) { grid[y][x - 1].dcnt = grid[y][x].dcnt + 1; q.push(make_pair(y, x - 1)); } if (x < w - 1 && grid[y][x].rwal == false && grid[y][x + 1].dcnt == -1) { grid[y][x + 1].dcnt = grid[y][x].dcnt + 1; q.push(make_pair(y, x + 1)); } q.pop(); } //判断路径是否唯一最短,以及墙是否多余 ok = true; for (y = 0; y < h && ok; ++y) { for (x = 0; x < w && ok; ++x) { if (grid[y][x].scnt == -1 || grid[y][x].dcnt == -1) ok = false; // 是否有封闭区域 if (y < h - 1 && grid[y][x].uwal && grid[y][x].scnt + grid[y + 1][x].dcnt + 1 > steps && grid[y][x].dcnt + grid[y + 1][x].scnt + 1 > steps) ok = false; // 是否上墙多余 if (x < w - 1 && grid[y][x].rwal && grid[y][x].scnt + grid[y][x + 1].dcnt + 1 > steps && grid[y][x].dcnt + grid[y][x + 1].scnt + 1 > steps) ok = false; // 是否右墙多余 if (!grid[y][x].inpath && grid[y][x].scnt + grid[y][x].dcnt <= steps) ok = false; // 是否存在更短路径或另一最短路径 } } if(ok) cout << "CORRECT" << endl; else cout << "INCORRECT" << endl; } return 0; }
来源:https://www.cnblogs.com/dengeven/p/3228687.html
标签:垂直 测试 step www second oid poj cin main
原文地址:https://www.cnblogs.com/sweet-ginger-candy/p/11518200.html