POJ 3414 Pots DFS

本题枚举每次可行的方案,并对枚举的上限做了限制,即如果当前的枚举次数已经大于目前最小次数解就剪枝。

/***********************************************
 * Author: fisty
 * Created Time: 2015/2/6 17:29:18
 * File Name   : 1_H.cpp
 *********************************************** */
#include <iostream>
#include <cstring>
#include <deque>
#include <cmath>
#include <queue>
#include <stack>
#include <list>
#include <map>
#include <set>
#include <string>
#include <vector>
#include <cstdio>
#include <bitset>
#include <algorithm>
using namespace std;
#define Debug(x) cout << #x << " " << x <<endl
#define Memset(x, a) memset(x, a, sizeof(x))
const int INF = 0x3f3f3f3f;
typedef long long LL;
typedef pair<int, int> P;
#define FOR(i, a, b) for(int i = a;i < b; i++)
#define MAX_N 1000
int deep, ok;
int A, B, C;
string ans[MAX_N], _ans[MAX_N];
int vis[110][110];

void dfs(int x, int y, int cnt){
    if(deep < cnt) return ;
    if(x == C || y == C){
        if(cnt < deep) {
            deep = cnt;
            FOR(i, 0, deep) _ans[i] = ans[i];
            ok = 1;
        }
        return ;
    }
    if(x < A && !vis[A][y]){
        vis[A][y] = 1;
        ans[cnt] = "FILL(1)";
        dfs(A, y, cnt+1);
        vis[A][y] = 0;
    }
    if(y < B && !vis[x][B]){
        vis[x][B] = 1;
        ans[cnt] = "FILL(2)";
        dfs(x, B, cnt+1);
        vis[x][B] = 0;
    }
    if(x > 0 && !vis[0][y]){
        vis[0][y] = 1;
        ans[cnt] = "DROP(1)";
        dfs(0, y, cnt+1);
        vis[0][y] = 0;
    }
    if(y > 0 && !vis[x][0]){
        vis[x][0] = 1;
        ans[cnt] = "DROP(2)";
        dfs(x, 0, cnt+1);
        vis[x][0] = 0;
    }
    if(x > 0 && y < B){
        int t = min(x, B-y);
        if(!vis[x-t][y+t]){
            vis[x-t][y+t] = 1;
            ans[cnt] = "POUR(1,2)";
            dfs(x-t, y+t, cnt+1);
            vis[x-t][y+t] = 0;
        }
    }
    if(y > 0 && x < A){
        int t = min(y, A-x);
        if(!vis[x+t][y-t]){
            vis[x+t][y-t] = 1;
            ans[cnt] = "POUR(2,1)";
            dfs(x+t, y-t, cnt+1);
            vis[x+t][y-t] = 0;
        }
    }
    return ;
}
int main() {
    //freopen("in.cpp", "r", stdin);
    cin.tie(0);
    ios::sync_with_stdio(false);
    cin >> A >> B >> C;
    deep = INF; ok = 0;
    Memset(vis, 0);
    vis[0][0] = 1;
    dfs(0, 0, 0);
    if(ok){
        cout << deep << endl;
        FOR(i, 0, deep) cout << _ans[i] << endl; 
    }else{
        cout << "impossible" << endl;
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值