代码:
#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;
}