Suppose there is a thief and n rooms.
We can only any door to check whether the thief is there or not. during the night, the thief can either move left one room or right one room. Given a sequence of open room.
Check whether we can get the thief using the given sequence.
For example: If there are three rooms, given sequence [1, 1], we can definitely find the thief. if thief was in room[1], we can find him right away. But, if he was in room[0]/room[2], we can find him on the second day since he will have to move one room right/left.
1: first thought, backtracking. if there are n rooms, k sequences, the time complexity will be: k*n^k
#include <vector>
#include <iostream>
using namespace std;
void findThief(int rooms, int thiefPos, vector<int>& sequence, int i, bool& flag) {
if(thiefPos < 0 || thiefPos >= rooms || i >= sequence.size()) return;
if(flag == true) return;
if(thiefPos == sequence[i]) {
flag = true;
}
findThief(rooms, thiefPos + 1, sequence, i + 1, flag);
findThief(rooms, thiefPos - 1, sequence, i + 1, flag);
}
bool findThief(int rooms, int thiefPos, vector<int>& sequence) {
bool flag = false;
findThief(rooms, thiefPos, sequence, 0, flag);
if(flag) return true;
return false;
}
int main(void) {
vector<int> sequence {1, 1};
cout << findThief(3, 2, sequence) << endl;
cout << findThief(3, 0, sequence) << endl;
cout << findThief(3, 1, sequence) << endl;
}
Further Optimize....
// using dp to cache the intermediate results.
bool ThiefSuriveII(int n, vector<int>& seq) {
int m = seq.size();
vector< vector<bool> > dp(m, vector<bool>(n, false));
for(int i = 0; i < n; ++i) dp[0][i] = true;
dp[0][seq[0]] = false; // dead in the first day.
for(int i = 1; i < m; ++i) {
for(int j = 0; j < n; ++j) {
bool left = j - 1 >= 0 ? dp[i-1][j-1] : false;
bool right = j + 1 < n ? dp[i-1][j+1] : false;
dp[i][j] = (left || right) && (seq[i] != j);
}
}
for(int i = 0; i < n; ++i) {
if(dp[m-1][i]) return true;
}
return false;
}
Further Optimize....
// further optimize... Since we dont need to preverver the last status.
bool thiefSuriveIII(int n, vector<int>& seq) {
int m = seq.size();
vector<bool> dp(n, true);
dp[seq[0]] = false;
for(int i = 1; i < m; ++i) {
vector<int> nextDp(n, false);
for(int j = 0; j < n; ++j) {
bool left = j - 1 >= 0 ? dp[j-1] : false;
bool right = j + 1 < n ? dp[j+1] : false;
nextDp[j] = (left || right) & (seq[i] != j);
}
dp = nextDp;
}
for(int i = 0; i < n; ++i) {
if(surrive[i]) return true;
}
return false;
}
Talked with huixuan, she promoted to solve it BFS!!! This idea is fantastic.
Think that each day we can only open one door. If the sequence can guarantee to find the thief out, by the end of the sequence, we will have no door left to check. Each day we can cut a branch. Thus, BFS can search the door most efficiently.
#include "header.h"
using namespace std;
bool findThief(int n, vector<int>& sequence) {
// initialize the first day's states.
set<int> prev;
for(int i = 0; i < n; ++i) prev.insert(i);
for(int i = 0; i < sequence.size(); ++i) {
set<int> current;
auto iter = prev.begin();
while(iter != prev.end()) {
int jth = *iter;
if(jth != sequence[i]) {
if(jth > 0) current.insert(jth - 1);
if(jth < n-1) current.insert(jth + 1);
}
iter++;
}
swap(prev, current);
}
return prev.size() == 0;
}
int main(void) {
vector<int> sequence{1, 1};
cout << findThief(3, sequence) << endl;
}

本文探讨了一种通过特定房间序列捕获在夜间只能移动到相邻房间的窃贼的算法。提出了回溯法、动态规划及BFS等不同优化方案,并详细分析了各种方法的时间和空间复杂度。
225

被折叠的 条评论
为什么被折叠?



