Channel LA4789

78 篇文章 2 订阅
16 篇文章 0 订阅

从左上方到右下方的一条简单路径问题,但是要求路径不能有任何的接触,所以除了要记录插头信息外,还要额外记录(M+1)个格子内是否有路径,状态转移的时候需要仔细分析,否则很容易出错


#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;
using std::random_shuffle;

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

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

template<typename T> void checkmax(T &a, T b){if(b > a) a = b;}

template<typename T> void checkmin(T &a, T b){if(b < a) a = b;}

template<typename T> T ABS(const T &a){return a < 0? -a: a;}

int pre[21][10][MAXN], opt[21][10][MAXN];


struct HASH_MAP
{
	int first[HSIZE], value[MAXN], next[MAXN];
	LL state[MAXN];
	int size;

	void init()
	{
		memset(first, -1, sizeof(first));
		size = 0;
	}

	void insert(LL ts, int tv, int x, int y, int pid, int op)
	{
		int h = ts%HSIZE;
		for(int i = first[h]; ~i; i = next[i])
			if(state[i] == ts)
			{
				if(tv > value[i])
				{
					pre[x][y][i] = pid;
					opt[x][y][i] = op;
					value[i] = tv;
				}
				return ;
			}
		pre[x][y][size] = pid;
		opt[x][y][size] = op;
		state[size] = ts;
		value[size] = tv;
		next[size] = first[h];
		first[h] = size++;
	}
} hm[2];

HASH_MAP *cur, *last;
int N, M;
int code[11], path[11];  //连通块标号, 是否有路径
int Num[8];
int PATH;             //(x-1, y-1)是否有路径
char mp[21][15];


void decode(LL ts)
{
	for(int i = 0; i <= M; ++i)
	{
		code[i] = ts&7;
		path[i] = ts&8;
		ts >>= 4;
	}
	PATH = ts&8;
}

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

void updata(int x, int y, int tv, int pid)
{
	int lc = (y == 0)? 0: code[y];         //左插头标号
	int uc = (x == 0)? 0: code[y+1];	   //上插头标号
	int lp = (y == 0)? 0: path[y-1];	   //(x, y-1)是否有路径	
	int lup = (x == 0 || y == 0)? 0: PATH;  //(x-1, y-1)是否有路径	
	int up = (x == 0)? 0: path[y+1];		//(x-1, y)是否有路径	
	int urp = (x == 0 || y == M-1)? 0: path[y+2];	//(x-1, y+1)是否有路径
	PATH = path[y+1];
	if(x == 0 && y == 0)             //起点
	{
		path[y] = 8;
		code[y] = 7;
		code[y+1] = 0;
		cur->insert(encode(), tv+1, x, y, pid, 1);
		path[y] = 8;
		code[y] = 0;
		code[y+1] = 7;
		cur->insert(encode(), tv+1, x, y, pid, 1);
		return;
	}
	if(x == N-1 && y == M-1)   //终点
	{
		if(lc == 0 || uc == 0)
		{
			if(lc == 0)
			{
				if(lp) return;
				path[y] = 8;
				code[y] = code[y+1] = 0;
				cur->insert(encode(), tv+1, x, y, pid, 1);
			}
			else
			{
				if(up) return;
				path[y] = 8;
				code[y] = code[y+1] = 0;
				cur->insert(encode(), tv+1, x, y, pid, 1);
			}
		}
		return;
	}
	if(mp[x][y] == '#')     //障碍
	{
		if(lc == 0 && uc == 0)
		{
			path[y] = 0;
			code[y] = code[y+1] = 0;
			cur->insert(encode(), tv, x, y, pid, 0);
		}
		return;
	}
	if(lc == 0 && uc == 0)                       
	{
		path[y] = 0;
		code[y] = code[y+1] = 0;
		cur->insert(encode(), tv, x, y, pid, 0);
		if(x == N-1 || y == M-1) return;
		if(lp || lup || up) return;       //(x, y-1),(x-1, y-1), (x-1, y)任意一个有路径均不合法
		path[y] = 8;
		code[y] = code[y+1] = 7;
		cur->insert(encode(), tv+1, x, y, pid, 1);
	}
	else
		if(lc == 0 || uc == 0)
		{
			if(lc)
			{
				if(up) return;         //(x, y-1)有路径不合法
				if(y < M-1)
				{
					path[y] = 8;
					code[y] = 0;
					code[y+1] = lc;
					cur->insert(encode(), tv+1, x, y, pid, 1);
				}
				if(x < N-1) 
				{
					if(urp) return;   //(x, y+1)有路径不合法
					path[y] = 8;
					code[y] = lc;
					code[y+1] = 0;
					cur->insert(encode(), tv+1, x, y, pid, 1);
				}
			}
			else
			{
				if(lp) return;     //(x, y-1)有路径不合法
				if(y < M-1)
				{
					path[y] = 8;
					code[y] = 0;
					code[y+1] = uc;
					cur->insert(encode(), tv+1, x, y, pid, 1);
				}
				if(x < N-1)
				{
					path[y] = 8;
					code[y] = uc;
					code[y+1] = 0;
					cur->insert(encode(), tv+1, x, y, pid, 1);
				}
			}
		}
		else
			if(lc != uc)
			{
				path[y] = 8;
				for(int i = 0; i <= M; ++i)
					if(code[i] == uc) code[i] = lc;
				code[y] = code[y+1] = 0;
				cur->insert(encode(), tv+1, x, y, pid, 1);
			}
}

void solve()
{
	cur = hm;
	last = hm+1;
	last->init();
	last->insert(0, 0, 0, 0, 0, 0);
	int t1 = (M+1)*4, t2 = M*4;
	for(int i = 0; i < N; ++i)
	{
		int sz = last->size;
		for(int j = 0; j < sz; ++j)
			last->state[j] = (last->state[j]&(8LL << t1))|((last->state[j]&((1LL << t2)-1)) << 4);
		for(int j = 0; j < M; ++j)
		{
			cur->init();
			sz = last->size;
			for(int k = 0; k < sz; ++k)
			{
				decode(last->state[k]);
				updata(i, j, last->value[k], k);
			}
			swap(cur, last);
		}
	}
	int ans = 0, id;
	for(int i = 0; i < last->size; ++i)
	{
		decode(last->state[i]);
		bool flag = true;
		for(int j = 0; j <= M; ++j)
			if(code[j])
			{
				flag = false;
				break;
			}
		if(flag)
		{
			if(last->value[i] > ans)
			{
				ans = last->value[i];
				id = i;
			}
		}
	}
	for(int i = N-1; i >= 0; --i)
		for(int j = M-1; j >= 0; --j)
		{
			if(opt[i][j][id]) mp[i][j] = 'C';
			id = pre[i][j][id];
		}
	for(int i = 0; i < N; ++i)
		printf("%s\n", mp[i]);
	printf("\n");
}

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


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值