实验4,5

在这里插入图片描述

代码:

#include<bits/stdc++.h>
using namespace std;

const int maxn = 1e3 + 10;
int a[1010];
bool vis[maxn];
int path[maxn];
int k,m;
stack<char>st;
bool flag = false;
void dfs2(int dep,int s){
	if(s == m){
		stack<char>cur = st;
		stack<char>now;
		while(!cur.empty()){
			now.push(cur.top());cur.pop();
		}
		int sum = a[path[0]];
		for(int i = 1; i < k; i++){
			cout<<sum<<" ";
			cout<<now.top()<<" ";
			cout<<a[path[i]]<<" = ";
			if(now.top() == '*')sum *= a[path[i]];
			if(now.top() == '/')sum /= a[path[i]];
			if(now.top() == '+')sum += a[path[i]];
			if(now.top() == '-')sum -= a[path[i]];
			now.pop();
			cout<<sum<<"; ";
		}
		cout<<endl;
		flag = true;
		return ;
	}
	if(dep >= k)return; 
	for(int i = 0; i < 4; i++){
		if(i == 0)st.push('+'),dfs2(dep + 1, s + a[path[dep]]),st.pop();
		if(i == 1)st.push('-'),dfs2(dep + 1, s - a[path[dep]]),st.pop();
		if(i == 2)st.push('*'),dfs2(dep + 1, s * a[path[dep]]),st.pop();
		if(i == 3)st.push('/'),dfs2(dep + 1, s / a[path[dep]]),st.pop();
	}
}
void dfs(int dep){
	if(dep >= k){
		if(flag)return;
		//for(int i = 0; i < k; i++)
		//	cout<<path[i]<<"  ";
		dfs2(1,a[path[0]]);
		return ; 
	}
	for(int i = 1; i <= k; i++){
		if(!vis[i]){
			vis[i] = true;
			path[dep] = i;
			dfs(dep + 1);
			vis[i] = false; 
		}
	}
}
/*
5 125
7 2 2 12 3
*/
void solved(){
	cin>>k>>m;
	for(int i = 1; i <= k; i++)cin>>a[i];
	dfs(0);
}
int main(){
	solved();
	return 0;
}

在这里插入图片描述

代码:

#include<bits/stdc++.h>
using namespace std;

const int maxn = 1e3 + 10;
int G[maxn][maxn];
int dir[4][2] = {{0,1},{0,-1},{1,0},{-1,0}};
bool vis[maxn][maxn];
struct node{
	int x,y,step;
	node(int a,int b,int step):x(a),y(b),step(step){}
};
int n,m;
void bfs(int x1,int y1,int x2,int y2){
	queue<node>st;
	st.push(node(x1,y1,0));
	vis[x1][y1] = true;
	while(!st.empty()){
		node cur = st.front();st.pop();
		if(cur.x == x2 && cur.y == y2){
			cout<<cur.step<<endl;
			break;
		}
		for(int i = 0; i < 4; i++){
			int fx = cur.x + dir[i][0];
			int fy = cur.y + dir[i][1];
			if(fx < 1 || fy < 1 || fx > n || fy > m)continue;
			if(!vis[fx][fy] && G[fx][fy] == 0){
				vis[fx][fy] = true;
				st.push(node(fx,fy,cur.step + 1));
			}
		}
	}
}
void solved(){
	cin>>n>>m;
	for(int i = 1; i <= n; i++){
		for(int j = 1; j <= m; j++){
			cin>>G[i][j];
		}
	} 
	int x1,y1,x2,y2;cin>>x1>>y1>>x2>>y2;
	bfs(x1,y1,x2,y2);
}
/*
7 7
0 0 1 0 0 0 0
0 0 1 1 0 0 0
0 0 0 0 1 0 0
0 0 0 1 1 0 0 
1 0 0 0 1 0 0
1 1 1 0 0 0 0
1 1 1 0 0 0 0
3 2 4 6
*/
int main(){
	solved();
	return 0;
} 

A*算法:

#include<bits/stdc++.h>
using namespace std;

const int maxn = 1e3 + 10;
int n,m;
int G[maxn][maxn];
int dir[4][2] = {{0,1},{0,-1},{1,0},{-1,0}};
bool vis[maxn][maxn];
struct node{
	int x,y,f,step;
	node(int a,int b,int c,int d):x(a),y(b),f(c),step(d){}
	bool operator < (const node &rhs)const{
		return f > rhs.f;
	}
};
int dis(int x1,int y1,int x2,int y2){
	return abs(x1 - x2) + abs(y1 - y2);
}
void A_star(int x1,int y1,int x2,int y2){
	priority_queue<node>st;
	st.push(node(x1,x2,dis(x1,y1,x2,y2),0));
	while(!st.empty()){
		node cur = st.top();st.pop();
		if(cur.x == x2 && cur.y == y2){
			cout<<cur.step<<endl;break;
		}
		for(int i = 0; i < 4; i++){
			int fx = cur.x + dir[i][0];
			int fy = cur.y + dir[i][1];
			if(fx < 1 || fy < 1 || fx > n || fy > m)continue;
			if(!vis[fx][fy] && G[fx][fy] == 0){
				vis[fx][fy] = true;
				st.push(node(fx,fy,dis(fx,fy,x2,y2),cur.step + 1));
			}
		}
	}
} 
void solved(){
	cin>>n>>m;
	for(int i = 1; i <= n; i++){
		for(int j = 1; j <= m; j++){
			cin>>G[i][j];
		}
	} 
	int x1,y1,x2,y2;cin>>x1>>y1>>x2>>y2;
	A_star(x1,y1,x2,y2);
}
int main(){
	solved();
	return 0;
} 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值