UVa 314 - Robot

題目:一個機器人在一個矩形的地圖上行走,地圖上有些方形的障礙(1m*1m),

            由於機器人有寬度,不能碰到障礙,每次機器人執行一條指令耗費1s時間,

            指令包括向前走(1或2或3m),向左轉,向右轉,給定其實位置和方向,

            以及目標的位置(方向任意),求最少的移動時間。

分析:圖論、搜索。最短時間,使用bfs求解到達每個點的最短時間即可。

            每個位置有四個狀態(對應四個方向),利用bfs求解;

            題目中數據給的是地圖中的方塊的數據,我們需要將他轉化成機器人走的點,

            即方塊的四個頂點,這裡相鄰的方塊的頂點公用構成(n+1)*(m+1)矩形,

            行編號從0到n,列編號從0到m;

說明:注意機器人有寬度不能走邊界上的路徑。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int block_maps[55][55];
int point_maps[55][55];
int moves_maps[55][55][4];
int dxy[4][2] = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}};

int get_direct(char str[])
{
	if (!strcmp(str, "north")) {
		return 0;
	}else if (!strcmp(str, "east")) {
		return 1;
	}else if (!strcmp(str, "south")) {
		return 2;
	}else if (!strcmp(str, "west")) {
		return 3;
	}else return -1;
}

int queue_x[11111];
int queue_y[11111];
int queue_d[11111];
int bfs(int N, int M, int Sx, int Sy, int Sd, int Ex, int Ey)
{
	memset(moves_maps, 0, sizeof(moves_maps));
	moves_maps[Sx][Sy][Sd] = 1;
	queue_x[0] = Sx;
	queue_y[0] = Sy;
	queue_d[0] = Sd;
	int head = 0, tail = 1;
	while (head < tail) {
		int x = queue_x[head];
		int y = queue_y[head];
		int d = queue_d[head];
		if (x == Ex && y == Ey) {
			return moves_maps[x][y][d]-1;
		}
		// move 
		for (int s = 1; s <= 3; ++ s) {
			int new_x = x + dxy[d][0]*s;
			int new_y = y + dxy[d][1]*s;
			if (new_x > 0 && new_x < N && new_y > 0 && new_y < M) {
				int flag = 0;
				for (int t = 1; t <= s; ++ t) {
					if (point_maps[x + dxy[d][0]*t][y + dxy[d][1]*t]) {
						flag = 1;
					}
				}
				if (!moves_maps[new_x][new_y][d] && !flag) {
					moves_maps[new_x][new_y][d] = moves_maps[x][y][d]+1;
					queue_x[tail] = new_x;
					queue_y[tail] = new_y;
					queue_d[tail] = d;			
					tail ++;
				}
			}
		}
		// turn left
		if (!moves_maps[x][y][(d+3)%4]) {
			moves_maps[x][y][(d+3)%4] = moves_maps[x][y][d]+1;
			queue_x[tail] = x;
			queue_y[tail] = y;
			queue_d[tail] = (d+3)%4;
			tail ++;
		}
		// turn right
		if (!moves_maps[x][y][(d+1)%4]) {
			moves_maps[x][y][(d+1)%4] = moves_maps[x][y][d]+1;
			queue_x[tail] = x;
			queue_y[tail] = y;
			queue_d[tail] = (d+1)%4;
			tail ++;
		}
		head ++;
	}
	return -1;
}

int main()
{
	int  N, M, Br, Bc, Er, Ec;
	char direct[10];
	while (~scanf("%d%d",&N,&M) && N+M) {
		for (int i = 0; i < N; ++ i) {
			for (int j = 0; j < M; ++ j) {
				scanf("%d",&block_maps[i][j]);
			}
		}
		scanf("%d%d%d%d%s",&Br,&Bc,&Er,&Ec,direct);
		
		memset(point_maps, 0 , sizeof(point_maps));
		for (int i = 0; i < N; ++ i) {
			for (int j = 0; j < M; ++ j) {
				if (block_maps[i][j]) {
					point_maps[i+0][j+0] = 1;
					point_maps[i+0][j+1] = 1;
					point_maps[i+1][j+0] = 1;
					point_maps[i+1][j+1] = 1;
				}
			}
		}

		printf("%d\n",bfs(N, M, Br, Bc, get_direct(direct), Er, Ec));
	}
	
	return 0;
}



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值