CodeForces 589J Cleaner Robot BFS

跟普通迷宫不同的是,它这个,每个点有四个方向,所以每个方向也对应一个状态,然后按要求走下去就行

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <queue>
#include <cstdlib>
#include <cstring>
#include <vector>
#include <set>
#include <map>
#include <string>
using namespace std;
#define ll long long
#define maxn 4005
const int dx[4] = { -1, 0, 1, 0 };
const int dy[4] = { 0, 1, 0, -1 };
char grid[12][12];
bool vis[12][12][4];
bool walk[12][12];
int N, M;
int main()
{
	//freopen("input.txt", "r", stdin);
	//freopen("output.txt", "w", stdout);
	scanf("%d%d", &N, &M);
	int dir = -1, ax = -1, ay = -1;
	for (int i = 0; i < N; ++i)
	{
		scanf("%s", grid[i]);
		for (int j = 0; j < M; ++j)
		{
			if (grid[i][j] != '*'&&grid[i][j] != '.')
			{
				if (grid[i][j] == 'U')
				{
					dir = 0;
				}
				else if (grid[i][j] == 'R')
				{
					dir = 1;
				}
				else if (grid[i][j] == 'D')
				{
					dir = 2;
				}
				else
				{
					dir = 3;
				}
				ax = i; ay = j; grid[i][j] = '.';
			}
		}
	}
	int ans = 0;
	int tx, ty, tdir;
	int cnt;
	bool cannot = false;
	while (1)
	{
		if (walk[ax][ay] == false)
		{
			++ans;
			walk[ax][ay] = true;
		}
		//printf("%d %d %d\n", ax, ay, dir);
		//vis[ax][ay][dir] = true;
		cnt = 0;
		tdir = dir;
		while (1)
		{
			tx = ax + dx[tdir]; ty = ay + dy[tdir];
			if (tx >= 0 && tx < N&&ty >= 0 && ty < M&&grid[tx][ty] != '*')
			{
				break;
			}
			vis[ax][ay][tdir] = true;
			cnt++;
			if (cnt >= 4)
			{
				cannot = true;
				break;
			}
			tdir++;
			if (tdir >= 4)
				tdir -= 4;
		}
		if (cannot || vis[tx][ty][tdir])
			break;
		ax = tx; ay = ty; dir = tdir;
	}
	printf("%d\n", ans);
	//system("pause");
	//while (1);
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值