UVa Problem 707 Robbery (抢劫)

// Robbery (抢劫)
// PC/UVa IDs: 111205/707, Popularity: B, Success rate: average Level: 3
// Verdict: Accepted
// Submission Date: 2011-11-04
// UVa Run Time: 0.036s
//
// 版权所有(C)2011,邱秋。metaphysis # yeah dot net
//
// [解题方法]
// 提示:如何建图来同时刻画时间和空间?如何高效的遍历以确定可能的位置?如果解决了这两个问题,本问
// 题就解决了!

#include <iostream>

using namespace std;

#define MAXN 100

#define UNOBSERVED 0
#define OBSERVED 1
#define REACHABLE 2
#define I_AM_HERE 3

int width, height, timeLocked;
int grid[MAXN + 1][MAXN + 1][MAXN + 1];
int offset[5][2] = { { 0, 0 }, { -1, 0 }, { 0, -1 }, { 1, 0 }, { 0, 1 } };

// 先从最后时刻往前搜索,找到可以到达的位置,标记。
void backward_dfs(int time, int y, int x)
{
	if (time == 1)
	{
		grid[time][y][x] = REACHABLE;
		return;
	}

	for (int i = 0; i < 5; i++)
	{
		int tmpY = y + offset[i][0];
		int tmpX = x + offset[i][1];

		if (1 <= tmpY && tmpY <= height && 1 <= tmpX && tmpX <= width)
			if (grid[time - 1][tmpY][tmpX] == UNOBSERVED)
			{
				grid[time][y][x] = REACHABLE;
				backward_dfs(time - 1, tmpY, tmpX);
			}
			else if (grid[time - 1][tmpY][tmpX] == REACHABLE)
				grid[time][y][x] = REACHABLE;
	}
}

// 从前往后搜索,将可能的位置标记。
void forward_dfs(int time, int y, int x)
{
	if (time == timeLocked)
	{
		grid[time][y][x] = I_AM_HERE;
		return;
	}

	for (int i = 0; i < 5; i++)
	{
		int tmpY = y + offset[i][0];
		int tmpX = x + offset[i][1];
		
		if (1 <= tmpY && tmpY <= height && 1 <= tmpX && tmpX <= width)
			if (grid[time + 1][tmpY][tmpX] == REACHABLE)
			{
				grid[time][y][x] = I_AM_HERE;
				forward_dfs(time + 1, tmpY, tmpX);
			}
			else if (grid[time + 1][tmpY][tmpX] == I_AM_HERE)
				grid[time][y][x] = I_AM_HERE;
	}
}

int main(int ac, char *av[])
{
	int cases = 1;
	int messages;
	int current, left, top, right, bottom;

	while (cin >> width >> height >> timeLocked, width || height || timeLocked)
	{
		cout << "Robbery #" << cases++ << ":\n";

		for (int t = 1; t <= timeLocked; t++)
			for (int y = 1; y <= height; y++)
				for (int x = 1; x <= width; x++)
					grid[t][y][x] = UNOBSERVED;
		
		// 读取各时间片的封锁范围,劫匪在该时刻不会在此范围内。
		cin >> messages;
		for (int i = 1; i <= messages; i++)
		{
			cin >> current >> left >> top >> right >> bottom;

			for (int y = top; y <= bottom; y++)
				for (int x = left; x <= right; x++)
					grid[current][y][x] = OBSERVED;
		}

		// 从最后一个时间片往前进行深度优先遍历。假设在时刻 t,劫匪在位置(width,
		// height),则在上一时刻 t - 1,劫匪只可能在(width - 1,height),
		// (width + 1,height),(width,height - 1),(width,height + 1)
		// (width,height)五个位置中的一个。注意需要经过正反两个方向的搜索才能确定
		// 通路上的可能位置。
		for (int y = 1; y <= height; y++)
			for (int x = 1; x <= width; x++)
				if (grid[timeLocked][y][x] == UNOBSERVED)
					backward_dfs(timeLocked, y, x);

		// 正向搜索以确定确实可行的位置。
		for (int y = 1; y <= height; y++)
			for (int x = 1; x <= width; x++)
				if (grid[1][y][x] == REACHABLE)
					forward_dfs(1, y, x);

		// 根据各个时间片可能位置的数量决定输出。
		bool nothing = true;
		pair < int, int > location;
		for (int t = 1; t <= timeLocked; t++)
		{
			int exactLocation = 0;
			for (int y = 1; y <= height; y++)
			{
				for (int x = 1; x <= width; x++)
					if (grid[t][y][x] == I_AM_HERE)
					{
						exactLocation++;
						location = make_pair(x, y);
						if (exactLocation > 1)
							break;
					}

				if (exactLocation > 1)
					break;
			}

			if (exactLocation == 0)
			{
				cout << "The robber has escaped.\n";
				nothing = false;
				break;
			}

			if (exactLocation == 1)
			{
				cout << "Time step " << t << ": ";
				cout << "The robber has been at ";
				cout << location.first << "," << location.second;
				cout << ".\n";
				nothing = false;
			}
		}

		if (nothing)
			cout << "Nothing known.\n";

		cout << "\n";
	}

	return 0;
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个基本的代码示例,可以从数据集中提取光流图并应用于i3d网络: ```python import os import cv2 import numpy as np # 定义数据集文件夹和光流图文件夹路径 data_dir = "C:/crime" frames_dir = "C:/crime/crime_frames" # 定义光流图的分辨率和时间跨度 flow_resolution = (224, 224) flow_timestep = 1 # 循环遍历数据集中的每个类别 for class_name in os.listdir(data_dir): # 获取当前类别的文件夹路径 class_dir = os.path.join(data_dir, class_name) # 循环遍历当前类别中的每个视频 for video_name in os.listdir(class_dir): # 获取当前视频的文件夹路径 video_dir = os.path.join(class_dir, video_name) # 获取当前视频的光流图文件夹路径 flow_dir = os.path.join(frames_dir, class_name, video_name) # 如果光流图文件夹不存在,则创建它 if not os.path.exists(flow_dir): os.makedirs(flow_dir) # 读取当前视频中的每一帧 frames = [] for frame_name in os.listdir(video_dir): frame_path = os.path.join(video_dir, frame_name) frame = cv2.imread(frame_path) frames.append(frame) # 将帧转换为灰度图像 gray_frames = [cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) for frame in frames] # 计算光流图 flows = [] for i in range(0, len(gray_frames) - 1, flow_timestep): prev_gray = gray_frames[i] curr_gray = gray_frames[i + flow_timestep] flow = cv2.calcOpticalFlowFarneback(prev_gray, curr_gray, None, 0.5, 3, 15, 3, 5, 1.2, 0) flow = cv2.resize(flow, flow_resolution) flows.append(flow) # 将光流图保存到文件夹中 for i, flow in enumerate(flows): flow_path = os.path.join(flow_dir, "{:05d}.jpg".format(i)) np.save(flow_path, flow) ``` 代码解释: 1. 首先定义了数据集文件夹和光流图文件夹的路径。 2. 然后循环遍历数据集中的每个类别,每个类别都有一个文件夹名字。 3. 对于每个类别,循环遍历其中的每个视频,每个视频也有一个文件夹名字。 4. 对于每个视频,获取它的光流图文件夹路径,如果该文件夹不存在,则创建它。 5. 读取每个视频的所有帧,并将它们转换为灰度图像。 6. 计算光流图,使用cv2.calcOpticalFlowFarneback函数计算两帧之间的光流。 7. 将光流图保存到光流图文件夹中,以.npy格式保存。 最后,我们可以将生成的光流图应用于i3d网络进行分类任务。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值