和Black&white类似,只要再加一维记录分数即可(-64~64)
#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::stringstream;
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(80010);
const int MAXM(5010);
const int MAXE(10010);
const int HSIZE(13131);
const int SIGMA_SIZE(26);
const int MAXH(19);
const int INFI(( -1) >> 1);
const ULL BASE(31);
const LL LIM(10000000);
const int INV(-10000);
int pre[8][8][MAXN], opt[8][8][MAXN];
struct HASH_MAP
{
int first[HSIZE], next[MAXN];
LL state[MAXN];
LL value[MAXN];
int size;
void init()
{
memset(first, -1, sizeof(first));
size = 0;
}
void insert(LL ts, LL tv, int x, int y, int pid, int tc)
{
int h = ts%HSIZE;
for(int i = first[h]; ~i; i = next[i])
if(state[i] == ts)
{
value[i] += tv;
return;
}
pre[x][y][size] = pid;
opt[x][y][size] = tc;
value[size] = tv;
state[size] = ts;
next[size] = first[h];
first[h] = size++;
}
}hm[2];
HASH_MAP *cur, *last;
int Num[8];
int code[8], color[8], COL, VAL; //轮廓线从左到右分量标号,颜色。(x-1, y-1)颜色
int forbid[2]; //某种颜色的分量是否已经结束,0白1黑
int N, M;
char mp[9][9];
int score[8][8];
void decode(LL ts)
{
for(int i = 0; i < M; ++i)
{
code[i] = ts&7;
color[i] = (ts&8) >> 3;
ts >>= 4;
}
forbid[0] = ts&1;
ts >>= 1;
forbid[1] = ts&1;
ts >>= 1;
COL = ts&1;
ts >>= 1;
VAL = (ts&255)-64;
}
LL encode()
{
int cnt = 0;
LL ret = VAL+64;
ret <<= 3;
ret |= (COL << 2)|(forbid[1] << 1)|(forbid[0]);
memset(Num, -1, sizeof(Num));
for(int i = M-1; i >= 0; --i)
{
if(Num[code[i]] == -1) Num[code[i]] = cnt++; //注意此处从0开始标号,因为最多可能有8个分量
ret = (ret << 4)|Num[code[i]]|(color[i] << 3);
}
return ret;
}
void updata(int x, int y, LL tv, int pid, int tc)
{
int left = (y == 0)? -1: color[y-1];
int up = (x == 0)? -1: color[y];
int lu = (x == 0 || y ==0)? -1: COL;
COL = color[y];
VAL += tc? score[x][y]: -score[x][y];
color[y] = tc;
if(left == -1 && up == -1)
code[y] = 7;
else
if(left == -1 || up == -1)
{
if(left == -1)
{
if(up == tc)
code[y] = code[y];
else
{
int cnt = 0;
for(int i = 0; i < M; ++i)
if(code[i] == code[y]) ++cnt;
if(cnt == 1)
{
if(forbid[up]) return;
forbid[up] = 1;
}
code[y] = 7;
}
}
else
{
if(left == tc)
code[y] = code[y-1];
else
code[y] = 7;
}
}
else
if(left == up)
{
if(left == tc)
{
if(lu == tc) return;
int temp = code[y];
for(int i = 0; i < M; ++i)
if(code[i] == temp) code[i] = code[y-1];
code[y] = code[y-1];
}
else
{
int cnt = 0;
for(int i = 0; i < M; ++i)
if(code[i] == code[y]) ++cnt;
if(cnt == 1)
{
if(forbid[up]) return;
forbid[up] = 1;
}
code[y] = 7;
}
}
else
{
if(up == tc)
code[y] = code[y];
else
{
int cnt = 0;
for(int i = 0; i < M; ++i)
if(code[i] == code[y]) ++cnt;
if(cnt == 1)
{
if(forbid[up]) return;
forbid[up] = 1;
}
code[y] = code[y-1];
}
}
cur->insert(encode(), tv, x, y, pid, tc);
}
void solve()
{
cur = hm;
last = hm+1;
last->init();
for(int i = 0; i < M; ++i)
color[i] = code[i] = 0;
COL = 0;
forbid[0] = forbid[1] = 0;
VAL = 0;
last->insert(encode(), 1, 0, 0, 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(mp[i][j] != 'o' && !forbid[1])
{
updata(i, j, last->value[k], k, 1);
decode(last->state[k]);
}
if(mp[i][j] != 'x' && !forbid[0])
updata(i, j, last->value[k], k, 0);
}
swap(cur, last);
}
int mabs = 100;
LL ans = 0;
int id;
bool vis[8];
for(int i = 0; i < last->size; ++i)
{
decode(last->state[i]);
memset(vis, 0, sizeof(vis));
int cnt = 0;
for(int j = 0; j < M; ++j)
if(!vis[code[j]])
{
++cnt;
vis[code[j]] = true;
}
cnt += forbid[0]+forbid[1];
if(cnt > 2) continue;
if(abs(VAL) == mabs)
ans += last->value[i];
else
if(abs(VAL) < mabs)
{
mabs = abs(VAL);
ans = last->value[i];
id = i;
}
}
if(ans)
{
printf(" %d %I64d\n", mabs, ans);
for(int i = N-1; i >= 0; --i)
for(int j = M-1; j >= 0; --j)
{
mp[i][j] = opt[i][j][id]? 'x': 'o';
id = pre[i][j][id];
}
for(int i = 0; i < N; ++i)
printf("%s\n", mp[i]);
}
else
printf(" 0 0\n");
printf("\n");
}
int main()
{
int TC, n_case(0);
scanf("%d", &TC);
while(TC--)
{
scanf("%d%d", &N, &M);
for(int i = 0; i < N; ++i)
scanf("%s", mp[i]);
for(int i = 0; i < N; ++i)
for(int j = 0; j < M; ++j)
scanf("%d", score[i]+j);
printf("Case %d:", ++n_case);
solve();
}
return 0;
}