Tango Tango Insurrection UVA - 10618

题目稍微复杂,要注意记录当前左脚位置、右脚位置、上一次迈了左脚还是右脚、以及当前需要迈哪一只脚以及所迈的目的地,注意不要混淆即可,具体实现见如下代码:

#include<iostream>
#include<vector>
#include<string>
#include<set>
#include<stack>
#include<queue>
#include<map>
#include<algorithm>
#include<cmath>
#include<iomanip>
#include<cstring>
#include<sstream>
#include<cstdio>
#include<deque>
using namespace std;
char foot[] = ".LR";
struct node{
	int m, n, k;
	node(int m1=0,int a = 0, int b = 0){
		m = m1, n = a, k = b;
	}
};

node motion[80][4][4][3];
string s;
int length[80][4][4][3];
map<char,int> direction;

int calEnerge(int source,int aim){
	if (source == aim) return 3;
	if (source + aim == 3) return 7;
	return 5;
}

int cost(int ind,int a,int b,int pre,int f,int aim,int &l,int &r){
	l = a; r = b;
	if (f == 1) l = aim;//左脚
	if (f == 2) r = aim;//右脚
	if (l == r) return -1;
	if (l == b&&r == a) return -1;//
	if (a == direction['R'] && r!=b) return -1;
	if (b == direction['L'] && l!=a) return -1;
	if (f == 0) return 0;
	else if (pre != f) return 1;
	else{
		if (f == 1) return calEnerge(a, l);
		return calEnerge(b, r);
	}
}

void Move(int ind,int a,int b,int pre,int f,int pos){//当前动作
	int l, r;
	int v = cost(ind,a, b, pre, f, pos, l, r);
	if (v < 0) return;
	int& ans = length[ind][a][b][pre];
	if (length[ind + 1][l][r][f] + v < ans){
		ans = length[ind + 1][l][r][f] + v;
		motion[ind][a][b][pre] = node(f,l,r);
	}
}

int main(){
	while (cin >> s){
		memset(length,0,sizeof(length));
		if (s == "#") break;
		direction['U'] = 0, direction['D'] = 3, direction['L'] = 1, direction['R'] = 2;
		for (int i = s.size() - 1; i >= 0; i--){
			for (int a = 0; a < 4; a++){
				for (int b = 0; b < 4; b++){
					if (a != b){
						for (int t = 0; t < 3; t++){//上次使用的是t
							length[i][a][b][t] = 1 << 30;
							if (s[i] == '.'){
								Move(i, a, b, t, 0, 0);
								for (int k = 0; k < 4; k++) {
									Move(i, a, b, t,1, k);
									Move(i, a, b, t, 2, k);
								}
							}
							else{
								Move(i, a, b, t, 1,direction[s[i]]);
								Move(i, a, b, t, 2,direction[s[i]]);
							}
						}
					}
				}
			}
		}
		int a = 1, b = 2, s1 = 0;
		for (int i = 0; i < s.size(); i++){
			int f = motion[i][a][b][s1].m;
			a = motion[i][a][b][s1].n;
			b = motion[i][a][b][s1].k;
			cout << foot[f];
			s1 = f;
		}
		cout << endl;
	}
	return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值