概述:三个杯子倒可乐,杯子之间互倒,求是否能够均等分开。
思路:杯子可乐为s,m,n,s中为s,mn为0。6种情况:s倒m,s倒n,m倒n,m倒s,n倒m,n倒s。最后n、s中能等分即可。
感想:上课讲的例题,所以直接把老师的代码拿来copy了.../脸红...只是出现了两次ce,改了头文件过了,原因大概是因为网站的编译器需要的头文件完整写出,不像一般的编译器,部分头文件可以从iostream中读取。
#include<iostream> #include<algorithm> #include<queue> #include<stdio.h> #include<cmath> #include<string.h> #include<fstream> using namespace std; #define maxn 101 bool visited[maxn][maxn]; int m, n, s, si, sj; struct node { int x, y, all, t; //x,y,all分别表示m,n,s杯中可乐的体积,t表示倒了多少次 }; void BFS() { queue<node> que; memset(visited, false, sizeof(visited)); node p, q; p.x = 0, p.y = 0, p.t = 0, p.all = s; que.push(p); visited[p.x][p.y] = true; while (!que.empty()) { p = que.front(); que.pop(); if (p.y == p.all && p.y == s / 2) { printf("%d\n", p.t); return; } if (p.all + p.x>m) //s倒m { q.x = m, q.y = p.y, q.all = p.all + p.x - m, q.t = p.t + 1; if (!visited[q.x][q.y]) que.push(q), visited[q.x][q.y] = true; } else { q.x = p.all + p.x, q.y = p.y, q.all = 0, q.t = p.t + 1; if (!visited[q.x][q.y]) que.push(q), visited[q.x][q.y] = true; } if (p.all + p.y>n) //s倒n { q.x = p.x, q.y = n, q.all = p.all + p.y - n, q.t = p.t + 1; if (!visited[q.x][q.y]) que.push(q), visited[q.x][q.y] = true; } else { q.x = p.x, q.y = p.all + p.y, q.all = 0, q.t = p.t + 1; if (!visited[q.x][q.y]) que.push(q), visited[q.x][q.y] = true; } if (p.x + p.y>n) //m倒n { q.x = p.x + p.y - n, q.y = n, q.all = p.all, q.t = p.t + 1; if (!visited[q.x][q.y]) que.push(q), visited[q.x][q.y] = true; } else { q.x = 0, q.y = p.x + p.y, q.all = p.all, q.t = p.t + 1; if (!visited[q.x][q.y]) que.push(q), visited[q.x][q.y] = true; } q.all = p.all + p.x, q.x = 0, q.y = p.y, q.t = p.t + 1; //m倒s if (!visited[q.x][q.y]) que.push(q), visited[q.x][q.y] = true; if (p.x + p.y > m) { q.y = p.y + p.x - m, q.x = m, q.all = p.all, q.t = p.t + 1;//n倒m if (!visited[q.x][q.y]) que.push(q), visited[q.x][q.y] = true; } else { q.x = p.x + p.y, q.y = 0, q.all = p.all, q.t = p.t + 1; if (!visited[q.x][q.y]) que.push(q), visited[q.x][q.y] = true; } q.all = p.all + p.y, q.x = p.x, q.y = 0, q.t = p.t + 1; //n倒s if (!visited[q.x][q.y]) que.push(q), visited[q.x][q.y] = true; } printf("NO\n"); } int main() { //ifstream cin("in.txt"); while (scanf("%d%d%d", &s, &m, &n) && (s || m || n)) { if (s % 2) { printf("NO\n"); continue; } if (m > n) swap(m, n); BFS(); } return 0; }