下午写了三道广搜,大同小异,一道涉及位压缩,表示不懂所以WA了,唉,三个都没能秒过,细微的错误太多T_T。。。悲剧,还是算法实现和写代码能力太差,失败!!以后要再加强锻炼了,昨天和Y大牛聊天到很晚,对他的言辞深有感触,特此谢谢Y大牛的教诲^_^。附此题AC代码如下:
# include<iostream># include<queue>using namespace std;struct Node{ int x, y; int time; int rest; };int n, m;int Go[4][2] = {{0, 1}, {0, -1}, {-1, 0}, {1, 0}};int map[11][11];int visited[11][11];int sx, sy, ex, ey;int check(int x, int y){ if (x >=1 && x <= n && y >= 1 && y <= m && map[x][y])return 1; return 0;}int check1;void Bfs(){ queue<Node> Que; //memset(visited, 0, sizeof(visited)); visited[sx][sy] = 6; Node temp; temp.x = sx; temp.y = sy; temp.rest = 6; temp.time = 0; Que.push(temp); while (!Que.empty()){ Node pre = Que.front(); Que.pop(); if (pre.x == ex && pre.y == ey && pre.rest > 0){ //cout <<"rest:"<< pre.rest<<endl; check1 = pre.time; return ; } for (int i = 0; i < 4; i ++){ Node next; next.x = pre.x + Go[i][0]; next.y = pre.y + Go[i][1]; next.rest = pre.rest; next.time = pre.time; if (check(next.x, next.y) && map[next.x][next.y]!= 0){ next.rest --; next.time ++; if (next.rest <= 0)continue; if (map[next.x][next.y] == 4){ next.rest = 6; } if (next.rest > 0 && visited[next.x][next.y] < next.rest){ visited[next.x][next.y] = next.rest; Que.push(next); } } } } return ;}int main(){ int t; scanf("%d", &t); while (t--){ scanf("%d %d", &n, &m); for (int i = 1; i <= n; i ++){ for (int j = 1; j <= m; j ++){ scanf("%d",&map[i][j]); if (map[i][j] == 2){sx = i; sy = j;} if (map[i][j] == 3){ex = i; ey = j;} visited[i][j] = 0; } } check1 = -1; Bfs(); printf("%d/n", check1); } return 0; }