共计 861 个字符,预计需要花费 3 分钟才能阅读完成。
逆置程序表
void Reverse(Seqlist,int left,int right){ | |
int k = left , j = right;DataType temp; | |
while(k<j){temp = L.data[k],L.Data[k]=L.data[j] | |
,L.Data[k]=tem; | |
k++,j--; | |
} | |
}// 工夫复杂度 O(n); |
走迷宫问题:
#include <cstring> | |
#include <iostream> | |
#include <algorithm> | |
#include <queue> | |
using namespace std; | |
typedef pair<int, int> PII; | |
const int N = 110; | |
int n, m; | |
int g[N][N], d[N][N]; | |
int bfs() | |
{ | |
queue<PII> q; | |
memset(d, -1, sizeof d); | |
d[0][0] = 0; | |
q.push({0, 0}); | |
int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1};// 联合起来别离对应上下左右 | |
while (q.size()) | |
{auto t = q.front();// 宽搜罕用, 队列后取数 | |
q.pop(); | |
// 当你看这段代码没有达到很透彻的时候倡议不要跳过,认真思考 | |
for (int i = 0; i < 4; i ++) | |
{int x = t.first + dx[i], y = t.second + dy[i];// 别离对应指标点像上下左右做试探,而后对二维上下左右做取值 | |
if (x >= 0 && x < n && y >= 0 && y < m && g[x][y] == 0 && d[x][y] == -1) | |
{// 如果 x,y 在符合条件范畴之内 且没有被摸索过 并且路是通的,d[x][y] = d[t.first][t.second] + 1; | |
q.push({x, y}); | |
} | |
} | |
} | |
// 这里为什么是 [n-1][m-1] | |
return d[n - 1][m - 1]; | |
} | |
int main() | |
{ | |
cin >> n >> m; | |
for (int i = 0; i < n; i ++) | |
for (int j = 0; j < m; j ++) | |
cin >> g[i][j]; | |
cout << bfs() << endl; | |
return 0; | |
} |
正文完