[HDU] [1495] Very Cola

Problem Description

Loading...

Problem Analysis

Looking at the problems under the search category in the blog, there aren’t that many……
Why do I feel like I’ve already done a lot of search problems……-.-

This problem is still very simple
(By the way, I suddenly feel that this kind of search isn’t hard; once the conditions are set, you don’t need to think much)

First, for the cola to be pourable, S&1==0, meaning S should be even
Then when N==M, it is obvious that only one pour is needed
Search over the remaining cases
The termination condition is that S contains S/2 of the cola, and the larger of N and M contains the remaining half of the cola

Then…… just search
For Pruning
Build an array to record whether the current state has already been searched

The highlight of this code, I feel, is still
saving data through arrays
can achieve the purpose of searching with loops
instead of writing them out one by one

cpp
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
int rflag[105][105][105];
struct node {
	int x[3];
	int cnt;
} t;
int main() {
//	freopen("input.txt","r",stdin);
//	freopen("output.txt","w",stdout);

	int l[3];
	while(scanf("%d %d %d",&l[0],&l[1],&l[2]),l[0]||l[1]||l[2]) {
		memset(rflag,0,sizeof(rflag));
		if(l[0]&1)
			printf("NO\n");
		else if(l[1]==l[2])
			printf("1\n");
		else {
			if(l[1]>l[2]) {
				int t=l[1];
				l[1]=l[2];
				l[2]=t;
			}
			queue<node>q;
			t.x[0]=l[0];
			t.x[1]=0;
			t.x[2]=0;
			t.cnt=0;
			q.push(t);
			bool flag=false;
			int res;
			while(!q.empty()) {
				t=q.front();
				if(rflag[t.x[0]][t.x[1]][t.x[2]]) {
					q.pop();
					continue;
				}
				rflag[t.x[0]][t.x[1]][t.x[2]]=1;
				if(t.x[0]==l[0]/2&&t.x[1]==0&&t.x[2]==l[0]/2) {
					flag=true;
					res=t.cnt;
					break;
				}
				for(int i=0; i<3; i++) {
					if(t.x[i]>0) {
						for(int j=0; j<3; j++) {
							if(i==j||t.x[j]==l[j])
								continue;
							node tq=t;
							if(t.x[i]+t.x[j]>l[j]) {
								tq.x[i]=t.x[i]+t.x[j]-l[j];
								tq.x[j]=l[j];
								tq.cnt=t.cnt+1;
								q.push(tq);
							} else {
								tq.x[i]=0;
								tq.x[j]=t.x[i]+t.x[j];
								tq.cnt=t.cnt+1;
								q.push(tq);
							}
						}
					}
				}
				q.pop();
			}
			if(flag)
				printf("%d\n",res);
			else
				printf("NO\n");
		}
	}
	return 0;
}

Problem address:HDU 1495 Very Cola

This post is licensed under CC BY-NC-SA 4.0. If it helped, or if you have anything to say, the comment box below is open.
Page views —
Unique visitors —

Loading...