• 每日算法


    每日算法

    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.3.14


    红与黑

    简单广搜,当然也可以深搜

    #include <iostream>
    #include <algorithm>
    #include <queue>
    
    using namespace std;
    
    const int N = 25;
    
    char a[N][N];
    bool vis[N][N];
    int w , h , dir[4][2] = {{1,0},{-1,0},{0,1},{0,-1}};
    int sx , sy , ans = 0;
    
    struct node{
    	int x, y ;
    };
    
    bool inmap(int x,int y)
    {
    	return x >= 1 && x <= h && y >= 1 && y <= w;
    }
    bool check(int x,int y)
    {
    	if(inmap(x,y) && a[x][y] == '.' && vis[x][y] == 0)
    	{
    		return 1;	
    	}else return 0;
    }
    void bfs(int x , int y)
    {
    	vis[x][y] = 1;
    	queue<node> q;
    	q.push({x, y});
    	ans ++ ;
    	
    	while(!q.empty())
    	{
    		node now = q.front();
    		for(int i = 0;i < 4;i ++)
    		{
    			int nx = now.x + dir[i][0];
    			int ny = now.y + dir[i][1];
    			if(check(nx,ny))
    			{
    				vis[nx][ny] = 1;
    				q.push({nx ,ny});
    				ans++;
    			}
    		}
    		q.pop();
    	}
    }
    int main()
    {
    	while(1)
    	{
    		cin >> w >> h;
    		if(w == 0 && h == 0)break;
    		string s;
    		for(int i = 1;i <= h ;i ++)
    		{
    			cin >> s;
    			for(int j = 0;j < s.size() ;j ++)
    			{
    				a[i][j + 1] = s[j];
    				if(a[i][j + 1] == '@')
    				{
    					sx = i;sy = j + 1;
    				}
    			}
    		}
    		bfs(sx,sy);
    		cout << ans << endl;
    		ans = 0;
    		fill(vis[0],vis[0] + N * N , 0);
    	}		
    	return 0;
    } 
    

    lqb完全二叉树得权值

    模拟完全二叉树得性质即可

    #include <iostream>
    #include <algorithm>
    #include <cstdio>
    #include <string>
    #include <cmath>
    
    using namespace std;
    const int N = 100005; 
    int t[N] , n ;
    long long ansl = 1,anss = 0;
    int main()
    {
    	cin >> n;
    	for(int i = 1; i <= n ;i ++) scanf("%d",&t[i]);
    	int level = 1 , i = 1; anss = t[1];
    	while(i <= n)
    	{
    		long long sum = 0;
    		for(int j = pow(2,level - 1);j < pow(2,level);i++,j++)
    		{
    			sum += t[i];
    			if(i == n){
    				i ++ ;break;
    			}
    		}
    		if(anss < sum){
    			anss = sum;ansl = level;
    		}
    		level++;	
    	}
    	cout << ansl << endl;
    	return 0;
    }
    

    POJ 2251 Dungeon Master

    多了一层维度得广度优先搜索,还是第一见到,就是比普通得多了一个上下维度

    #include <iostream>
    #include <algorithm>
    #include <cstdio>
    #include <queue>
    
    using namespace std;
    const int N = 105;
    
    
    char a[N][N][N];
    bool vis[N][N][N];
    int l , r, c;
    int sl, sx ,sy;
    int dir[4][2] = {{1,0},{-1,0},{0,1},{0,-1}};
    int updown[2] = {1,-1};
    struct node{
    	int l , x, y , time;
    };
    
    bool inmap(int ll ,int x,int y)
    {
    	return ll >= 1 && ll <= l && x >= 1 && x <= r && y >= 1 && y <= c;	
    }
    
    bool check(int l , int x , int y)
    {
    	if(inmap(l,x,y) && !vis[l][x][y] && a[l][x][y] != '#')
    	{
    		return true;
    	}else return false;
    }
    
    int bfs()
    {
    	vis[sl][sx][sy] = 1;
    	queue<node> q;
    	q.push({sl, sx ,sy , 0});
    	bool flag = 0;
    	while(!q.empty())
    	{
    		node now = q.front();
    		// 在当前层不变
    		 
    		for(int i = 0;i < 4;i ++)
    		{
    			int nx = now.x + dir[i][0];
    			int ny = now.y + dir[i][1];
    			int nl = now.l;
    			if(check(nl,nx,ny))
    			{
    				if(a[nl][nx][ny] == 'E')
    				{
    					flag = 1;
    					return now.time + 1;
    				}
    				vis[nl][nx][ny] = 1;
    				q.push({nl,nx,ny,now.time + 1});	
    			}	
    		}
    		// 值改变当前层数
    		for(int i = 0;i < 2;i ++)
    		{
    			int nx = now.x,ny = now.y;
    			int nl = now.l + updown[i];
    			if(check(nl,nx,ny))
    			{
    				if(a[nl][nx][ny] == 'E')
    				{
    					flag = 1;
    					return now.time + 1;
    				}
    				vis[nl][nx][ny] = 1;
    				q.push({nl,nx,ny,now.time + 1});	
    			}	
    		} 
    		q.pop();
    	}
    	if(!flag) return -1;
    }
    
    void output(int ans)
    {
    	if(ans == -1)
    	{
    		cout << "Trapped!" << endl;
    		return;	
    	}else{
    		printf("Escaped in %d minute(s).
    ",ans);
    	}
    }
    
    void input()
    {
    	string s;
    	for(int k = 1;k <= l;k ++)
    	{
    		for(int i = 1;i <= r ;i ++)
    		{
    			cin >> s;
    			for(int j = 0;j < s.size() ;j ++)
    			{
    				a[k][i][j+1] = s[j];
    				if(a[k][i][j+1] == 'S')
    				{
    					sl = k;sx = i;sy = j + 1;		
    				}
    			}
    		}	
    	}	
    }
    void clear()
    {
    	for(int k = 1;k <= l;k ++)
    	{
    		for(int i = 1;i <= r ;i ++)
    		{
    			for(int j = 1;j <= c ;j ++)
    			{
    				vis[k][i][j] = 0;
    			}
    		}	
    	}
    }
    int main()
    {
    	while(1)
    	{
    		cin >> l >> r >> c;
    		if(l == 0 && r == 0 && c == 0)break;
    		input();
    		int dis = bfs();
    		output(dis);
    		clear();
    	}	
    	return 0;
    }
    

    lqb 全球变暖

  • 相关阅读:
    初始化生成linux sysfs(8)
    内存延迟监控系统组件
    数组代码First Missing Positive
    类文件Spring中空值的写法java教程
    状态键盘完美适应iOS中的键盘高度变化
    框架绑定JavaScript MVC框架PK:Angular、Backbone、CanJS与Ember
    域编码jquery的AJAX跨域请求及跨域请求的原理
    数据格式利用GSON接卸JSON数据
    网元查看一个无厘头的core dump问题定位
    类型应用oracle如何显示毫秒?
  • 原文地址:https://www.cnblogs.com/wlw-x/p/12493514.html
Copyright © 2020-2023  润新知