/**
  * @param root: The root of binary tree.
  * @return: The maximum amount of money you can rob tonight
  */
 ii rob(TreeNode* root) {
     if (!root) return ii(0, 0);
     
     ii left = rob(root->left);
     ii right = rob(root->right);
     
     return ii(root->val + left.second + right.second, max(left.first, left.second) + max(right.first, right.second));
 }
Example #2
0
 int rob(vector<int>& nums) {
     const int n=nums.size();
     if(n==0) return 0;
     else if(n==1) return nums[0];
     else if(n==2) return max(nums[0],nums[1]);
     
     return max(rob(nums,0,n-2),rob(nums,1,n-1));
 }
Example #3
0
 void rob(TreeNode* root, int &robMax, int &noRobMax) {
     robMax = noRobMax = 0;
     if (root == NULL)
         return;
     int robMaxLeft, noRobMaxLeft, robMaxRight, noRobMaxRight;
     rob(root->left, robMaxLeft, noRobMaxLeft);
     rob(root->right, robMaxRight, noRobMaxRight);
 
     robMax = root->val + noRobMaxLeft + noRobMaxRight;
     noRobMax = max(robMaxLeft, noRobMaxLeft) + max(robMaxRight, noRobMaxRight);
 }
Example #4
0
	void rob(TreeNode *cur, int opt[]) {
		if (cur == nullptr) {
			opt[0] = opt[1] = 0;
			return;
		}
		int lopt[2], ropt[2];
		rob(cur->left, lopt);
		rob(cur->right, ropt);
		opt[0] = cur->val + lopt[1] + ropt[1];
		opt[1] = max(lopt[0], lopt[1]) + max(ropt[0], ropt[1]);
	}
    int rob(vector<int> & num, vector<int> & values, int house) {
        if (house == 0) values[house] = num[0];
        else if (house == 1) values[house] = max(num[0], num[1]);

        if (values[house] != -1)
            return values[house];

        int valueLessOne = rob(num, values, house - 1);
        int valueLessTwo = rob(num, values, house - 2);
        
        values[house] = max(valueLessOne, valueLessTwo + num[house]);
            
        return values[house];
    }
    void rob(TreeNode* node, int& mt, int& ms)
    {
        mt = ms = 0;
        if (!node) return;
        // mtl 抢左子树根节点
        // mtr 抢右子树根节点
        // msl 不抢左子树根节点
        // msl 不抢右子树根节点
        int mtl, mtr, msl, msr;
        rob(node->left, mtl, msl);
        rob(node->right, mtr, msr);

        mt = msl + msr + node->val;
        ms = max(mtl, msl) + max(mtr, msr);
        return;
    }
Example #7
0
    int rob(vector<int>& nums) {
        int len = nums.size();
        if (len == 0) return 0;
        vector<int> rob(len), robnot(len);

        int ret1 = nums[0];
        rob[0] = nums[0];
        for (int i=1; i<len; i++) {
            if (i == len-1) {
                continue;
            }
            rob[i] = robnot[i-1] + nums[i];
            robnot[i] = max(rob[i-1], robnot[i-1]);
            ret1 = max(ret1, rob[i]);
            ret1 = max(ret1, robnot[i]);
        }
        int ret2 = 0;
        rob[0] = robnot[0] = 0;
        for (int i=1; i<len; i++) {
            if (i == len-1) {
                ret2 = max(ret2, robnot[i-1]+nums[i]);
                continue;
            }
            rob[i] = robnot[i-1] + nums[i];
            robnot[i] = max(rob[i-1], robnot[i-1]);
            ret2 = max(ret2, rob[i]);
            ret2 = max(ret2, robnot[i]);
        }

        return max(ret1, ret2);
    }
HRESULT DensoControllerRC8::AddRobot(XMLElement *xmlElem)
{
  int objs;
  HRESULT hr;

  Name_Vec   vecName;
  hr = DensoBase::GetObjectNames(ID_CONTROLLER_GETROBOTNAMES, vecName);
  if(SUCCEEDED(hr)) {
    for(objs = 0; objs < vecName.size(); objs++) {
      Handle_Vec vecHandle;
      hr = DensoBase::AddObject(
          ID_CONTROLLER_GETROBOT, vecName[objs], vecHandle);
      if(FAILED(hr)) break;

      DensoRobot_Ptr rob(new DensoRobotRC8(this,
          m_vecService, vecHandle, vecName[objs], m_mode));
      hr = rob->InitializeBCAP(xmlElem);
      if(FAILED(hr)) break;

      m_vecRobot.push_back(rob);
    }
  }

  return hr;
}
 int rob(TreeNode* root)
 {
     // mt: rob root
     // ms: don't rob root
     int mt, ms;
     rob(root, mt, ms);
     return max(mt, ms);
 }
 int rob_new(vector<int> &nums) {
   vector<int> rob(nums.size(),0);
   rob[0] = nums[0];
   for (int i=1;i<nums.size();i++) {
     rob[i] = max(rob[i-1],rob[i-2] + nums[i]);
   }
   return rob[nums.size()-1];
 }
Example #11
0
int main() {
    int nums[] = { 1, 2, 3, 4, 5 };

    assert(rob(nums, sizeof(nums) / sizeof(nums[0])) == 8);

    printf("all tests passed!\n");

    return 0;
}
Example #12
0
int main(int argc, char **argv) {
//initialize the map
    Map * map = new Map("cave.png");

    Robot rob(map);

    rob.run();

}
 int rob(vector<int> & num) {
     int size = num.size();
     
     if (size == 0)
         return 0;
     
     vector<int> values(size, -1);
     return rob(num, values, size - 1);
 }
Example #14
0
    /*
     * returns:
     * 0 - robot transform
     * 1 - object transform
     */
    std::vector<tf::Transform>
      getObjectPose(const gazebo_msgs::ModelStates::ConstPtr & msg, std::string obj_name)
    {
        tf::Vector3 pose(0,0,0);
        tf::Quaternion rot(0,0,0,1);
        tf::Transform obj_t(rot, pose);
        tf::Transform rob(rot, pose);
        unsigned int found = 0;
        std::vector<tf::Transform> tfs;

        unsigned int count = msg->name.size();
        // Note: backwards loop as typically these objects are at the end of the
        // array. Does not have to be!
        for (int i=count-1; i>=0; --i)
        {
          if( obj_name ==  msg->name[i] )
          {
            pose.setValue(
                  msg->pose[i].position.x,
                  msg->pose[i].position.y,
                  msg->pose[i].position.z);
            rot.setX(msg->pose[i].orientation.x);
            rot.setY(msg->pose[i].orientation.y);
            rot.setZ(msg->pose[i].orientation.z);
            rot.setW(msg->pose[i].orientation.w);

            obj_t.setOrigin(pose);
            obj_t.setRotation(rot);
            ++found;
          }
          if( robot_gaz_name == msg->name[i] )
          {
            pose.setValue(
                  msg->pose[i].position.x,
                  msg->pose[i].position.y,
                  msg->pose[i].position.z);
            rot.setX(msg->pose[i].orientation.x);
            rot.setY(msg->pose[i].orientation.y);
            rot.setZ(msg->pose[i].orientation.z);
            rot.setW(msg->pose[i].orientation.w);

            rob.setOrigin(pose);
            rob.setRotation(rot);
            ++found;
          }
          // optimization
          if(found >= 2) break;
        }

        tfs.push_back(rob);
        tfs.push_back(obj_t);
        //ROS_INFO("found pose: %4.2f, %4.2f", msg->pose[i].position.x, msg->pose[i].position.y);
        //ROS_INFO("found pose: %4.2f, %4.2f", obj_t.getOrigin().getX(), obj_t.getOrigin().getY());
        return tfs;
    }
int main(int argc, char **argv)
{
    //int nums[] = {1,1,1,1,1,2};
    //int nums[] = {5,11,9,100,2};
    //int nums[] = {2,1,1,2};
    int nums[] = {1,3,1,3,100};
    int numsSize = sizeof(nums)/sizeof(int);
    printf("harvest %d\n", rob(nums, numsSize));

    return 0;
}
Example #16
0
int main(int argc, const char * argv[]) {

    
    if (argc > 1){
        // Read from given filename
        std::fstream file(argv[1]);
        if(file.is_open()){
            Labyrinth lab (file);
            
            if (argc == 2) {
                WallFollower rob(lab);
                rob.path();
                std::cout << "Robot finished on " << rob.getSteps() << " steps" << std::endl;
            } else {
                std::vector<Robot*> robList;
                
                for (int i = 2; i < argc; i++) {
                    std::string arg = argv[i];
                    
                    if (arg == "-h") lab.help();
                    else if (arg == "-t1") robList.push_back(new WallFollower(lab));
                    else if (arg == "-t2") robList.push_back(new Tremaux(lab));
                    else if (arg == "-t3") robList.push_back(new Recursive(lab));
                    else std::cout << "Argument " << arg << " not recognised" << std::endl;
                }
                
                // Solve robots
                for(auto it = robList.begin(); it != robList.end(); ++it)
                    (*it)->path();
            
                // Show result
                for(auto it = robList.begin(); it != robList.end(); ++it){
                    std::cout << "Robot " << it - robList.begin() << " has taken " << (*it)->getSteps() << " steps"<< std::endl;
                    (*it)->showPath();
                }
                
                // Remove robots
                for(auto it = robList.begin(); it != robList.end(); ++it)
                    delete *it;
                
            }
        } else {
            std::cout << "File '" << argv[1] << "' cold not be oppend." << std::endl;
        }
        file.close();
    } else {
        std::cout << "Filename missing" << std::endl;
    }
    
    return 0;
}
Example #17
0
int main() {
	int n;
	while (cin >> n && n > 0) {
		vector<int> nums;
		int temp;
		for (int i = 0; i < n; i++) {
			cin >> temp;
			nums.push_back(temp);
		}

		cout << "Max Money: " << rob(nums) << endl;
	}
	return 0;
}
Example #18
0
    int rob(TreeNode* root) {
        if (root == NULL) return 0;
        int sum1 = rob(root->left) + rob(root->right);
        int sum2 = root->val;
        if (root->left) sum2 += rob(root->left->left) + rob(root->left->right);
        if (root->right) sum2 += rob(root->right->left) + rob(root->right->right);

        return max(sum2, sum1);
    }
Example #19
0
 int rob(vector<int>& nums) {
     int len = nums.size();
     if (len == 0) return 0;
     vector<int> rob(len), robnot(len);
     
     int ret = nums[0];
     rob[0] = nums[0];
     for (int i=1; i<len; i++) {
         rob[i] = robnot[i-1] + nums[i];
         robnot[i] = max(rob[i-1], robnot[i-1]);
         ret = max(ret, rob[i]);
         ret = max(ret, robnot[i]);
     }
     return ret;
 }
int rob(TreeNode* root) {
    if(root==NULL) return 0;
    if(tmap.find(root)!=tmap.end()) return tmap[root];
    int ls=0,rs=0;
    if(root->left)
        ls = rob(root->left->left) + rob(root->left->right);
    if(root->right)
        rs = rob(root->right->left) + rob(root->right->right);
    return tmap[root] = max(root->val+ls+rs, rob(root->left)+rob(root->right));
}
Example #21
0
 int rob(TreeNode* root) {
     if (! root) return 0;
     
     // without this may work, but times out for large input.
     if (! root->left && ! root->right) return root->val;
     
     return max(
         root->val + (root->left ? (rob(root->left->left) + rob(root->left->right)) : 0)
                + (root->right ? (rob(root->right->left) + rob(root->right->right)) : 0),
         rob(root->left) + rob(root->right)
         );
 }
Example #22
0
int rob(struct TreeNode* root) {
    if(!root){
        return 0;
    }
    int sum1 = 0;
    if(root->left){
        sum1 += (rob(root->left->left) + rob(root->left->right));
    }
    if(root->right){
        sum1 += ( rob(root->right->left) + rob(root->right->right));
    }
    sum1 += (root->val);
    int sum2 = rob(root->left) + rob(root->right);
    return sum1 > sum2 ? sum1: sum2;
}
Example #23
0
 int rob(TreeNode* root) {
     if(!root) return 0;
     if(!root->left && !root->right) return root->val;
     
     int l = 0, r = 0, subl = 0, subr = 0;
     if(root->left){
         l = rob(root->left);
         subl = rob(root->left->left)+rob(root->left->right);
     }
     if(root->right){
         r = rob(root->right);
         subr = rob(root->right->left) + rob(root->right->right);
     }
     return max(subl+subr+root->val, l+r);
 }
Example #24
0
 int rob(vector<int>& nums) {
     int len = nums.size();
     if (len == 0) return 0;
     if (len == 1) return max(0, nums[0]);
     vector<int> rob(len), pass(len);
     // not rob first house
     rob[0] = pass[0] = 0;
     for (int i = 1; i < len; ++i) {
         rob[i] = pass[i - 1] + nums[i];
         pass[i] = max(pass[i - 1], rob[i - 1]);
     }
     int res = max(rob[len - 1], pass[len - 1]);
     // rob first house
     rob[0] = nums[0]; pass[0] = 0;
     for (int i = 1; i < len; ++i) {
         rob[i] = pass[i - 1] + nums[i];
         pass[i] = max(pass[i - 1], rob[i - 1]);
     }
     res = max(res, pass[len - 1]);
     return res;
 }
Example #25
0
    int rob(TreeNode* root) {
        if (root == NULL)
            return 0;

        if (root->left == NULL && root->right == NULL)
            return root->val;

        int left = 0, right = 0;

        if (root->left)
            left = rob(root->left->left) + rob(root->left->right);
        if (root->right)
            right = rob(root->right->left) + rob(root->right->right);

        int max = 0;
        max = left + right + root->val;

        left = rob(root->left);
        right = rob(root->right);

        max = std::max(max, left + right);
        return max;
    }
Example #26
0
    int rob(TreeNode* root) {
		int opt[2];
		rob(root, opt);
		return max(opt[0], opt[1]);
    }
 int rob(vector<int>& nums) {
     if (nums.size() < 2) return nums.size() ? nums[0] : 0;
     return max(rob(nums, 0, nums.size() - 2), rob(nums, 1, nums.size() - 1));
 }
 int houseRobber3(TreeNode* root) {
     ii ans = rob(root);
     return max(ans.first, ans.second);
 }
int rob(vector<int>& nums){
	if(nums.size() ==0) return 0;
    if(nums.size() == 1) return nums[0];
    int n = nums.size();
    return max(rob(nums,0, n-2), rob(nums, 1, n-1));
}
Example #30
0
 int rob(TreeNode* root) {
     int robMax, noRobMax;
     rob(root, robMax, noRobMax);
     return max(robMax, noRobMax);
 }