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


    luogu- P1162 填涂颜色

    广度优先搜索

    #include <iostream>
    #include <algorithm>
    #include <cstdio>
    #include <queue>
    
    using namespace std;
    const int N = 35;
    int a[N][N], n ,dir[4][2] = {{1,0},{-1,0},{0,1},{0,-1}};
    bool vis[N][N];
    
    
    struct node{
    	int x, y;
    };
    
    bool inmap(int x,int y)
    {
    	return x >= 0 && x <= n + 1 && y >= 0 && y <= n + 1;
    }
    
    void bfs(int x,int y)
    {
    	queue<node> q;
    	q.push({x,y});
    	vis[x][y] = 1;
    	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(inmap(nx,ny) && a[nx][ny] != 1 && !vis[nx][ny])
    			{
    				vis[nx][ny] = 1;
    				q.push({nx,ny});
    			}
    		}
    		q.pop();
    	}
    }
    int main()
    {
    	cin >> n;
    	for(int i = 1;i <= n ;i ++)
    	{
    		for(int j = 1;j <= n ;j ++)
    		{
    			scanf("%d",&a[i][j]);
    		}
    	} 
    	bfs(0,0);
    	for(int i = 1;i <= n ;i ++)
    	{
    		for(int j = 1;j <= n ;j ++)
    		{
    			if(vis[i][j] == 0 && a[i][j] != 1)cout << "2 ";
    			else cout << a[i][j] << " ";
    		}
    		cout << endl;
    	}
    	return 0;
    }
    

    P1141 01 迷宫

    广度优先搜索 + 记忆化

    #include <iostream>
    #include <string>
    #include <cstdio>
    #include <algorithm>
    #include <queue>
    #include <vector>
    
    using namespace std;
    const int N = 1010;
    int a[N][N], f[N][N], book[1000010][2], n, m;
    bool vis[N][N];
    int cnt = 0, dir[4][2] = { {1,0},{-1,0},{0,1},{0,-1} };
    struct node {
    	int x, y;
    };
    
    bool inmap(int x, int y)
    {
    	return x >= 1 && y >= 1 && x <= n && y <= n;
    }
    
    void bfs(int x, int y)
    {
    	vis[x][y] = 1;
    	book[cnt][0] = x;
    	book[cnt][1] = y;
    	queue<node> q;
    	q.push({ x,y });
    	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 (inmap(nx, ny) && !vis[nx][ny] && a[now.x][now.y] != a[nx][ny])
    			{
    				cnt++;
    				book[cnt][0] = nx;
    				book[cnt][1] = ny;
    				vis[nx][ny] = 1;
    				q.push({ nx,ny });
    			}
    		}
    		q.pop();
    	}
    }
    int main()
    {
    	cin >> n >> m;
    	string s;
    	for (int i = 1; i <= n; i++)
    	{
    		cin >> s;
    		for (int j = 0; j < s.size(); j++)
    		{
    			a[i][j + 1] = (s[j] == '1' ? 1 : 0);
    		}
    	}
    
    	for (int i = 1; i <= n; i++)
    	{
    		for (int j = 1; j <= n; j++)
    		{
    			if (!vis[i][j])
    			{
    				cnt = 0;
    				bfs(i, j);
    				for (int k = 0; k <= cnt; k++)
    				{
    					f[book[k][0]][book[k][1]] = cnt + 1;
    				}
    			}
    		}
    	}
    	int a, b;
    	for (int i = 0; i < m; i++)
    	{
    		scanf("%d %d", &a, &b);
    		printf("%d
    ", f[a][b]);
    	}
    	return 0;
    }
    
  • 相关阅读:
    盒子跟随鼠标移动而移动(兼容IE8)
    JavaScript捕获鼠标坐标
    JavaScript判断滚动条是否滚动到底部
    JavaScript自定义getStyle方法获取元素样式
    JavaScript实现简单的图片切换功能
    SpeedReader
    JavaScript定时器
    远程登陆ubantu服务器 .bashrc文件每次打开终端都需要source的问题
    阿里云服务器Ubantu16.04升级python3.6
    阿里云服务器ubantu创建新用户登录显示问题
  • 原文地址:https://www.cnblogs.com/wlw-x/p/12431542.html
Copyright © 2011-2022 走看看