void* main_loop(void* phil) { while(1) { hungry((int)phil); for(i = 0; i<99999999; i++); think((int)phil); for(i = 0; i<99999999; i++); } }
int hungry(int cnt_point) { for (int i = 1; i <= row; i++) { if (maps[cnt_point][i] && !state[i]) { state[i] = 1; if (!previous_node[i] || hungry(previous_node[i])) { previous_node[i] = cnt_point; return 1; } } } return 0; }
void* philosopher(void *arg) { int i; int tid = *( ( int* ) arg ); /*philosopher thread thinks, becomes hungry, ...*/ for (i=0;i<10;i++) { thinking( tid ); hungry( tid ); eating( tid); } return 0; }
int main() { scanf("%d%d", &row, &point_number); memset(maps, 0, sizeof(maps)); for (int i = 1; i <= point_number; i++) { int x,y; scanf("%d%d", &x, &y); maps[x][y] = 1; } int ans = 0; for (int i = 1; i <= row; i++) { memset(state, 0, sizeof(state)); if (hungry(i)) { ans++; } } printf("%d\n", ans); return 0; }
int main() { freopen("sum.in","r",stdin); freopen("sum.out","w",stdout); while(scanf("%d%d",&n,&m)!=EOF) { memset(g,false,sizeof(g)); memset(cal,0,sizeof(cal)); for(int i=0;i<n;i++) scanf("%s",ch[i]); nw = np = 0; for(int i=0;i<n;i++) { for(int j=0;j<m;j++) if(ch[i][j]=='W') //表示这个位置为狼,则给它付号为第++nw头 cal[1][i][j] = ++nw; else if(ch[i][j]=='P') cal[0][i][j] = ++np; } for(int i=0;i<n;i++) { for(int j=0;j<m;j++) if(ch[i][j]=='W') if(i>0&&ch[i-1][j]=='P') //上面为猪的话 g[cal[1][i][j]][cal[0][i-1][j]] = true; else if(i<n-1&&ch[i+1][j]=='P') //下 g[cal[1][i][j]][cal[0][i+1][j]] = true; else if(j>0&&ch[i][j-1]=='P') //左 g[cal[1][i][j]][cal[0][i][j-1]] = true; else if(j<m-1&&ch[i][j+1]=='P') //右 g[cal[1][i][j]][cal[0][i][j+1]] = true; } printf("%d\n",hungry()); } return 0; }
/* * try_eating - thread function * @arg: the pointer which point to an struct * * it seems should return a void pointer, but it's not actually. * This function is going be a dead loop unless you interrupt it, * manually. */ void *try_eating(void *arg) { /* get which philosopher reaching here */ struct philosopher *ph = (struct philosopher *) arg; int i; /* time seconds as our paramaters for random number */ srand(time(0)); while (1) { if (count >= NR) count = 0; /* lock it up */ pthread_mutex_lock(&cp_mutex[count]); i = rand()%NR; thinking(ph+i); pthread_mutex_unlock(&cp_mutex[count]); phv_index[i] = i; /* check the philosopher's location */ if ((ph + phv_index[i])->ith == 0) { pthread_mutex_lock(&cp_mutex[count]); if (cs_shared[NR - 1] == 0 && cs_shared[(ph + phv_index[i])->ith] == 0) { cs_shared[NR - 1] = 1; cs_shared[0] = 1; pthread_mutex_unlock(&cp_mutex[count]); eating(ph + phv_index[i]); pthread_mutex_lock(&cp_mutex[count]); cs_shared[NR - 1] = 0; cs_shared[0] = 0; pthread_mutex_unlock(&cp_mutex[count]); } else { hungry(ph + phv_index[i]); cs_shared[NR - 1] = 0; cs_shared[0] = 0; pthread_mutex_unlock(&cp_mutex[count]); } } else { pthread_mutex_lock(&cp_mutex[1]); if (cs_shared[(ph + phv_index[i])->ith - 1] == 0 && cs_shared[(ph + phv_index[i])->ith] == 0) { cs_shared[(ph + phv_index[i])->ith - 1] = 1; cs_shared[(ph + phv_index[i])->ith] = 1; eating(ph + phv_index[i]); pthread_mutex_unlock(&cp_mutex[1]); pthread_mutex_lock(&cp_mutex[1]); cs_shared[(ph + phv_index[i])->ith - 1] = 0; cs_shared[(ph + phv_index[i])->ith] = 0; pthread_mutex_unlock(&cp_mutex[1]); } else { hungry(ph + phv_index[i]); cs_shared[(ph + phv_index[i])->ith - 1] = 0; cs_shared[(ph + phv_index[i])->ith] = 0; pthread_mutex_unlock(&cp_mutex[1]); } } sleep(1); } }
int main () { do{ eat_humberger(); }while (hungry()); }