广搜求条

P1746 离开中山路

@[tireden](/user/1058090) 嗯嗯 我试试
by 违规用户名K&xs3Z^ @ 2024-08-20 09:12:57


@[违规用户名K&xs3Z^](/user/1271316) ```cpp struct Pos { int x,y; }; queue <Pos> q; ```
by _WHITE_NIGHT_ @ 2024-08-20 09:13:07


@[_WHITE_NIGHT_](/user/601236) 你这定义跟没定义一样
by tireden @ 2024-08-20 09:14:33


@[_WHITE_NIGHT_](/user/601236) 谢谢 已关
by 违规用户名K&xs3Z^ @ 2024-08-20 09:14:59


@[tireden](/user/1058090) 天才
by _WHITE_NIGHT_ @ 2024-08-20 09:15:27


@[违规用户名K&xs3Z^](/user/1271316) 逆天
by Deeplove_lzs @ 2024-08-20 09:16:21


@[_WHITE_NIGHT_](/user/601236) 最起码也得这样吧 ```cpp struct Pos { int x,y; int step; }; queue <Pos> q; ```
by tireden @ 2024-08-20 09:16:31


@[chenyunxi1](/user/1273309) 包的
by 违规用户名K&xs3Z^ @ 2024-08-20 09:17:01


@[违规用户名K&xs3Z^](/user/1271316) 分两个队列也不是不行,因为我就喜欢这样写() ```cpp #include <iostream> #include <queue> using namespace std; int n; int a[1010][1010]; int vis[1010][1010]; int sx, sy, ex, ey; int dx[] = {-1, 0, 1, 0}; int dy[] = {0, 1, 0, -1}; queue <int> qx, qy; int main() { cin >> n; for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { char ch; cin >> ch; a[i][j] = ch - '0'; } } cin >> sx >> sy >> ex >> ey; qx.push(sx); qy.push(sy); while (!qx.empty()) { int nx = qx.front(); int ny = qy.front(); if (nx == ex && ny == ey) { break; } for (int i = 0; i < 4; i++) { int cx = nx + dx[i]; int cy = ny + dy[i]; if (cx >= 1 && cx <= n && cy >= 1 && cy <= n && !a[cx][cy] && !vis[cx][cy]) { vis[cx][cy] = vis[nx][ny] + 1; qx.push(cx); qy.push(cy); } } qx.pop(); qy.pop(); } cout << vis[ex][ey]; return 0; } ```
by LikablePie79015 @ 2024-08-20 09:18:26


@[tireden](/user/1058090) 6 别人只是问如何用结构体定义数组而已,我这样写也是对的,只是写法不同。 ```cpp #include<bits/stdc++.h> using namespace std; const int N = 1005; struct Dot { int x,y; }; queue <Dot> mp; int n,tarx,tary,begx,begy; int vis[N][N],dis[N][N]; char chara; int dx[] = {0,1,0,-1,0}; int dy[] = {0,0,-1,0,1}; int check(int x,int y) { return x > 0 && x <= n && y > 0 && y <= n && vis[x][y] == 0; } int main() { cin >> n; for(int i = 1;i <= n;i++) { scanf("\n"); for(int j = 1;j <= n;j++) { scanf("%c",&chara); vis[i][j] = chara-'0'; } } scanf("%d%d%d%d",&begx,&begy,&tarx,&tary); mp.push((Dot){begx,begy}); vis[begx][begy] = 1; while(!mp.empty()) { int nx = mp.front().x; int ny = mp.front().y; if(nx == tarx && ny == tary) return printf("%d",dis[nx][ny]) && 0; for(int i = 1;i <= 4;i++) { if(check(nx+dx[i],ny+dy[i])) { vis[nx+dx[i]][ny+dy[i]] = 1; mp.push((Dot){nx + dx[i],ny + dy[i]}); dis[nx + dx[i]][ny + dy[i]] = dis[nx][ny]+1; } } mp.pop(); } } ```
by _WHITE_NIGHT_ @ 2024-08-20 09:22:09


上一页 | 下一页