CodeForces Gym 100646G The Worm Turns DFS

本文介绍了一个问题:在一个由食物和石头组成的矩阵中,找到一条能够收集最多食物的路径。通过深度优先搜索(DFS)策略,从每个可能的起点出发,探索所有可能的方向组合,最终确定最优解。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

就是有一个矩阵,每个点要么有食物,要么是石头 ,然后有个人会从某一个点沿着某个方向吃食物,当下个地方不能走时它会向左或者向右转,他也不会再走已经走过的点,然后问他从哪里开始吃能吃到最多食物

就暴力枚举每个点以及四个方向,但是因为下个地方不能走时他既能向左又能向右,所以两个方向都有走,所以要DFS去搜所有情况,然后每个点每个方向都DFS一次就行

#include <cstdio>
#include <cstring>
#include <vector>
#include <iostream>
#include <string>
#include <cmath>
#include <algorithm>
#include <set>
#include <map>
#include <queue>
using namespace std;
#define ll long long
#define mod 1000000009
#define maxn 105
#define maxm 10003
#define INF 0x3f3f3f3f
const int dx[4] = { 0, -1, 1, 0 };
const int dy[4] = { 1, 0, 0, -1 };
int N, M;
char grid[630];
bool vis[630];
char d[4] = { 'E', 'N', 'S', 'W' };
struct Node
{
	int x, y, dir, cnt;
	Node()
	{
		x = 0; y = 0; dir = 0; cnt = 0;
	}
	Node(int x, int y,int dir, int cnt)
	{
		this->x = x; this->y = y; this->dir = dir; this->cnt = cnt;
	}
};
queue<Node> Q;
int ans, ansdir, ansx, ansy;
int xi, yi, di;
void DFS(int ax,int ay,int dir, int step)
{
	//printf("%d %d\t", ax, ay);
	vis[ax*M + ay] = true;
	int xx, yy;
	xx = ax + dx[dir]; yy = ay + dy[dir];
	if (xx >= 0 && xx < N&&yy >= 0 && yy < M&&grid[xx*M + yy] != 'X'&&!vis[xx*M + yy])
	{
		DFS(xx, yy, dir, step + 1);
	}
	else
	{
		bool expand = false;
		int cnt = 1, d = dir + 1;
		if (d >= 4)
			d -= 4;
		while (1)
		{
			if (cnt >= 4)
				break;
			xx = ax + dx[d]; yy = ay + dy[d];
			if (xx >= 0 && xx < N&&yy >= 0 && yy < M&&grid[xx*M + yy] != 'X'&&!vis[xx*M + yy])
			{
				expand = true;
				DFS(xx, yy, d, step + 1);
			}
			++cnt; ++d;
			if (d >= 4)
				d -= 4;
		}
		if (expand == false)
		{
			if (step > ans)
			{
				//printf("\nxi %d yi %d dir %d step %d xx %d yy %d\n", xi, yi, di, step, ax, ay);
				ans = step; ansx = xi; ansy = yi; ansdir = di;
			}
		}
	}
	vis[ax*M + ay] = false;
}
int main()
{
	//freopen("input.txt", "r", stdin);
	int kase = 1;
	while (scanf("%d%d",&N,&M)!=EOF)
	{
		if (N == 0 && M == 0)
			break;
		memset(grid, 0, sizeof(char) * 630);
		int r;
		scanf("%d", &r);
		int xx, yy;
		for (int i = 0; i < r; ++i)
		{
			scanf("%d%d", &xx, &yy);
			grid[xx*M + yy] = 'X';
		}
		
		Node t;
		ans = -1; ansdir = 0; ansx = 0; ansy = 0;
		for (xi = 0; xi < N; ++xi)
		{
			for (yi = 0; yi < M; ++yi)
			{
				if (grid[xi*M + yi] == 'X')
					continue;
				for (di = 0; di < 4; ++di)
				{
					//printf("%d %d %d\n", i, j, k);
					xx = xi + dx[di]; yy = yi + dy[di];
					if (xx < 0 || xx >= N || yy < 0 || yy >= M || grid[xx*M + yy] == 'X')
						continue;
					memset(vis, 0, sizeof(vis));
					DFS(xi, yi, di, 1);
				}
			}
		}
		printf("Case %d: %d %d %d %c\n", kase, ans, ansx, ansy, d[ansdir]);
		++kase;
	}
	//system("pause");
	//while (1);
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值