Plan hdoj3377

确定起点和终点的简单路径问题,可以改造成一条回路问题,也可以修改转移条件来实现。


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <queue>
#include <algorithm>
#include <vector>
#include <cstring>
#include <stack>
#include <cctype>
#include <utility>   
#include <map>
#include <string>  
#include <climits> 
#include <set>
#include <string>    
#include <sstream>
#include <utility>   
#include <ctime>

using std::priority_queue;
using std::vector;
using std::swap;
using std::stack;
using std::sort;
using std::max;
using std::min;
using std::pair;
using std::map;
using std::string;
using std::cin;
using std::cout;
using std::set;
using std::queue;
using std::string;
using std::istringstream;
using std::make_pair;
using std::getline;
using std::greater;
using std::endl;
using std::multimap;
using std::deque;

typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> PAIR;
typedef multimap<int, int> MMAP;

const int MAXN(10010);
const int MAXM(10010);
const int MAXE(10010);
const int HSIZE(3131);
const int SIGMA_SIZE(26);
const int MAXH(19);
const int INFI((INT_MAX-1) >> 1);
const int MOD(7777777);
const ULL BASE(31);
const LL LIM(10000000);
const int INV(-10000);


void checkmax(int &op1, int op2) {if(op2 > op1) op1 = op2;}

struct HASH_MAP
{
    int first[HSIZE], next[MAXN];
    int value[MAXN];
    int state[MAXN];
    int  size;
    void init()
    {
        memset(first, -1, sizeof(first));
        size = 0;
    }

    void insert(int ts, int tv)
    {
        int h = ts%HSIZE;
        for(int i = first[h]; ~i; i = next[i])
            if(state[i] == ts)
            {
                checkmax(value[i], tv);
                return;
            }
        value[size] = tv;
        state[size] = ts;
        next[size] = first[h];
        first[h] = size++;
    }
}hm[2];

HASH_MAP *cur, *last;

int code[10];
int Num[9];
int N, M;
int score[10][10];


void decode(int ts)
{
    for(int i = 0; i <= M; ++i)
    {
        code[i] = ts&7;
        ts >>= 3;
    }
}

int encode()
{
    int ret = 0;
    int cnt = 0;
    memset(Num, -1, sizeof(Num));
    for(int i = M; i >= 0; --i)
        if(code[i] == 0)
            ret <<= 3;
        else
        {
            if(Num[code[i]] < 0) Num[code[i]] = ++cnt;
            ret = (ret << 3)|Num[code[i]];
        }
    return ret;
}

inline bool firstgrid(int x, int y) {return x == 0 && y == 0;}
inline bool lastgrid(int x, int y) {return x == N-1 && y == M-1;}

void updata(int x, int y, int tv)
{
    int up = (x == 0)? 0: code[y+1];
    int left = (y == 0)? 0: code[y];
    if(up == 0 && left == 0)
    {
        if(firstgrid(x, y))
        {
            code[y] = 7;
            code[y+1] = 0;
            cur->insert(encode(), tv+score[x][y]);
            if(y == M-1) return;
            code[y] = 0;
            code[y+1] = 7;
            cur->insert(encode(), tv+score[x][y]);
            return;
        }
        if(lastgrid(x, y)) return;
        code[y] = code[y+1] = 0;
        cur->insert(encode(), tv);
        if(y == M-1) return;
        code[y] = code[y+1] = 7;
        cur->insert(encode(), tv+score[x][y]);
    }
    else
        if(up == 0 || left == 0)
        {
            if(lastgrid(x, y))
            {
                for(int i = 0; i < y; ++i)
                    if(code[i] > 0)
                        return;
                code[y] = code[y+1] = 0;
                cur->insert(encode(), tv+score[x][y]);
                return;
            }
            code[y] = up+left;
            code[y+1] = 0;
            cur->insert(encode(), tv+score[x][y]);
            if(y == M-1) return;
            code[y] = 0;
            code[y+1] = up+left;
            cur->insert(encode(), tv+score[x][y]);
        }
        else
            if(up != left)
            {
                if(lastgrid(x, y)) return;
                for(int i = 0; i <= M; ++i)
                    if(code[i] == up) code[i] = left;
                code[y] = code[y+1] = 0;
                cur->insert(encode(), tv+score[x][y]);
            }
}

void solve()
{
    cur = hm;
    last = hm+1;
    last->init();
    last->insert(0, 0);
    for(int i = 0; i < N; ++i)
        for(int j = 0; j < M; ++j)
        {
            cur->init();
            int sz = last->size;
            for(int k = 0; k < sz; ++k)
            {
                decode(last->state[k]);
                if(j == 0)
                    for(int k2 = M; k2 >= 1; --k2)
                        code[k2] = code[k2-1];
                updata(i, j, last->value[k]);
            }
            swap(cur, last);
        }
    printf("%d\n", last->value[0]);
}

int main()
{
    int n_case(0);
    while(~scanf("%d%d", &N, &M))
    {
        for(int i = 0; i < N; ++i)
            for(int j = 0; j < M; ++j)
                scanf("%d", score[i]+j);
        printf("Case %d: ", ++n_case);
        if(N == 1 && M == 1)
        {
            printf("%d\n", score[0][0]);
            continue;
        }
        solve();
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值