1.学会了原来字符串也有比较方法,也就是字符串987 > 98 等等,可以解决拼最大数问题
2.今天又复习了一下bfs,感觉还是很不熟练,可能是那个过程我些许有点不熟悉,准备再看看bfs然后自己总结一下。
bfs做题步骤
1,定义好结构体,数组,标记数组
2,定义好方向数组
3,定义好出发点和结束点
4,在用结构体定义好起始点给队列,起始点的标记数组设置为1。
5,while循环,条件是队列不能为空
6,里面先写结束条件,结束条件就是当前走的点走到了结束点
7,然后for循环方向数组,重新定义当前的x,y,step,让step + 1。让当前的x,y的标记数组变成1。
8,然后弹出来。
标准的bfs模板代码如下
# include <iostream>
# include <queue>
using namespace std;
int a[110][110];
bool vis[110][110];
struct Type{
int x;
int y;
int step;
};
int dx[] = {-1, 0, 1, 0};
int dy[] = {0, -1, 0, 1};
int main()
{
int n, m, x1, x2, y1, y2;
queue<Type> q1;
cin>>n>>m;
for(int i = 1; i <= n; i++)
{
for(int j = 1; j <= m; j++)
{
cin>>a[i][j];
}
}
cin>>x1>>y1>>x2>>y2;
Type start;
start.x = x1;
start.y = y1;
start.step = 0;
q1.push(start);
vis[x1][x2] = 1;
int flag = 0;
while(!q1.empty())
{
int x = q1.front().x, y = q1.front().y;
if(x == x2 && y == y2)
{
flag = 1;
cout<<q1.front().step<<endl;
break;
}
for(int k = 0; k < 4; k++)
{
int tx, ty;
tx = q1.front().x + dx[k];
ty = q1.front().y + dy[k];
if(a[tx][ty] == 1 && vis[tx][ty] == 0)
{
Type temp;
temp.x = tx;
temp.y = ty;
temp.step = q1.front().step + 1;
q1.push(temp);
vis[tx][ty] = 1;
}
}
q1.pop();
}
if(flag == 0)
{
cout<<-1<<endl;
}
return 0;
}
题目链接:7.走迷宫 - 蓝桥云课 (lanqiao.cn)
STL库,今天晚上看见一个非常值得注意的库函数stoi,substr
# include <iostream>
# include <cstring>
using namespace std;
int main()
{
string s = "520love";
int a = stoi(s); //这个只能把数字字符转化为数字,其他的不行
cout<<a<<endl;
size_t pos; //这个是无类型定义
int b = stoi(s, &pos, 16); //这个里面的pos也就是检测到转化不了的数字当前的位置意思也就是说pos = 3
cout<<pos<<endl;
cout<<b<<endl;
//substr也就是提取当前的位置到末尾,如果没有特殊标记的话。默认是提取到末尾
cout<<s.substr(pos)<<endl;
cout<<s.substr(pos, 2);
return 0;
}
优先队列prority_queue
greater从小到大排序的队列,less是从大到小排序的队列。
//------prority_queue->greater<int>----------
//#include <queue>
//#include <iostream>
//using namespace std;
//int main() {
// priority_queue<int, vector<int>, greater<int>> pq;
//
// pq.push(3);
// pq.push(1);
// pq.push(4);
// pq.push(1);
//
// while (!pq.empty()) {
// cout << pq.top() << " ";
// pq.pop();
// }
//
// return 0;
//}
//---------prority_queue->less------------
#include <queue>
#include <iostream>
using namespace std;
int main() {
priority_queue<int, vector<int>, less<int>> pq;
pq.push(3);
pq.push(1);
pq.push(4);
pq.push(1);
while (!pq.empty()) {
cout << pq.top() << " ";
pq.pop();
}
return 0;
}
5864

被折叠的 条评论
为什么被折叠?



