ICPC Pacific Northwest Regional Contest 2016 - Buggy Robot(bfs+dp)

You are trying to program a robot to navigate through a 2-dimensional maze and find the exit. 

The maze can be represented as a grid with n rows and m columns. Some grid cells have obstaclesthat the robot cannot pass. The other cells are empty, which the robot can freely pass. Exactly oneof the empty cells in the grid is marked as the exit, and the robot will exit the maze immediatelyonce it reaches there.

You can program the robot by sending it a command string. A command string consists of characters ‘L’, ‘U’, ‘R’, ‘D’, corresponding to the directions left, up, right, down, respectively. The robotwill then start executing the commands, by moving to an adjacent cell in the directions specifiedby the command string. If the robot would run into an obstacle or off the edge of the grid, it willignore the command, but it will continue on to remaining commands. The robot will also ignoreall commands after reaching the exit cell. 

Your friend sent you a draft of a command string, but you quickly realize that the command stringwill not necessarily take the robot to the exit. You would like to fix the string so that the robotwill reach the exit square. In one second, you can delete an arbitrary character, or add an arbitrarycharacter at an arbitrary position. Find how quickly you can fix your friend’s command string. 

You do not care how long it takes the robot to find the exit, but only how long it takes to repairthe command string.

Input

The first line of input contains the two integers n and m (1 ≤ n, m ≤ 50).Each of the next n lines contains m characters, describing the corresponding row of the grid. Emptycells are denoted as ‘.’, the robot’s initial position is denoted as ‘R’, obstacles are denoted as ‘#’,and the exit is denoted as ‘E’.The next and final line of input contains your friend’s command string, consisting of between 1 and50 characters, inclusive.It is guaranteed that the grid contains exactly one ‘R’ and one ‘E’, and that there is always a pathfrom ‘R’ to ‘E’.

Output

Print, on a single line, a single integer indicating the minimum amount of time to fix the program. 

樣例輸入1複製

3 3
R..
.#.
..E
LRDD

樣例輸出1複製

1

樣例輸入2複製

2 4
R.#.
#..E
RRUUDDRRUUUU

樣例輸出2複製

0

題目鏈接:點擊查看

題目大意:給出一個 n * m 的迷宮,裏面有一個起點和一個終點,再給出一條方向指令,機器人會按照指令行走一次,問最少添加或者刪除多少個指令字符,可以使得機器人到達終點

題目分析:首先需要知道的是,添加或者刪除指令,這兩個操作其實是等價的,所以我們只需要考慮添加指令的情況即可,因爲刪除一個指令,相當於添加這個指令的反向指令

因爲是需要求一個最優解,可以考慮 dp ,dp[ x ][ y ][ step ] 表示機器人走到了點 ( x , y ) 到了第 step 條指令時,所需要添加指令的最小個數,轉移的話可以類似於 spfa 的思想,對於任意一個狀態 ( x , y , step ) ,考慮添加指令的操作,可以向四周擴散一個單位,並且轉移最優狀態,再考慮按照指令前進一步,那麼就是無代價花費,直接轉移即可

代碼:
 

#include<iostream>
#include<cstdio>
#include<string>
#include<ctime>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<stack>
#include<climits>
#include<queue>
#include<map>
#include<set>
#include<sstream>
#include<cassert>
using namespace std;

typedef long long LL;

typedef unsigned long long ull;

const int inf=0x3f3f3f3f;

const int N=60;

const int b[4][2]={0,1,0,-1,1,0,-1,0};

int dp[N][N][N],n,m;

char maze[N][N],s[N],mp[150];

struct Node
{
	int x,y,step;
	Node(int x,int y,int step):x(x),y(y),step(step){}
};

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

int bfs()
{
	int len=strlen(s+1);
	int ex,ey,sx,sy;
	memset(dp,inf,sizeof(dp));
	for(int i=1;i<=n;i++)
		for(int j=1;j<=m;j++)
			if(maze[i][j]=='R')
				sx=i,sy=j;
			else if(maze[i][j]=='E')
				ex=i,ey=j;
	queue<Node>q;
	q.push(Node(sx,sy,0));
	dp[sx][sy][0]=0;
	while(q.size())
	{
		Node cur=q.front();
		int x=cur.x,y=cur.y,step=cur.step;
		q.pop();
		for(int i=0;i<4;i++)
		{
			int xx=x+b[i][0];
			int yy=y+b[i][1];
			if(!check(xx,yy))
				continue;
			if(dp[xx][yy][step]>dp[x][y][step]+1)
			{
				dp[xx][yy][step]=dp[x][y][step]+1;
				q.push(Node(xx,yy,step));
			}
		}
		if(step<len)
		{
			int pos=mp[s[step+1]];
			int xx=x+b[pos][0];
			int yy=y+b[pos][1];
			if(!check(xx,yy))
				xx=x,yy=y;
			if(dp[xx][yy][step+1]>dp[x][y][step])
			{
				dp[xx][yy][step+1]=dp[x][y][step];
				q.push(Node(xx,yy,step+1));
			}
		}
	}
	int ans=inf;
	for(int i=0;i<=len;i++)
		ans=min(ans,dp[ex][ey][i]);
	return ans;
}

int main()
{
#ifndef ONLINE_JUDGE
//  freopen("input.txt","r",stdin);
//  freopen("output.txt","w",stdout);
#endif
//  ios::sync_with_stdio(false);
	mp['R']=0,mp['L']=1,mp['D']=2,mp['U']=3;
	scanf("%d%d",&n,&m);
	for(int i=1;i<=n;i++)
		scanf("%s",maze[i]+1);
	scanf("%s",s+1);
	printf("%d\n",bfs());


















    return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章