Пример #1
0
void pour(int x, int y){
    if(x + 1 < w){
        if(grid[y][x] > grid[y][x + 1]){
            grid[y][x + 1]++;
            spread++;
            pour(x + 1, y);
        }
    }
    if(x - 1 >= 0){
        if(grid[y][x] > grid[y][x - 1]){
            grid[y][x - 1]++;
            spread++;
            pour(x - 1, y);
        }
    }
    if(y + 1 < h){
        if(grid[y][x] > grid[y + 1][x]){
            grid[y + 1][x]++;
            spread++;
            pour(x, y + 1);
        }
    }
    if(y - 1 >= 0){
        if(grid[y][x] > grid[y - 1][x]){
            grid[y - 1][x]++;
            spread++;
            pour(x, y - 1);
        }
    }
}
Пример #2
0
int main(){
    int p;
    int i, j;
    int x, y;

    while(1){
        scanf("%d %d %d", &w, &h, &p);
        if(w == 0 && h == 0 && p == 0)break;
        for(i = 0; i < h; i++){
            for(j = 0; j < w; j++){
                scanf("%d", &grid[i][j]);
            }
        }
        if(p){
            spread = 1;
            while(p--){
                scanf("%d %d", &x, &y);
                pour(x, y);
            }
        }else{
            spread = 0;
        }
        printf("%d\n", spread);
    }

    return 0;
}
Пример #3
0
void search_heuristic(void) {
unsigned i,j,k,par,all=1;

for(i=0; i<MAXBUCKET; i++)
   all*=(full_buckets[i]+1);
searched=(char *)malloc(all);
memset(searched,0,all);
k=(vertices+ffree-1)->bucket[0];
for(i=1; i<MAXBUCKET; i++)
   k=k*(full_buckets[i]+1)+(vertices+ffree-1)->bucket[i];
searched[k]=1;
enqueue_heuristic(ffree - 1);

while(1) {
   par = dequeue();
   if (par == 0)
      break;
   if (check(par)) {
      save(par, (vertices + par)->backptr);
      break;
   }

   for(i = 0; i < MAXBUCKET; i++) {
      if (!isfull(par, i)) {
         fill(par,i);
         putnew_heuristic(par);
         for(j = 0; j < MAXBUCKET; j++) {
            if (i != j && !isempty(par, j)) {
               pour(par, j, i);
               putnew_heuristic(par);
            }
         }
      }

      if (!isempty(par, i)) {
         empty(par,i);
         putnew_heuristic(par);
      }
   }
}

free(searched);

printf("SEARCH: DONE\n");
fprintf(trace,"SEARCH: DONE\n");
}
Пример #4
0
int main() {
  int v1, v2, v3, px, py, x, y;
  freopen("fg.in", "r", stdin);
  freopen("fg.out", "w", stdout);
  scanf("%d%d", &N, &M);
  for (int i = 0; i < N; i ++) {
    for (int j = 0; j < M; j ++) {
      scanf("%d", &map[i][j]);
      map[i][j] --;
      pos[map[i][j]] = i * M + j;
    }
  }
  memset(bl, 255, sizeof(bl));
  memset(fa, 255, sizeof(fa));
  for (int i = 0; i < N * M; i ++) {
    px = pos[i] / M;
    py = pos[i] % M;
    if (can(px, py)) {
      bl[i] = P ++;
      for (int j = 0; j < 4; j ++) {
        x = px + dx[j]; 
        y = py + dy[j];
        if (x >= 0 && x < N && y >= 0 && y < M && bl[map[x][y]] == -1 && map[x][y] < i) {
          fa[P] = i;
          bfs(x, y, P ++, i);
        }
      }
    }
  }
  scanf("%d", &Q);
  for (int i = 0; i < Q; i ++) {
    scanf("%d%d%d", &v1, &v2, &v3);
    v1 --; v2 --;
    V[bl[map[v1][v2]]] += v3;
  }
  for (int i = N * M - 1; i >= 0; i --) if (!size[bl[i]]) {
    while (true) {
      pour(i);
      if (V[bl[i]] < Eps) break;
    }
  }
  printf("%.2lf\n", ans);
  return 0;
}
Пример #5
0
void filter(STACK *stream,STACK *integral,STACK *decimal)
{	
	STACK temp;
	init_stack(&temp);
	while(!is_empty(stream) && (int) peek(stream)!=DECIMAL_PT)
		push(&temp,pop(stream));
	
	if(is_empty(stream))
		copy_stack(&temp,integral);
	else
	{	
		pour(decimal,&temp);
		pop(stream);
		if(is_empty(stream))
			push(integral,0);
		else
			while(!is_empty(stream))
				push(integral,pop(stream));
	}
}
Пример #6
0
float process_data(STACK *pst1)
{	float d;
	STACK *pst2;
	init_stack((pst2=create_stack()));
	pour(pst2,pst1);
	while(!is_empty(pst2)){
		d = pop(pst2);
		if(!is_empty(pst2))
			push(pst2,10*d + pop(pst2));
		else {
			push(pst2,d);
			return peek(pst2);
		}
	}
	//while(pst2->top>0){
	//	d = pop(pst2);
	//	pst2->item[pst2->top] = 10*d + pst2->item[pst2->top];
	//}
	//empty(pst1);   //pst points to stack temp this process is superfluous
			//as temp has been make empty during call to pour()
	//return pst2->item[pst2->top];
}
Пример #7
0
void copy_stack(STACK *ps1,STACK *ps2)
{
	STACK temp;init_stack(&temp);
	pour(&temp,ps1);
	pour(ps2,&temp);
}
 // Get the front element.
 int peek(void) {
     pour(s1, s2);
     int top = s2.top();
     pour(s2, s1);
     return top;
 }
 // Removes the element from in front of queue.
 void pop(void) {
     pour(s1, s2);
     s2.pop();
     pour(s2, s1);
 }
Пример #10
0
void visit(int a, int b, int c) {
  if (state_visited[a][b][c]) return;
  state_visited[a][b][c] = 1;
  print_state[c] |= (a == 0);
  
  visit(a-pour(a, b, b_cap), b+pour(a, b, b_cap) , c);
  visit(a-pour(a, c, c_cap), b, c+pour(a, c, c_cap));
  visit(a+pour(b, a, a_cap), b-pour(b, a, a_cap) , c);
  visit(a, b-pour(b, c, c_cap) , c+pour(b, c, c_cap));
  visit(a+pour(c, a, a_cap), b, c-pour(c, a, a_cap) );
  visit(a, b+pour(c, b, b_cap) , c-pour(c, b, b_cap));
}