| ||||||||||
| Online Judge | Problem Set | Authors | Online Contests | User | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest | |||||||||
Re:我真是不知道到底哪里错了In Reply To:我真是不知道到底哪里错了 Posted by:houzhe at 2021-02-09 13:26:07 哦,逻辑上有错误。要探索往左和往下的路径,对于某些情况要绕过被彗星砸到的区域,然后往回走,得到最短的路径。
#include <iostream>
#include <vector>
#include <queue>
#include <stack>
#include <string>
using namespace std;
struct Point {
int x, y, t;
Point(int xx, int yy, int tt) : x(xx), y(yy), t(tt) {}
};
const int SZ = 400;
const int INF = 100000;
int map[SZ][SZ];
bool visited[SZ][SZ];
bool isQualified(int x, int y) {
if (x >= 0 && x < SZ && y >= 0 && y < SZ)
return true;
return false;
}
void setValue(int x, int y, int t) {
if (isQualified(x, y))
map[x][y] = min(map[x][y], t);
}
int main() {
for (int i = 0; i < SZ; i++)
for (int j = 0; j < SZ; j++) {
map[i][j] = INF;
visited[i][j] = false;
}
int m = 0;
cin >> m;
for (int i = 0; i < m; i++) {
int x, y, t;
cin >> x >> y >> t;
setValue(x, y, t);
setValue(x-1, y, t);
setValue(x+1, y, t);
setValue(x, y-1, t);
setValue(x, y+1, t);
}
int ans = -1;
if (map[0][0] == INF) {
ans = 0;
}
else if (map[0][0] == 0) {
ans = -1;
}
else {
queue<Point> que;
que.push(Point(0, 0, 0));
visited[0][0] = true;
while (!que.empty()) {
Point point = que.front();
que.pop();
int x = point.x;
int y = point.y;
int t = point.t;
if (map[x][y] == INF) {
ans = t;
break;
}
if (isQualified(x - 1, y) && !visited[x - 1][y] && map[x - 1][y] > (t + 1)) {
que.push(Point(x - 1, y, t + 1));
visited[x - 1][y] = true;
}
if (isQualified(x, y - 1) && !visited[x][y - 1] && map[x][y - 1] > (t + 1)) {
que.push(Point(x, y - 1, t + 1));
visited[x][y - 1] = true;
}
if (isQualified(x + 1, y) && !visited[x + 1][y] && map[x + 1][y] > (t + 1)) {
que.push(Point(x + 1, y, t + 1));
visited[x + 1][y] = true;
}
if (isQualified(x, y + 1) && !visited[x][y + 1] && map[x][y + 1] > (t + 1)) {
que.push(Point(x, y + 1, t + 1));
visited[x][y + 1] = true;
}
}
}
cout << ans << endl;
return 0;
}
> 用上面的答案是13的也测过了,还是WA。谁有原题的测试数据呀?
>
> #include <iostream>
> #include <vector>
> #include <queue>
> #include <stack>
> #include <string>
>
> using namespace std;
>
> struct Point {
> int x, y, t;
> Point(int xx, int yy, int tt) : x(xx), y(yy), t(tt) {}
> };
>
> const int SZ = 400;
> const int INF = 100000;
>
> int map[SZ][SZ];
> bool visited[SZ][SZ];
>
> bool isQualified(int x, int y) {
> if (x >= 0 && x < SZ && y >= 0 && y < SZ)
> return true;
> return false;
> }
>
> void setValue(int x, int y, int t) {
> if (isQualified(x, y))
> map[x][y] = min(map[x][y], t);
> }
>
> int main() {
> for (int i = 0; i < SZ; i++)
> for (int j = 0; j < SZ; j++) {
> map[i][j] = INF;
> visited[i][j] = false;
> }
>
> int m = 0;
> cin >> m;
>
> for (int i = 0; i < m; i++) {
> int x, y, t;
> cin >> x >> y >> t;
> setValue(x, y, t);
> setValue(x-1, y, t);
> setValue(x+1, y, t);
> setValue(x, y-1, t);
> setValue(x, y+1, t);
> }
>
> int ans = -1;
> if (map[0][0] == INF) {
> ans = 0;
> }
> else if (map[0][0] == 0) {
> ans = -1;
> }
> else {
> queue<Point> que;
> que.push(Point(0, 0, 0));
> visited[0][0] = true;
> while (!que.empty()) {
> Point point = que.front();
> que.pop();
> int x = point.x;
> int y = point.y;
> int t = point.t;
> if (map[x][y] == INF) {
> ans = t;
> break;
> }
> if (isQualified(x + 1, y) && !visited[x + 1][y] && map[x + 1][y] > (t + 1)) {
> que.push(Point(x + 1, y, t + 1));
> visited[x + 1][y] = true;
> }
> if (isQualified(x, y + 1) && !visited[x][y + 1] && map[x][y + 1] > (t + 1)) {
> que.push(Point(x, y + 1, t + 1));
> visited[x][y + 1] = true;
> }
> }
> }
> cout << ans << endl;
> return 0;
> }
Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator