zoukankan      html  css  js  c++  java
  • 每日算法

    每日算法

    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 全球变暖

  • 相关阅读:
    Programming Style
    一则SQL问题
    C# WINFORM中读取config文件
    《Windows Communication Foundation之旅》系列之四 (转)
    [译]ASP.Net 2.0: Export GridView to Excel (转) 如果GridView中有其它控件,比如Checkboxes,Dropdownlists,我们需要将它转换为其相关的值,以下递归就用于导出Excel前的准备工作,将各类控件转换为其相关值.
    Windows Communication Foundation入门(Part One) (转)
    DOM方法和属性 使用范例
    一套.net面试题~ 没有正确答案~ 大家做做看
    一则 Oracle 和 SqlServer 语法区别 (原创)
    最基本的Socket编程 C#版 [转]
  • 原文地址:https://www.cnblogs.com/wlw-x/p/12493514.html
Copyright © 2011-2022 走看看