赞!
JakeLin 2020-03-03 09:49:27 |
谢谢~加油!
纯洁的宇仔 2020-03-04 15:50:34 |
#include <iostream> #include <queue> #include <cstring> #include <string> using namespace std; char maze[505][505]; bool vis[505][505]; struct node{ int x, y, d; string s; node(int xx, int yy, int dd){ x = xx; y = yy; d = dd; } }; string d[4] = {"D","L","R","U"}; int dir[4][2] = {{1,0},{0,-1},{0,1},{-1,0}}; int n, m; int bfs(int x, int y){ queue<node> s; s.push(node(x, y, 0)); vis[x][y] = true; while(!s.empty()){ node now = s.front(); s.pop(); if(now.x == n && now.y == m){ cout << now.d << endl << now.s << endl; return true; } for(int i = 0; i < 4; i ++){ int tx = now.x + dir[i][0]; int ty = now.y + dir[i][1]; if(tx >= 1 && tx <= n && ty >= 1 && ty <= m && !vis[tx][ty] && maze[tx][ty] != '1'){ now.s += d[i]; s.push(node(tx, ty, now.d + 1)); vis[tx][ty] = true; } } } } int main(){ cin >> n >> m; for(int i = 1; i <= n; i ++){ for(int j = 1; j <= m; j ++){ cin >> maze[i][j]; } } bfs(1, 1); return 0; }
纯洁的宇仔 2020-03-04 15:51:09 |
请问我这段代码为啥拼接不了,能输出数字,但字符串拼接不成
JakeLin 2020-04-07 23:03:17 |
@pureayu sorry~这代码贴这乱成一片了