Poj 3414 Pots (bfs)

Source: Internet
Author: User

Link: poj 3414

Question: The capacity of two bottles A, B, and A target water volume C are given. You can perform the following operations on A and B:

FILL (I) fills the bottle with water

DROP (I) empty the bottle I

POUR (I, j) POUR the water in bottle I into bottle j. After this operation, either the bottle j is filled with water or the bottle I is empty

Perform at least several operations to make the water in A or B package C and output the specific operation.

Analysis: bfs can be used in six aspects. To output specific operations, you can use arrays to simulate queues,

Record the status of the previous operation, and then trace the path.


# Include <stdio. h> # include <string. h> # include <stdlib. h> struct stu {int step, a, B, pos;} que [1010]; int a, B, c, ope [1010], step [1010], x, y, front, rear; bool vis [105] [105]; void fill (int I) {if (I = 1) x = a; else y = B ;} void drop (int I) {if (I = 1) x = 0; else y = 0;} void pour (int I, int j) {if (I = 1) {if (x + y> = B) {x-= B-y; y = B;} else {y + = x; x = 0 ;}} else {if (x + y> = a) {y-= a-x; x = a;} else {x + = y; y = 0 ;}} void is_push (int step, I Nt num) {if (! Vis [x] [y]) {que [rear]. a = x; que [rear]. B = y; que [rear]. step = step + 1; // record the number of steps que [rear]. pos = front; // record the previous state ope [rear ++] = num; // The current operation vis [x] [y] = true; // Mark as Accessed} int bfs () {int I, num, q [7] = {0,110,120,210,220,312,321}; // The q array is used to mark the executed operation, in order to finally output struct stu t; front = 0; rear = 1; memset (vis, 0, sizeof (vis); vis [0] [0] = 1; que [front]. a = que [front]. B = 0; que [front]. step = 0; while (front <rear) {t = que [front]; if (t. a = c | t. B = c ) {Num = t. step; step [num] = front; num --; while (num) {step [num] = que [step [num + 1]. pos; num --;} return t. step;} for (I = 1; I <= 6; I ++) {x = t. a; y = t. b; switch (I) {case 1: fill (1); break; case 2: fill (2); break; case 3: drop (1); break; case 4: drop (2); break; case 5: pour (1, 2); break; case 6: pour (2, 1);} is_push (t. step, q [I]);} front ++;} return-1;} int main () {int sum, I, k; while (scanf ("% d", & a, & B, & c )! = EOF) {sum = bfs (); if (sum =-1) {printf ("impossible \ n"); continue;} printf ("% d \ n ", sum); for (I = 1; I <= sum; I ++) {k = ope [step [I]; if (k/100 = 1) printf ("FILL (% d) \ n", k/10% 10); else if (k/100 = 2) printf ("DROP (% d) \ n ", k/10% 10); else if (k/100 = 3) printf ("POUR (% d, % d) \ n", k/10% 10, k % 10) ;}} return 0 ;}


Poj 3414 Pots (bfs)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.