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;
    }
    
  • 相关阅读:
    Synalyze It! Pro v1.11.2
    C# 打开浏览器并 POST 提交信息
    Cocos2d-x iOS Mac环境编译出错 can't locate file for: -lpng -ljpeg -ltiff -lwebp -lfreetype -lwebsockets -lcurl
    Visual Studio 2008用过一段时间后编辑器自动提示(智能提示:Intellisense)功能失效
    iOS根据坐标数据点所在的坐标区域来动态显示到可视范围
    iOS关于百度地图坐标偏移的处理
    WPF画图性能问题
    Xcode Error: The service is invalid (0XE8000022) 解决方法
    引用-定位大量占用CPU的问题
    Semaphore信号量
  • 原文地址:https://www.cnblogs.com/wlw-x/p/12431542.html
Copyright © 2011-2022 走看看