BFS60pts, 求调!!!

P1747 好奇怪的游戏

输入:3 5 19 19 输出:2 2
by mediocre_ @ 2023-04-11 19:24:09


你的队列q在第二次循环的时候没有清空 ```cpp #include<bits/stdc++.h> using namespace std; const int N = 20 + 9; const int fx[] = {1, 1, 2, 2, 2, 2, -1, -1, -2, -2, -2, -2}; const int fy[] = {-2, 2, -2, -1, 1, 2, -2, 2, -1, 1, -2, 2}; int x, y; int vis[N][N]; bool use[N][N]; struct Node { int x, y; }; queue <Node>q; bool inmap(int nx, int ny) { return nx >= 1 && nx <= 20 && ny >= 1 && ny <= 20; } int main() { for (int i = 1; i <= 2; ++i) { memset(vis, 0, sizeof vis); memset(use, 0, sizeof use); while (!q.empty()) q.pop();//就是这!!! scanf("%d%d", &x, &y); q.push(Node{x, y}); use[x][y] = true; while (!q.empty()) { Node u = q.front(); q.pop(); if (u.x == 1 && u.y == 1) { printf("%d\n", vis[1][1]); break; } for (int d = 0; d < 12; d++) { int nx = u.x + fx[d]; int ny = u.y + fy[d]; if (inmap(nx, ny) && !use[nx][ny]) { vis[nx][ny] = vis[u.x][u.y] + 1; use[nx][ny] = true; q.push(Node{nx, ny}); } } } /*for (int j = 1; j <= 20; ++j) { for (int k = 1; k <= 20; ++k) printf("%d ", vis[j][k]); printf("\n"); }*/ } return 0; } ```
by Creeper250 @ 2023-05-18 22:56:12


@[Mr_Huang12](/user/565707) 我把要加的补在了main函数第4行
by Creeper250 @ 2023-05-19 19:35:00


手打队列存图: ```cpp #include<bits/stdc++.h> using namespace std; int flag[25][25],startx,starty; struct queue{ int x,y,s=0; }que[1005]; int dx[12]={-1,-2,-2,-2,-2,-1,1,2,2,2,2,1}; int dy[12]={-2,-2,-1,1,2,2,2,2,1,-1,-2,-2}; bool check(int p,int q) { if(p<1||p>20||q<1||q>20) return false; return true; } void bfs() { int head=0,tail=1; while(head<tail) { head++; for(int i=0;i<12;i++) { int nx=dx[i]+que[head].x; int ny=dy[i]+que[head].y; if(check(nx,ny)==true&&flag[nx][ny]==0) { tail++; flag[nx][ny]=1; que[tail].x=nx; que[tail].y=ny; que[tail].s=que[head].s+1; if(nx==1&&ny==1) { cout<<que[tail].s<<endl; return; } } } } } int main() { for(int i=1;i<=2;i++) { memset(flag,0,sizeof(flag)); memset(que,0,sizeof(que)); cin>>startx>>starty; que[1].x=startx; que[1].y=starty; flag[startx][starty]=1; bfs(); } return 0; } ```
by poor_OIer @ 2023-05-27 22:16:00


|