Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

使用状态记录上一步位置

Posted by Hzj_jie at 2013-03-06 23:58:00 on Problem 3414 and last updated at 2013-03-07 00:12:01
一般bfs的时候需要记录一个状态是否已经到达过,所以可以正好利用这个记录来标记上一步的状态。
还有就是因为a和b都小于101,所以可以用一个整数来标记一个状态。
不过最烦躁的是六种不同的操作,实在是写的太恶心了。

#include <stdio.h>
#include <queue>
#include <vector>
using namespace std;
const int MAX = 100;
#define ASSERT(x) {if(!(x)) { int a = 0; while(1) a /= a; } }

inline int pos(int a, int b, int max_a) { return a + b * (max_a + 1); }
inline void status(int& a, int& b, int max_a, int c)
{
    a = c % (max_a + 1);
    b = c / (max_a + 1);
}

void print(const vector<int>& v, const int max_a)
{
    printf("%d\n", v.size() - 1);
    for(int i = v.size() - 1; i > 0; i--)
    {
        int ca, cb, la, lb;
        status(ca, cb, max_a, v[i - 1]);
        status(la, lb, max_a, v[i]);
        if(la == 0 && cb == lb) printf("FILL(1)\n");
        else if(lb == 0 && ca == la) printf("FILL(2)\n");
        else if(ca == 0 && cb == lb) printf("DROP(1)\n");
        else if(cb == 0 && ca == la) printf("DROP(2)\n");
        else if(ca < la && cb > lb) printf("POUR(1,2)\n");
        else if(ca > la && cb < lb) printf("POUR(2,1)\n");
        else ASSERT(false);
    }
}

bool run()
{
    const int undef = -1;
    int max_a, max_b, exp;
    if(scanf("%d%d%d", &max_a, &max_b, &exp) == EOF) return false;
    int last_step[MAX * MAX + 1];
    for(int i = 0; i <= max_a; i++) for(int j = 0; j <= max_b; j++) last_step[pos(i, j, max_a)] = undef;
    queue<int> q;
    q.push(0);
    while(!q.empty())
    {
        int c = q.front();
        q.pop();
        int a, b;
        status(a, b, max_a, c);
        int na, nb;
        for(int i = 0; i < 6; i++)
        {
            if(i == 0)
            {
                na = max_a;
                nb = b;
            }
            else if(i == 1)
            {
                na = a;
                nb = max_b;
            }
            else if(i == 2)
            {
                na = 0;
                nb = b;
            }
            else if(i == 3)
            {
                na = a;
                nb = 0;
            }
            else if(i == 4)
            {
                na = a + b;
                nb = 0;
                if(na > max_a)
                {
                    nb = na - max_a;
                    na = max_a;
                }
            }
            else if(i == 5)
            {
                nb = a + b;
                na = 0;
                if(nb > max_b)
                {
                    na = nb - max_b;
                    nb = max_b;
                }
            }

            int p = pos(na, nb, max_a);
            if(na == exp || nb == exp)
            {
                ASSERT(last_step[p] == undef);
                last_step[p] = c;
                vector<int> v;
                while(p != undef)
                {
#ifndef ONLINE_JUDGE
                    int a, b;
                    status(a, b, max_a, p);
                    printf("%d => %d %d\n", p, a, b);
#endif
                    v.push_back(p);
                    p = last_step[p];
                }
                print(v, max_a);
                return true;
            }
            else if((na > 0 || nb > 0) && last_step[p] == undef)
            {
                last_step[p] = c;
                q.push(p);
            }
        }
    }

    printf("impossible\n");
    return true;
}

int main() { while(run()); }

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator