POJ 3414 Pots(BFS+回溯)

#include<iostream>#include<algorithm>#include<stdio.h>#include<string.h>#include<stdlib.h>#include<queue>using namespace std;int n,m,k;int ans;int v[110][110];struct node{int x;int y;int z;int cnt;} a[1000010];void DFS(int kk){int pt = a[kk].cnt;if(pt<=0){return ;}DFS(pt);if(a[pt].x == 1){if(a[pt].y == 1){printf("FILL(1)\n");}else{printf("FILL(2)\n");}}else if(a[pt].x == 2){if(a[pt].y == 1){printf("DROP(1)\n");}else{printf("DROP(2)\n");}}else if(a[pt].x == 3){if(a[pt].y == 1){printf("POUR(1,2)\n");}else{printf("POUR(2,1)\n");}}}void BFS(){ans = 1;queue<node>q;memset(v,0,sizeof(v));struct node t,f;t.x = 0;t.y = 0;t.z = 0;t.cnt = 0;a[0].x = 0;a[0].y = 0;a[0].cnt = 0;q.push(t);v[t.x][t.y] = 1;while(!q.empty()){t = q.front();q.pop();for(int i=1; i<=3; i++){for(int j=1; j<=2; j++){f.x = t.x;f.y = t.y;if(i == 1){if(j == 1 && f.x!=n){f.x = n;}else if(j == 2 && f.y!=m){f.y = m;}}else if(i == 2){if(j == 1 && f.x!=0){f.x = 0;}else if(j == 2 && f.y!=0){f.y = 0;}}else if(i == 3){if(j == 1 && (f.x!=0 && f.y!=m)){if(f.x>=m-f.y){f.x = f.x – m + f.y;f.y = m;}else{f.y = f.y + f.x;f.x = 0;}}else if(j == 2 && (f.y!=0 && f.x!=n)){if(f.y>=n-f.x){f.y = f.y – n + f.x;f.x = n;}else{f.x = f.x + f.y;f.y = 0;}}}if(v[f.x][f.y] == 0){f.cnt = ans;f.z = t.z + 1;a[ans].x = i;a[ans].y = j;a[ans].cnt = t.cnt;q.push(f);v[f.x][f.y] = 1;if(f.x == k || f.y == k){printf("%d\n",f.z);DFS(ans);if(i == 1){if(j == 1){printf("FILL(1)\n");}else{printf("FILL(2)\n");}}else if(i == 2){if(j == 1){printf("DROP(1)\n");}else{printf("DROP(2)\n");}}else if(i == 3){if(j == 1){printf("POUR(1,2)\n");}else{printf("POUR(2,1)\n");}}return ;}ans++;}}}}printf("impossible\n");}int main(){while(scanf("%d%d%d",&n,&m,&k)!=EOF){BFS();}return 0;}

,收敛自己的脾气,偶尔要刻意沉默,

POJ 3414 Pots(BFS+回溯)

相关文章:

你感兴趣的文章:

标签云: