前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >天池在线编程限时赛 --- 【夕阳下的奔跑场】脱单队

天池在线编程限时赛 --- 【夕阳下的奔跑场】脱单队

作者头像
杨鹏伟
发布2020-11-12 10:45:11
3060
发布2020-11-12 10:45:11
举报
文章被收录于专栏:ypw

1.Character deletion

代码语言:javascript
复制
class Solution {
public:
    /**
     * @param str: The first string given
     * @param sub: The given second string
     * @return: Returns the deleted string
     */
    string CharacterDeletion(string &str, string &sub) {
        // write your code here
    char buf[256] = { 0 };
	for (int i = 0; i < sub.size(); i++)
	{
		buf[sub[i]]++;
	}
	string ret;
	for (int i = 0; i < str.size(); i++)
	{
		if (buf[str[i]] == 0)
			ret += str[i];
	}
	return ret;

    }
};

2.扫雷 一个dfs

代码语言:javascript
复制
class Solution {
public:
    /**
     * @param Mine_map: an array represents the map.
     * @param Start: the start position.
     * @return: return an array including all reachable positions.
     */
     using pii = pair<int,int>;
    vector<vector<int>> Mine_sweeping(vector<vector<int>> &Mine_map, vector<int> &Start) {
        // write your code here
        	int dx[] = {-1,1,0,0};
			int dy[] = {0,0,-1,1};
			queue<pii> q;
			q.emplace(Start[0],Start[1]);
			int n=Mine_map.size(),m=Mine_map[0].size();
			auto check = [&](int x,int y){
				return x >= 0 && x < n && y>=0 && y<m; 
			};
			set<vector<int>> temp;
			while(q.size()){
				auto t = q.front();
				q.pop();
				int i=t.first,j=t.second;
				temp.insert(vector<int>{i,j});
				if(!Mine_map[i][j]) continue;
				Mine_map[i][j] = 0;
				for(int k=0;k<4;k++){
					int x = i + dx[k];
					int y = j + dy[k];
					if(check(x,y)) q.emplace(x,y);
				}
			}
			return vector<vector<int>>(temp.begin(),temp.end()); 
    }
};

3.旅行计划

代码语言:javascript
复制
class Solution {
public:
    /**
     * @param arr: the distance between any two cities
     * @return: the minimum distance Alice needs to walk to complete the travel plan
     */
    int travelPlan(vector<vector<int>> &arr) {
        // Write your code here.
        int n = arr.size();
        vector<int> a;
        for(int i=1;i<n;i++) a.emplace_back(i);
        int ans  =INT_MAX;
        do{
          int mini = 0,now = 0;
          for(auto nxt : a){
          mini += arr[now][nxt];
          now = nxt;
          }
          ans = min(ans,mini + arr[now][0]);
        }while(next_permutation(a.begin(),a.end()));
        return ans;
    }
};
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2020/11/07 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档