2016-2017 ACM-ICPC Pacific Northwest Regional Contest (Div. 1) Problem B — Buggy Robot

题面:https://codeforces.com/gym/101201/attachments/download/5206/fast-en.pdf

Solution:BFS维护DP

dp[i][j][k]表示走完前k个字符串后,到达点(i, j)的最小改变次数

数据范围很小,考虑直接bfs的时候把所有可行状态都压缩进去,然后dp更新

然后一直跑到所有状态都最新就行了

 

接下来考虑一个状态可以更新到那些状态

对于node(x, y, pos),表示点(x, y)走到了s[pos]

然后对于一个状态,考虑三种转移:

1,增加一个,枚举增加的方向,转移过去

2,删除一个,删除s[pos+1]的条件是dp[x][y][pos] + 1 < dp[x][y][pos+1]

3,不变,能转移的条件是dp[x][y][pos] < dp[next_x][next_y][pos+1]

然后瞎写就行了

 

AC Code:

#include<set>
#include<map>
#include<cmath>
#include<ctime>
#include<queue>
#include<stack>
#include<cstdio>
#include<string>
#include<vector>
#include<cstdlib>
#include<cstring>
#include<iomanip>
#include<iostream>
#include<algorithm>
#define endl "\n"
#define fi first
#define se second
#define db double
#define gcd __gcd
#define pb push_back
#define mp make_pair
#define lowbit(x) (x & (-x))
#define PII  pair<int, int> 
#define all(x) x.begin(), x.end()
#define debug(x) cout << #x << " = " << x << endl
#define rep(i, a, b) for(int i = a; i <= (b); i++)
#define Rep(i, a, b) for(int i = a; i >= (b); i--)
#define FAST ios::sync_with_stdio(false); cin.tie(0); cout.tie(0)
template<class T> inline T qmin(T a, T b) { return a < b ? a : b; }
template<class T> inline T qmax(T a, T b) { return a > b ? a : b; }
typedef long long ll;
typedef unsigned long long ull;
const db eps = 1e-9;
const db PI = acos(-1);
const int inf = 0x3f3f3f3f;
const int mod = (int)1e9 + 7;
const int maxn = (int)55;
const ll INF = 0x3f3f3f3f3f3f3f3f;
using namespace std;

struct node{
	int x, y, pos;
	node(){}
	node(int x, int y, int pos) { this->x = x; this->y = y; this->pos = pos; }
}S, T;

char s[maxn];
char G[maxn][maxn];
int dp[maxn][maxn][maxn];
int n, m, len;

queue<node> q;
const int dir[4][2] = { {1, 0}, {-1, 0}, {0, 1}, {0, -1} };

bool check(int x, int y){
	return (x >= 1 && x <= n && y >= 1 && y <= m && G[x][y] != '#');
}

void solve(){
	memset(dp, 0x3f, sizeof dp);
	dp[S.x][S.y][0] = 0;
	q.push(node(S.x, S.y, 0));
	while(!q.empty()){
		node now = q.front(); q.pop();
		int x = now.x, y = now.y, pos = now.pos;
		int tx, ty, tpos;
		rep(i, 0, 3){
			tx = x + dir[i][0], ty = y + dir[i][1];
			if(check(tx, ty)){
				if(dp[x][y][pos] + 1 < dp[tx][ty][pos]){
					dp[tx][ty][pos] = dp[x][y][pos] + 1;
					q.push(node(tx, ty, pos));
				}
			}
		}
		if(pos < len){
			if(dp[x][y][pos] + 1 < dp[x][y][pos+1]){
				dp[x][y][pos+1] = dp[x][y][pos] + 1;
				q.push(node(x, y, pos + 1));
			}
			tx = x, ty = y;
			if(s[pos+1] == 'U')	tx--;
			else if(s[pos+1] == 'D') tx++;
			else if(s[pos+1] == 'L') ty--;
			else ty++;
			if(!check(tx, ty)) tx = x, ty = y;
			if(dp[x][y][pos] < dp[tx][ty][pos+1]){
				dp[tx][ty][pos+1] = dp[x][y][pos];
				q.push(node(tx, ty, pos + 1));
			}
		}
	}
}

int main()
{
	scanf("%d %d", &n, &m);
	rep(i, 1, n) rep(j, 1, m) {
		scanf(" %c", &G[i][j]);
		if(G[i][j] == 'R') S.x = i, S.y = j;
		else if(G[i][j] == 'E') T.x = i, T.y = j;
	}
	scanf(" %s", s + 1);
	len = strlen(s + 1);
	solve();
	int ans = inf;
	rep(i, 0, len) ans = qmin(ans, dp[T.x][T.y][i]);
	printf("%d\n", ans);
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值