think: 1今天上午AC了自己的第三道英文題,感覺和英文題目的距離不再那么遙遠,而且自己在這個題目開始逐漸嘗試學習使用C++編程,雖然只是頭文件,但感覺自己不再那么畏懼,開始逐漸嘗試戰勝自己的畏懼,害怕難免會有,想起畢淑敏的一句話,你生而有翼,又何必匍匐前行,相信自己可以戰勝自己的消極情緒,做一個積極向上的人,自己曾經考慮過用大學時光來寫一本書,把自己的大學時光記錄下來,或許,自己應該燃起曾經想過的希望,寫一本自己的書,寫一本平凡人的自傳,或許我可以用這本書以記錄我的編程時光為主,日后想想現在的自己,正如現在的自己想想高中的那些努力的時光,那種感動猶如陽光灑在自己的身上,暖暖的 2回歸題目,題意是我們有兩個杯子A和B,需要盛C升水,有3種宏觀操作,共包含6種詳細操作, FILL:1>A歸0,B不變 2>A不變,B歸零 DROP:1>A盛滿,B不變 2>A不變,B盛滿 POUR:1>把A倒入B 2>把B倒入A 注:POUR操作需要注意會不會有一個杯子會有水剩余
sdut原題鏈接
Pots Time Limit: 1000MS Memory Limit: 65536KB
PRoblem Description You are given two pots, having the volume of A and B liters respectively. The following Operations can be performed: FILL(i) fill the pot i (1 ≤ i ≤ 2) from the tap; DROP(i) empty the pot i to the drain; POUR(i,j) pour from pot i to pot j; after this operation either the pot j is full (and there may be some water left in the pot i), or the pot i is empty (and all its contents have been moved to the pot j). Write a program to find the shortest possible sequence of these operations that will yield exactly C liters of water in one of the pots.
Input On the first and only line are the numbers A, B, and C. These are all integers in the range from 1 to 100 and C≤max(A,B).
Output The first line of the output must contain the length of the sequence of operations K. If the desired result can’t be achieved, the first and only line of the file must contain the Word ‘impossible’.
Example Input 3 5 4
Example Output 6
Hint FILL(2) POUR(2,1) DROP(1) POUR(2,1) FILL(2) POUR(2,1)
Author
以下為accepted代碼
#include <iostream>#include <stdio.h>#include <string.h>#include <stdlib.h>using namespace std;int a, b, c, tp, op;int vis[104][104];struct node{ int a; int b; int ans;}link[312];void FILL(int x, int y, int size, int l){ struct node f; if(l == 0) { f.a = a; f.b = y; } else if(l == 1) { f.a = x; f.b = b; } if(vis[f.a][f.b] == 0) { f.ans = size + 1; link[tp++] = f; vis[f.a][f.b] = 1; }}void DROP(int x, int y, int size, int l){ struct node f; if(l == 2) { f.a = 0; f.b = y; } else if(l == 3) { f.a = x; f.b = 0; } if(vis[f.a][f.b] == 0) { f.ans = size + 1; link[tp++] = f; vis[f.a][f.b] = 1; }}void POUR(int x, int y, int size, int l){ struct node f; if(l == 4)//a->b { if(x + y <= b) { f.a = 0; f.b = x + y; } else if(x + y > b) { f.a = x + y - b; f.b = b; } } else if(l == 5)//b->a { if(x + y <= a) { f.a = x + y; f.b = 0; } else if(x + y > a) { f.a = a; f.b = x + y -a; } } if(vis[f.a][f.b] == 0) { f.ans = size + 1; link[tp++] = f; vis[f.a][f.b] = 1; }}void BFS(){ struct node t, f; tp = op = 0; memset(vis, 0, sizeof(vis)); t.a = 0; t.b = 0; t.ans = 0; link[tp++] = t; vis[t.a][t.b] = 1; while(op < tp) { f = link[op++]; if(f.a == c ||f.b == c) { printf("%d/n", f.ans); return; } for(int i = 0; i < 6; i++) { if(i == 0) FILL(f.a, f.b, f.ans, i); if(i == 1) FILL(f.a, f.b, f.ans, i); if(i == 2) DROP(f.a, f.b, f.ans, i); if(i == 3) DROP(f.a, f.b, f.ans, i); if(i == 4) POUR(f.a, f.b, f.ans, i); if(i == 5) POUR(f.a, f.b, f.ans, i); } } printf("impossible/n");}int main(){ while(scanf("%d %d %d", &a, &b, &c) != EOF) { BFS(); } return 0;}/***************************************************User name: Result: AcceptedTake time: 0msTake Memory: 200KBSubmit time: 2017-02-17 11:44:12****************************************************/新聞熱點
疑難解答