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;
    }
    
  • 相关阅读:
    有效管理时间的十八种方法
    针对某个块下面的按钮注册点击事件
    vs2015运行项目时出现“编译器失败,错误代码为 1”的解决方案
    淘宝API调用 申请 获取session key
    中小型研发团队架构实践:生产环境诊断利器WinDbg帮你快速分析异常情况Dump文件
    中小型研发团队架构实践:如何规范公司所有应用分层?
    中小型研发团队架构实践:电商如何做企业总体架构?
    中小型研发团队架构实践:高效率、低风险,一键发布并测试的持续集成工具Jenkins
    ASP.NET全栈开发验证模块之在Vue中使用前端校验
    计算机基础存储结构
  • 原文地址:https://www.cnblogs.com/wlw-x/p/12431542.html
Copyright © 2011-2022 走看看