zoukankan      html  css  js  c++  java
  • Daliy Algorithm(链表&搜索&剪枝) -- day 52

    Nothing to fear

    those times when you get up early and you work hard; those times when you stay up late and you work hard; those times when don’t feel like working — you’re too tired, you don’t want to push yourself — but you do it anyway. That is actually the dream. That’s the dream. It’s not the destination, it’s the journey. And if you guys can understand that, what you’ll see happen is that you won’t accomplish your dreams, your dreams won’t come true, something greater will. mamba out


    那些你早出晚归付出的刻苦努力,你不想训练,当你觉的太累了但还是要咬牙坚持的时候,那就是在追逐梦想,不要在意终点有什么,要享受路途的过程,或许你不能成就梦想,但一定会有更伟大的事情随之而来。 mamba out~

    2020.4.7


    P1378 油滴扩展

    fabs(float x): 求浮点类型数字的绝对值
    round(float x) : 将小数进行四舍五入
    fixed: cout << fixed << xxx; 用一般的方式输出浮点型,例如C++程序在控制台显示大一点的数,显示的时候使用了科学计数法,使用该命令即可像一般的方式显示
    思路:

    1. 由于N 比较小我们可以枚举油滴的放入顺序最多一工会有2^6种情况
    2. 搜索十分容易实现关键在于如何处理油滴的扩展和选择当前最佳的半径
      需要满足两个条件:
      该半径最多只能和其他圆相切或者相离
      该半径最多只能到达边界
      通过以上两个条件筛选出来最优结果
    3. 细节处理:
      坐标可能出现负数
      一个油滴可能在别的油滴的内部 : 此时需要将油滴的r置为0
    #include <cmath>
    #include <cstdio>
    #include <climits>
    #include <cstdlib>
    #include <cstring>
    #include <iostream>
    
    #define PI 3.1415926
    #define sqar(x)( (x) * (x))
    using namespace std;
    
    const int N = 10;
    int n ;
    struct node{
    	int x , y;
    	double r;
    }oil[N];
    bool vis[N]; // 判断是否用过
    int sx,sy,ex,ey;
    double minn = INT_MAX;
    
    inline double dis(int x,int y)
    {
    	return sqrt(sqar(x) + sqar(y));
    }
    // 得到带点可以达到的最大半径
    double calradius(int index)
    {
    	// 首先计算该点达到
    	// 
    	double x = oil[index].x , y = oil[index].y;
    	double r = min(fabs(x-sx),fabs(y-sy));
    	r = min(r , min(fabs(x-ex),fabs(y-ey)));
    
    	for(int i = 0;i < n ;i ++)
    	{
    		if(vis[i])
    			r = min(r , dis(x - oil[i].x,y - oil[i].y) - oil[i].r);
    	}
    	if(r < 0)r = 0;
    	return r;
    }
    // 一共有N个物品每一个物品都有选择和不选择两种
    void dfs(int step)
    {
    	if(!step) // 当来到叶子节点
    	{
    		// 计算出来每一个圆形的面积然后更新答案
    		double sum = 0;
    		for(int i = 0; i < n ;i ++)
    		{
    			sum += sqar(oil[i].r) * PI;
    		}
    		// 更新最小空间答案
    		sum = abs(sx - ex) * abs(sy - ey) - sum;
    		if(sum < minn) minn = sum;
    		return;
    	}
    	for(int i = 0;i < n ;i ++)
    	{
    		if(vis[i])continue;
    		else{
    			oil[i].r = calradius(i);
    			vis[i] = 1;
    			dfs(step - 1);
    			// 状态恢复
    			vis[i] = oil[i].r = 0;
    		}
    	}
    }
    void input()
    {
    	cin >> n;
    	cin >> sx >> sy >> ex >> ey;
    	for(int i = 0;i < n ;i ++)
    	{
    		scanf("%d %d",&oil[i].x,&oil[i].y);
    	}
    }
    int main()
    {
    	input();
    	dfs(n);
    	cout << fixed << (int)round(minn);
    	return 0;
    }
    

    单链表

    标准版 数组模拟

    
    #include <iostream>
    #include <cstdio>
    #include <cstdlib>
    #include <algorithm>
    #include <cstring>
    
    using namespace std;
    const int N = 100010;
    
    int head , e[N] , ne[N], idx;
    
    void init()
    {
    	head = -1;
    }
    void add_head(int x)
    {
    	e[idx] = x;
    	ne[idx] = head;
    	head = idx++;
    }
    void add_k(int k,int x)
    {
    	e[idx] = x;
    	ne[idx] = ne[k];
    	ne[k] = idx++;
    }
    void remove(int k)
    {
    	ne[k] = ne[ne[k]];
    }
    int main()
    {
    	init();
    	int m , x , k;
    	cin >> m;
    	while(m--)
    	{
    		char op;cin >> op;
    		if(op == 'H')
    		{
    			cin >> x;
    			add_head(x);
    		}
    		else if(op == 'I')
    		{
    			cin >> k >> x;
    			add_k(k  -1, x);
    		}
    		else{
    			cin >> k;
    			if(!k)head = ne[head];
    			else remove(k - 1);
    		}
    	}
    
    	for(int i = head; i != -1 ;i = ne[i])
    		cout << e[i] << " ";
    	return 0;
    }
    

    心碎版 就图一乐

    #include <iostream>
    #include <cstdio>
    #include <cstdlib>
    #include <cstring>
    #include <algorithm>
    
    using namespace std;
    
    int m;
    struct node{
    	int val;
    	int id;
    	struct node *next = NULL;
    };
    int cur = 1;
    node *del(node *link,int k)
    {
    	node *head = link;
    	if(k == 0){
    		node *p = link->next;
    		link->next = link->next->next;
    		free(p);
    		return link;
    	}
    	else{
    		while(head->id != k && head)
    		{
    			head = head->next;
    		}
    		node *p = head->next;
    		head->next = head->next->next;
    		free(p);
    		return link;
    	}
    }
    node *insert(node *link,int k, int val)
    {
    	node *head = link;
    	node *now = new node;
    	now->next = NULL;
    	now->val = val;
    	now->id = cur;cur++;
    	int cnt = 0;
    	while(head->id != k  && head != NULL)
    	{
    		cnt++;
    		head = head->next;
    	}
    	now->next = head->next;
    	head->next = now;
    	return link;
    }
    
    node *inserthead(node *link , int val)
    {
    	node *now = new node;
    	now->next = NULL;
    	now->val = val;
    	now->id = cur;cur++;
    	now->next = link->next;
    	link->next = now;
    	return link;
    }
    
    int main()
    {
    	cin >> m;
    	char ch;int k , x;
    	node *link = new node;
    	for(int i = 1;i <= m ;i ++)
    	{
    		cin >> ch;
    		if(ch == 'H'){
    			cin >> x;
    			link = inserthead(link,x);
    		}else if(ch == 'I'){
    			cin >> k >> x;
    			link = insert(link,k , x);
    		}else if(ch == 'D'){
    			cin >> k;
    			link = del(link,k);
    		}
    	}
    	link=link->next;
    	while(link)
    	{
    		cout << link->val << " ";
    		link = link->next;
    	}
    	return 0;
    }
    

    献给阿尔吉侬的花束

    简单题 就当练手速了

    #include <iostream>
    #include <cstdio>
    #include <algorithm>
    #include <cstdlib>
    #include <cstring>
    #include <queue>
    
    using namespace std;
    const int N = 205;
    char a[N][N];
    bool vis[N][N];
    int n , m  , t;
    int sx , sy ;
    int dir[4][2] = {{1,0},{-1,0},{0,1},{0,-1}}; 
    struct node
    {
    	int x, y , step;
    	node(int x,int y,int c):x(x),y(y), step(c){}
    	node(){} 
    };
    
    bool inmap(int x,int y)
    {
    	return x >= 1 && x <= n && y >= 1 && y <= m;
    }
    
    int bfs(int x ,int y)
    {
    	memset(vis , 0 , sizeof vis);
    	queue<node> q;
    	q.push(node(x , y , 0));
    
    	while(!q.empty())
    	{
    		node now = q.front();
    		q.pop();
    
    		for(int i = 0; i< 4 ;i ++)
    		{
    			int nx = now.x + dir[i][0];
    			int ny = now.y + dir[i][1];
    
    			if(inmap(nx ,ny) && !vis[nx][ny] && a[nx][ny] != '#')
    			{
    				vis[nx][ny] = 1;
    				if(a[nx][ny] == 'E')
    				{
    					return now.step+ 1;
    				}
    				q.push(node(nx,ny, now.step + 1));
    			}
    		}
    	}
    	return -1;
    }
    
    void input()
    {
    	cin >> n >> m;
    	for(int i = 1;i <= n ;i ++)
    	{
    		for(int j = 1;j <= m ;j ++)
    		{
    			cin >> a[i][j];
    			if(a[i][j] == 'S')
    			{
    				sx = i;sy = j;
    			}
    		}
    	}
    	int ans = -1;
    	 ans = bfs(sx ,sy);
    	if(ans < 0){
    		cout << "oop!" << endl;
    	}else cout << ans << endl;
    
    }
    int main()
    {
    	cin >> t;
    	while(t--)
    	{
    		input();
    	}
    	return 0;
    }
    
  • 相关阅读:
    忘记 mysql 数据库连接密码(解决方案)
    CVE-2020-14882&CVE-2020-14883 Weblogic未授权远程命令执行漏洞
    社会工程学之信息收集之信息收集
    8种src常用越权测试小技巧
    《数据中台-让数据用起来》思维导图(更新中)
    idea使用zsh代替系统的terminal
    mac安装oh my zsh
    mac安装homebrew
    navicat破解(亲测可用)
    docker搭建typecho博客系统,并启用https
  • 原文地址:https://www.cnblogs.com/wlw-x/p/12656037.html
Copyright © 2011-2022 走看看