I tried to solve algorithm that print out the maximum value.
And it printed out segmentation fault. I looked up for any memory access, but I couldn't find any mistakes..
Please help me why the code occurs segmentation fault..
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
int M, A;
int ans=0;
struct BC{
int x, y;
int C;
int P;
bool valid;
}ap[8];
int path[2][100];
int dx[5]={0, 0, 1, 0, -1};
int dy[5]={0, -1, 0, 1, 0};
struct people{
int x, y;
}human[2];
void init(){
memset(path, 0, sizeof(path));
std::cin>>M>>A;
for(int j=0; j<2; j++){
for(int i=0; i<M; i++){
std::cin>>path[j][i];
}
}
for(int i=0; i<A; i++){
std::cin>>ap[i].x>>ap[i].y>>ap[i].C>>ap[i].P;
ap[i].valid=true;
}
ans=0;
human[0].x=1;
human[0].y=1;
human[1].x=10;
human[1].y=10;
}
bool pos(int h, int idx){
int dist=abs(human[h].x-ap[idx].x)+abs(human[h].y-ap[idx].y);
if(dist<=ap[idx].C) return true;
return false;
}
int ret=0;
void dfs(int user, int d){
if(user==2){
if(ret<d) ret=d;
return;
}
for(int i=0; i<A; i++){
if(ap[i].valid && pos(user, i)){
ap[i].valid=false;
dfs(user+1, d+ap[i].P);
ap[i].valid=true;
}
}
dfs(user+1, d);
}
void solve(){
ret=0;
dfs(0,0);
ans+=ret;
for(int i=0; i<M; i++){
for(int j=0; j<2; j++){
human[j].x+=dx[path[j][i]];
human[j].y+=dy[path[j][i]];
}
ret=0;
dfs(0,0);
ans+=ret;
}
}
int main(){
std::ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int TC;
for(int i=0; i<TC; i++){
init();
solve();
std::cout<<"#"<<i+1<<" "<<ans<<"\n";
}
return 0;
}
sample input is like this:
1
20 3
2 2 3 2 2 2 2 3 3 4 4 3 2 2 3 3 3 2 2 3
4 4 1 4 4 1 4 4 1 1 1 4 1 4 3 3 3 3 3 3
4 4 1 100
7 10 3 40
6 3 2 70
Aucun commentaire:
Enregistrer un commentaire