这题因为各种琐事耽耽搁搁做了2天,也出了挺多错误,最后出了一个结论:像这种有对neighbor有通路的图形用一个4个位表示4个方向的int进行位运算比较靠谱。
/* ID: yingzho1 LANG: C++ TASK: maze1 */ #include <iostream> #include <fstream> #include <string> #include <map> #include <vector> #include <set> #include <algorithm> #include <stdio.h> #include <queue> #include <cstring> #include <cmath> #include <list> using namespace std; ifstream fin("maze1.in"); ofstream fout("maze1.out"); int W, H; ]; struct node { int x, y; int neigh; int dis; node(int a, int b, int c, int d) : x(a), y(b), neigh(c), dis(d) { } node() : x(), y(), neigh(), dis() { } } maps[][]; int dfs(node source) { ][] = {{-, }, {, }, {, -}, {, }}; queue<node> S, T; S.push(source); ; vector<vector<, vector<)); visit[source.x][source.y] = true; while (!S.empty()) { while (!S.empty()) { node tmp = S.front(); S.pop(); ; i < ; i++) { == ) { ]; ]; || xx > H || yy <= || yy > W) continue; if (!visit[xx][yy]) { T.push(maps[xx][yy]); maps[xx][yy].dis = min(maps[xx][yy].dis, account+); visit[xx][yy] = true; } } } } if (!T.empty()) account++; swap(S, T); } return account; } int main() { fin >> W >> H; string tmp; vector<node> exits; getline(fin, tmp); ; i < *H+; i++) { getline(fin, board[i]); *W+) board[i] += *W+-board[i].size(), ' '); } ; i <= H; i++) { ; j <= W; j++) { maps[i][j].x = i, maps[i][j].y = j; *i--][*j-] != '-') { maps[i][j].neigh |= ; //N 0001 ) exits.push_back(maps[i][j]); } *i-+][*j-] != '-') { maps[i][j].neigh |= ; // S 0010 if (i == H) exits.push_back(maps[i][j]); } *i-][*j--] != '|') { maps[i][j].neigh |= ; // W 0100 ) exits.push_back(maps[i][j]); } *i-][*j-+] != '|') { maps[i][j].neigh |= ; // E 1000 if (j == W) exits.push_back(maps[i][j]); } } } maps[exits[].x][exits[].y].dis = , maps[exits[].x][exits[].y].dis = ; dfs(maps[exits[].x][exits[].y]); dfs(maps[exits[].x][exits[].y]); ; ; i <= H; i++) { ; j <= W; j++) { maxdis = max(maxdis, maps[i][j].dis); } } fout << maxdis << endl; ; }