标签:search log return bug ber use through represent miss
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 7866 | Accepted: 2586 |
Description
Input
Output
Sample Input
9 10 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 7 2 2 7 south 0 0
Sample Output
12
这题改了好几天。。。。。
错误的点:
1.在DEBUG的时候我尝试恢复路径,此时发现有的结点的pre信息被后来修改,是因为应当在入队的时候标记,而不是出队的时候
2.对于位置的移动判断写错,首先边界不能触碰,而且一个黑色方格周围的点也不能。
3.在枚举一个点沿着一个方向行走的可行距离的时候,当遇到黑色方块或者边界的时候要break
代码:
#include<iostream> #include<cstdio> #include<cmath> #include<cstring> #include<sstream> #include<algorithm> #include<queue> #include<deque> #include<iomanip> #include<vector> #include<cmath> #include<map> #include<stack> #include<set> #include<fstream> #include<memory> #include<list> #include<string> using namespace std; typedef long long LL; typedef unsigned long long ULL; #define MAXN 109 #define N 33 #define MOD 1000000 #define INF 1000000009 const double eps = 1e-9; const double PI = acos(-1.0); int X[4] = { -1,0,1,0 }, Y[4] = { 0,1,0,-1 }; bool been[MAXN][MAXN][4];//四个方向 int n, m, rx, ry, g[MAXN][MAXN]; struct node { int x, y, dir, time; node() = default; node(int _x, int _y, int _dir, int _t) :x(_x), y(_y), dir(_dir), time(_t) {} }; node pre[MAXN][MAXN][4]; int d[MAXN][MAXN]; void print(node u) { vector<node> v; for (;;) { //cout << u.x <<‘ ‘<< u.y << ‘ ‘<< u.dir << endl; v.push_back(u); if (u.time == 0) break; u = pre[u.x][u.y][u.dir]; } int cnt = 0; for (int i = v.size() - 1; i >= 0; i--) { printf("%d %d %d %d\n", v[i].x, v[i].y, v[i].dir, v[i].time); } } bool CanGo(int x, int y) { if (x<1 || x >= n || y<1 || y >= m) return false; if (g[x][y] || g[x + 1][y] || g[x][y + 1] || g[x + 1][y + 1]) return false; return true; } int BFS(int x, int y, int d) { been[x][y][d] = true; queue<node> q; q.push(node(x, y, d, 0)); while (!q.empty()) { node t = q.front(); q.pop(); //cout << t.x << ‘ ‘ << t.y << ‘ ‘ << t.dir <<‘ ‘ << t.time << endl; if (t.x == rx&&t.y == ry) { //cout << t.prex << ‘ ‘ << t.prey << "::::" << t.dir << endl; //print(t); return t.time; } if (!been[t.x][t.y][(t.dir + 1) % 4]) { been[t.x][t.y][(t.dir + 1) % 4] = true; pre[t.x][t.y][(t.dir + 1) % 4] = t; q.push(node(t.x, t.y, (t.dir + 1) % 4, t.time + 1)); } if (!been[t.x][t.y][(t.dir - 1 + 4) % 4]) { been[t.x][t.y][(t.dir - 1 + 4) % 4] = true; pre[t.x][t.y][(t.dir - 1 + 4) % 4] = t; q.push(node(t.x, t.y, (t.dir - 1 + 4) % 4, t.time + 1)); } for (int i = 1; i <= 3; i++) { int tx = t.x + X[t.dir] * i, ty = t.y + Y[t.dir] * i; if (CanGo(tx, ty)) { if(!been[tx][ty][t.dir]) { been[tx][ty][t.dir] = true; pre[tx][ty][t.dir] = t; q.push(node(tx, ty, t.dir, t.time + 1)); } } else break; } } return -1; } int main() { while (scanf("%d%d", &n, &m), n + m) { memset(been, false, sizeof(been)); int tx, ty, d; char op[10]; for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) scanf("%d", &g[i][j]); scanf("%d%d%d%d%s", &tx, &ty, &rx, &ry, op); if (!CanGo(rx, ry)) { printf("-1\n"); continue; } if (op[0] == ‘n‘) d = 0; else if (op[0] == ‘e‘) d = 1; else if (op[0] == ‘s‘) d = 2; else d = 3; printf("%d\n", BFS(tx, ty, d)); } }
标签:search log return bug ber use through represent miss
原文地址:http://www.cnblogs.com/joeylee97/p/7015692.html