CCF-CSP 20180302:小球碰撞

yunxiaoMr發表於2020-11-27
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
#define N 101
#define T 101
#define L 1001
int pos[N];
int v[N];
int id[N];
bool check(int pos[],int j,int l,int n){
	bool flag = false;
	
	if(pos[j]==0||pos[j]==l)  {
			flag = true; 
		}	
	if(j==0){
		if(pos[j]==pos[j+1]) {
			flag = true; 
		}	
	}else if(j==n-1){
		if(pos[j]==pos[j-1]) {
			flag = true; 
		}	
	}else{
		if(pos[j]==pos[j-1]||pos[j]==pos[j+1]) {
			flag = true; 
		}	 
	}	
	return flag;
}
int change(int pos,int v){
	int new_pos = pos+v; 
	return new_pos;
}
int xiangfanshu(int v){
	return (-1)*v;
}
bool cmp_second(pair<int, int> id_pos1,pair<int, int>  id_pos2){
	return id_pos1.second<id_pos2.second;
}
bool cmp_first(pair<int, int> id_pos1,pair<int, int>  id_pos2){
	return id_pos1.first<id_pos2.first;
}
int main(){
	int n,t,l;
	cin>>n>>l>>t;
	vector< pair<int, int> > id_pos;
	for(int i=0;i<n;i++){
		cin>>pos[i];
		id_pos.push_back(make_pair(i,pos[i]));
		v[i]=1;
	}
	
	sort(id_pos.begin(),id_pos.end(),cmp_second);
	
	for(int i=0;i<n;i++){
		id[i]=id_pos[i].first;
		pos[i]=id_pos[i].second;
	}
	
	for(int i=1;i<=t;i++){
		for(int j=0;j<n;j++){
			pos[j]=change(pos[j],v[j]); //經過了1秒後,j小球移動到了新的pos[j] 
		}
		
		for(int j=0;j<n;j++){
			if(check(pos,j,l,n)){  	
				//判斷是否有球相撞或者撞到邊界,改變位置和方向 
				v[j]=xiangfanshu(v[j]);
			}else{ 			
				//沒有相撞改變位置 	
			}
		}
	}
	for(int i=0;i<n;i++){
		id_pos[i].first=id[i];
		id_pos[i].second=pos[i];
	}
	sort(id_pos.begin(),id_pos.end(),cmp_first);
	for(int i=0;i<n;i++){
		cout<<id_pos[i].second<<" ";
	}
	
	return 0;
}

 

相關文章