有障碍方格总全职最大的一条简单路径问题,加一个状态位表示是否已经形成了一条简单路径,还要注意转移的条件。
#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(20010);
const int MAXM(5010);
const int MAXE(10010);
const int HSIZE(13131);
const int SIGMA_SIZE(26);
const int MAXH(19);
const int INFI((INT_MAX-1) >> 1);
const int MOD(123456791);
const ULL BASE(31);
const LL LIM(10000000);
const int INV(-10000);
void check_max(int &a, int b) {if(b > a) a = b;}
struct HASH_MAP
{
int first[HSIZE], next[MAXN];
int state[MAXN];
int value[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)
{
check_max(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], Num[8];
int N, M;
int mp[10][10];
int forbid;
void decode(int ts)
{
for(int i = 0; i <= M; ++i)
{
code[i] = ts&7;
ts >>= 3;
}
forbid = ts;
}
int encode()
{
int ret = forbid, cnt = 0;
memset(Num, -1, sizeof(Num));
Num[1] = ++cnt;
for(int i = M; i >= 0; --i)
if(code[i] == 0)
ret <<= 3;
else
{
if(Num[code[i]] == -1) Num[code[i]] = ++cnt;
ret = (ret << 3)|Num[code[i]];
}
return ret;
}
void updata(int x, int y, int tv)
{
int up = (x == 0)? 0: code[y+1];
int left = (y == 0)? 0: code[y];
if(mp[x][y] == 0)
{
if(up == 0 && left == 0)
{
code[y] = code[y+1] = 0;
cur->insert(encode(), tv);
}
return;
}
if(up == 0 && left == 0)
{
code[y] = code[y+1] = 0;
cur->insert(encode(), tv);
if(forbid) return;
if(x < N-1)
{
code[y] = 1;
code[y+1] = 0;
cur->insert(encode(), tv+mp[x][y]);
}
if(y < M-1)
{
code[y] = 0;
code[y+1] = 1;
cur->insert(encode(), tv+mp[x][y]);
}
if(x < N-1 && y < M-1)
{
code[y] = code[y+1] = 7;
cur->insert(encode(), tv+mp[x][y]);
}
code[y] = code[y+1] = 0;
forbid = 1;
cur->insert(encode(), tv+mp[x][y]);
}
else
if(up == 0 || left == 0)
{
if(forbid) return;
if(x < N-1)
{
code[y] = up+left;
code[y+1] = 0;
cur->insert(encode(), tv+mp[x][y]);
}
if(y < M-1)
{
code[y] = 0;
code[y+1] = up+left;
cur->insert(encode(), tv+mp[x][y]);
}
if(up == 1 || left == 1)
{
code[y] = code[y+1] = 0;
forbid = 1;
cur->insert(encode(), tv+mp[x][y]);
}
else
{
int temp = up+left;
for(int i = 0; i <= M; ++i)
if(code[i] == temp)
code[i] = 1;
code[y] = code[y+1] = 0;
cur->insert(encode(), tv+mp[x][y]);
}
}
else
if(up != left)
{
if(forbid) return;
int t1 = up < left? up: left;
int t2 = t1^up^left;
for(int i = 0; i <= M; ++i)
if(code[i] == t2)
code[i] = t1;
code[y] = code[y+1] = 0;
cur->insert(encode(), tv+mp[x][y]);
}
else
if(up == 1)
{
if(forbid) return;
code[y] = code[y+1] = 0;
forbid = 1;
cur->insert(encode(), tv+mp[x][y]);
}
}
void solve()
{
cur = hm;
last = hm+1;
last->init();
last->insert(0, 0);
int temp = 1 << (3*(M+1));
for(int i = 0; i < N; ++i)
{
for(int k = 0; k < last->size; ++k)
last->state[k] = (last->state[k]&temp)|((last->state[k]&(temp-1)) << 3);
for(int j = 0; j < M; ++j)
{
cur->init();
int sz = last->size;
for(int k = 0; k < sz; ++k)
{
decode(last->state[k]);
updata(i, j, last->value[k]);
}
swap(cur, last);
}
}
int ans = 0;
for(int i = 0; i < last->size; ++i)
if((last->state[i]&temp) != 0 && (last->state[i]&(temp-1)) == 0)
{
ans = last->value[i];
break;
}
printf("%d\n", ans);
}
int main()
{
int TC;
scanf("%d", &TC);
while(TC--)
{
scanf("%d%d", &N, &M);
for(int i = 0; i < N; ++i)
for(int j = 0; j < M; ++j)
scanf("%d", mp[i]+j);
solve();
}
return 0;
}