c++考场代码模板

c++ 代码模板

随机数

mt19937

1
2
mt19937 engine(chrono::_V2::steady_clock::now().time_since_epoch().count());
int rand(int l,int r){return uniform_int_distribution<int>(l,r)(engine);}

对拍

1
2
3
4
5
6
7
8
9
10
@echo off
:go
make_data.exe >in.txt
need_test.exe < in.txt > out1.txt
ok.exe < in.txt > out2.txt
fc out1.txt out2.txt
if not errorlevel 1 goto go
echo find
goto go

缺省源

1
2
3
4
5
6
7
8
9
10
11
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;

mt19937 engine(chrono::_V2::steady_clock::now().time_since_epoch().count());
int rand(int l,int r){return uniform_int_distribution<int>(l,r)(engine);}

signed main(){

return 0;
}

文件读写

1
2
freopen(".in","r",stdin);
freopen(".out","w",stdout);

队列

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
template<typename T>
class queue_usingstack{
private:
stack<T> stack_in;
stack<T> stack_out;
void make_stack_in_to_stack_out(){
if(stack_out.empty()){
while(!stack_in.empty()){
stack_out.push(stack_in.top());
stack_in.pop();
}
}
}
public:
void push(T n){
stack_in.push(n);
}
T top(){//top=front 功能相同
make_stack_in_to_stack_out();
return stack_out.top();
}
T front(){
make_stack_in_to_stack_out();
return stack_out.top();
}
void pop(){
if(stack_out.empty()){
make_stack_in_to_stack_out();
}
stack_out.pop();
}
bool empty(){
return stack_in.empty()&&stack_out.empty();
}
int size(){
return stack_in.size()+stack_out.size();
}
void clean(){
stack_in.clean();
stack_out.clean();
}
};

广搜

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include<bits/stdc++.h>
using namespace std;
const int fangxiangshu=8;//把这个数改成方向数
bool vis[505][505];
int n;
int sx,sy;
int ex,ey;
int dx[fangxiangshu]={-1,-2,-2,-1,1,2,2,1};//方向数组1(x轴)
int dy[fangxiangshu]={-2,-1,1,2,2,1,-1,-2};//方向数组2(y轴)
struct node{//节点记录结构体
int x,y,step;
};

queue<node> q;//创建一个node类型的队列

int bfs(){
q.push(node{sx,sy,0});//1.队首元素入队
while(!q.empty()){//2.遍历队列,直到队列为空
node f=q.front();//2.1 找到队首元素
q.pop();//2.2存下队首元素后,队首元素出队
for(int i=0;i<fangxiangshu;i++){//2.3遍历队首元素能到达的所有节点
int nx=f.x+dx[i];
int ny=f.y+dy[i];
if(nx==ex&&ny==ey) return f.step+1;//2.4如果当前节点是答案,return结果
if(nx>=0&&nx<n&&ny>=0&&ny<n&&!vis[nx][ny]){//2.5判断合法合法性
//2.6入队
vis[nx][ny]=1;//2.6.1 标记
q.push(node{nx,ny,f.step+1});//2.6.2 入队
}
}
}
return -1;//3 无答案
}


int main(){
cin>>n;
cin>>sx>>sy>>ex>>ey;
cout<<bfs();
return 0;
}

c++考场代码模板
https://joshua0729.github.io/2025/08/06/c-考场代码模板/
作者
Joshua0729
发布于
2025-08-06 09:08:00.1616
更新于
2025-08-06 09:08:80.2525
许可协议