POJ - 1376 Robot

题意:求在可以一秒沿着既定方向走1到3步和向左或右转90度的情况下,从起点到终点的最短时间

思路:坑的是这机器人还有体积,所以不能走到边界,然后就是单纯的BFS

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
const int MAXN = 110;

struct node {
	int x,y;
	int time,pos;
};
int n,m,sx,sy,sdir,ex,ey,map[MAXN][MAXN];
int vis[MAXN][MAXN][4];
int dx[4] = {-1, 0, 1, 0};
int dy[4] = {0, 1, 0, -1}; 

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

int check(int x, int y) {
	if (x < 1 || x >=n || y < 1 || y >= m)
		return 0;
	if (map[x][y] || map[x+1][y] || map[x][y+1] || map[x+1][y+1])
		return 0;
	return 1;
}

int bfs() {
	queue<node> q;
	node a;
	a.x = sx, a.y = sy, a.pos = sdir, a.time = 0;
	q.push(a);
	vis[sx][sy][sdir] = 1;
	while (!q.empty()) {
		node cur = q.front();
		q.pop();
		if (cur.x == ex && cur.y == ey) 
			return cur.time;
		int nx = cur.x;
		int ny = cur.y;
		for (int i = 1; i < 4; i++) {
			nx += dx[cur.pos];
			ny += dy[cur.pos];
			if (!check(nx, ny))
				break;
			if (!vis[nx][ny][cur.pos]) {
				vis[nx][ny][cur.pos] = 1;
				node cnt;
				cnt.x = nx, cnt.y = ny;
				cnt.pos = cur.pos, cnt.time = cur.time+1;
				q.push(cnt);
			}
		}
		for (int i = 0; i < 4; i++) {
			if (max(cur.pos, i)-min(cur.pos, i) == 2)
				continue;
			if (vis[cur.x][cur.y][i])
				continue;
			vis[cur.x][cur.y][i] = 1;
			node cnt = cur;
			cnt.pos = i;
			cnt.time = cur.time+1;
			q.push(cnt);
		}
	}
	return -1;
}

int main() {
	while (scanf("%d%d", &n, &m) != EOF && n+m) {
		for (int i = 1; i <= n; i++)
			for (int j = 1; j <= m; j++)
				scanf("%d", &map[i][j]);
		memset(vis, 0, sizeof(vis));
		scanf("%d%d%d%d", &sx, &sy, &ex, &ey);
		char dir[20];
		scanf("%s", dir);
		sdir = getDir(dir);
		if (sx == ex && sy == ey) {
			printf("0\n");
			continue;
		}
		if (!check(ex, ey)) {
			printf("-1\n");
			continue;
		}
		int ans = bfs();
		if (ans != -1)
			printf("%d\n", ans);
		else printf("-1\n");
	}
	return 0;
}



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值