65,174
社区成员




#include <iostream>
#include <list>
#include <vector>
using namespace std;
char b[21][21];
bool is_visited[21][21];
int a[21];
struct point{
int x;
int y;
int step;
int point_serial;
};
list<point> work_stack;
vector<point> trash_vector;
vector<point>::iterator trash_vector_iterator;
int trash_count=0;
int for_added,for_compare;
int least_step_total=99999;
int shortest_step;
point start_point_for_sort,end_point_for_sort;
int sx,sy,ex,ey;
void initial_number_group()
{
for(int j=0;j<=20;j++)
{
a[j]=j;
}
}
void swap(int*a,int*b)
{
int tmp=*a;
*a=*b;
*b=tmp;
}
//获得地图,并将地上的垃圾存入垃圾队列
void get_b()
{
string a=".......x.......";
int lenth=15;
int i_in_get_b;
int serial=1;
for(i_in_get_b=1;i_in_get_b<=lenth;i_in_get_b++)
{
b[1][i_in_get_b]=a[i_in_get_b-1];
}
a="...o...x....*..";
for(i_in_get_b=1;i_in_get_b<=lenth;i_in_get_b++)
{
b[2][i_in_get_b]=a[i_in_get_b-1];
}
a=".......x.......";
for(i_in_get_b=1;i_in_get_b<=lenth;i_in_get_b++)
{
b[3][i_in_get_b]=a[i_in_get_b-1];
}
a=".......x.......";
for(i_in_get_b=1;i_in_get_b<=lenth;i_in_get_b++)
{
b[4][i_in_get_b]=a[i_in_get_b-1];
}
a=".......x.......";
for(i_in_get_b=1;i_in_get_b<=lenth;i_in_get_b++)
{
b[5][i_in_get_b]=a[i_in_get_b-1];
}
a="...............";
for(i_in_get_b=1;i_in_get_b<=lenth;i_in_get_b++)
{
b[6][i_in_get_b]=a[i_in_get_b-1];
}
a="xxxxx.....xxxxx";
for(i_in_get_b=1;i_in_get_b<=lenth;i_in_get_b++)
{
b[7][i_in_get_b]=a[i_in_get_b-1];
}
a="...............";
for(i_in_get_b=1;i_in_get_b<=lenth;i_in_get_b++)
{
b[8][i_in_get_b]=a[i_in_get_b-1];
}
a=".......x.......";
for(i_in_get_b=1;i_in_get_b<=lenth;i_in_get_b++)
{
b[9][i_in_get_b]=a[i_in_get_b-1];
}
a=".......x.......";
for(i_in_get_b=1;i_in_get_b<=lenth;i_in_get_b++)
{
b[10][i_in_get_b]=a[i_in_get_b-1];
}
a=".......x.......";
for(i_in_get_b=1;i_in_get_b<=lenth;i_in_get_b++)
{
b[11][i_in_get_b]=a[i_in_get_b-1];
}
a="..*....x....*..";
for(i_in_get_b=1;i_in_get_b<=lenth;i_in_get_b++)
{
b[12][i_in_get_b]=a[i_in_get_b-1];
}
a=".......x.......";
for(i_in_get_b=1;i_in_get_b<=lenth;i_in_get_b++)
{
b[13][i_in_get_b]=a[i_in_get_b-1];
}
point get_b_tmp_point;
int k_in_get_b,j_in_get_b;
for(k_in_get_b=1;k_in_get_b<=21;k_in_get_b++)
{
for(j_in_get_b=1;j_in_get_b<=21;j_in_get_b++)
{
if( b[k_in_get_b][j_in_get_b]=='*' )
{
get_b_tmp_point.point_serial=serial;
serial++;
get_b_tmp_point.x=k_in_get_b;
get_b_tmp_point.y=j_in_get_b;
trash_vector.push_back(get_b_tmp_point);
}
}
}
}
//获得地图上两点之间的最短路径
int get_shortest_path(int start_x,int start_y,int end_x,int end_y)
{
cout<<"函数开始处end_x= "<<end_x<<endl;
point start_point,end_point,work_point,add_point;
start_point.x=start_x;
start_point.y=start_y;
start_point.step=0;
end_point.x=end_x;
end_point.y=end_y;
shortest_step=666666;
//初始化is_visited数组
int i_in_get_shortest_path,j_in_get_shortest_path;
for(i_in_get_shortest_path=1;i_in_get_shortest_path<=21;i_in_get_shortest_path++)
{
for(j_in_get_shortest_path=1;j_in_get_shortest_path<=21;j_in_get_shortest_path++)
{
is_visited[i_in_get_shortest_path][j_in_get_shortest_path]=false;
}
}
work_stack.push_front(start_point);
work_point=start_point;
//is_visited[work_point.x][work_point.y]=true;
while(!work_stack.empty())
{
//cout<<"跑了一次"<<endl;
if(work_point.x+1==end_x&&work_point.y==end_y||work_point.x-1==end_x&&work_point.y==end_y||work_point.x==end_x&&work_point.y+1==end_y||work_point.x==end_x&&work_point.y-1==end_y)
{
//cout<<work_point.x<<" "<<work_point.y<<endl;
shortest_step=work_point.step+1;
work_stack.clear();
break;
}else
{
if(work_point.x+1>=1&&work_point.x+1<=20&&work_point.y>=1&&work_point.y<=20&&is_visited[work_point.x+1][work_point.y]==false&&b[work_point.x+1][work_point.y]=='.')
{
//cout<<"进入1"<<endl;
is_visited[work_point.x+1][work_point.y]=true;
add_point.step=work_point.step+1;
add_point.x=work_point.x+1;
add_point.y=work_point.y;
work_stack.push_back(add_point);
}
if(work_point.x-1>=1&&work_point.x-1<=20&&work_point.y>=1&&work_point.y<=20&&is_visited[work_point.x-1][work_point.y]==false&&b[work_point.x-1][work_point.y]=='.')
{
//cout<<"进入2"<<endl;
is_visited[work_point.x-1][work_point.y]=true;
add_point.step=work_point.step+1;
add_point.x=work_point.x-1;
add_point.y=work_point.y;
work_stack.push_back(add_point);
}
if(work_point.x>=1&&work_point.x<=20&&work_point.y+1>=1&&work_point.y+1<=20&&is_visited[work_point.x][work_point.y+1]==false&&b[work_point.x][work_point.y+1]=='.')
{
//cout<<"进入3"<<endl;
is_visited[work_point.x][work_point.y+1]=true;
add_point.step=work_point.step+1;
add_point.x=work_point.x;
add_point.y=work_point.y+1;
work_stack.push_back(add_point);
}
if(work_point.x>=1&&work_point.x<=20&&work_point.y-1>=1&&work_point.y-1<=20&&is_visited[work_point.x][work_point.y-1]==false&&b[work_point.x][work_point.y-1]=='.')
{
//cout<<"进入4"<<endl;
is_visited[work_point.x][work_point.y-1]=true;
add_point.step=work_point.step+1;
add_point.x=work_point.x;
add_point.y=work_point.y-1;
work_stack.push_back(add_point);
}
work_point=*work_stack.begin();
work_stack.pop_front();
}
//cout<<"=============="<<work_point.x<<" "<<work_point.y<<"=============="<<endl;
//for(i_in_get_shortest_path=1;i_in_get_shortest_path<=13;i_in_get_shortest_path++)
//{
// for(j_in_get_shortest_path=1;j_in_get_shortest_path<=21;j_in_get_shortest_path++)
// {
// if(is_visited[i_in_get_shortest_path][j_in_get_shortest_path])
// {
// cout<<".";
// }else if(b[i_in_get_shortest_path][j_in_get_shortest_path]=='x')
// {
// cout<<"x";
// }
// else if(!is_visited[i_in_get_shortest_path][j_in_get_shortest_path])
// cout<<" ";
//
// }
// cout<<i_in_get_shortest_path<<endl;
//}
//cout<<"================================================================"<<endl;
}
cout<<"函数开始处end_x= "<<end_x<<endl;
return shortest_step;
}
void sort(int start,int end)
{
if(start>end)
{
for_compare=0;
sx=2;
sy=4;
for(int i_in_sort=1;i_in_sort<=trash_count;i_in_sort++)
{
end_point_for_sort=trash_vector[a[i_in_sort]-1];
ex=end_point_for_sort.x;
ey=end_point_for_sort.y;
//问题出在这里,明明没有传值进去,在get_shortest_path函数内也没有改动过ex,为什么调用get_shortest_path函数之后ex会变为0======================================================================================================================
cout<<"计算之前 ex= "<<ex<<endl;
for_added += get_shortest_path(sx,sy,ex,ey);
cout<<"计算完后 ex= "<<ex<<endl<<endl<<endl;
//============================================================================================================================================================================================================================================
sx=ex;
sy=ey;
//cout<<"for_added= "<<for_added<<endl;
for_compare+=for_added;
}
if(for_compare<least_step_total)
{
least_step_total=for_compare;
}
//cout<<"当前为"<<for_compare<<endl;
//cout<<"当前最小为"<<least_step_total<<endl<<endl;
return;
}
for(int i=start;i<=end;i++)
{
swap(&a[i],&a[start]);
sort(start+1,end);
swap(&a[i],&a[start]);
}
}
int main(void)
{
initial_number_group();
//sort(6,9);
get_b();
trash_count=trash_vector.size();
sort(1,3);
//cout<<get_shortest_path(2,4,12,3)<<endl;
//cout<<get_shortest_path(12,3,12,13)<<endl;
//cout<<get_shortest_path(12,13,2,13)<<endl;
system("pause");
return 0;
}
cout<<"计算之前 ex= "<<ex<<endl; for_added += get_shortest_path(sx,sy,ex,ey); cout<<"计算完后 ex= "<<ex<<endl<<endl<<endl;
函数里面是没动ex的 也没把地址传进去 怎么调用完函数ex就变成0了 我怀疑是编译器的问题