每日算法 - day 37

每日算法

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


luogu-P2298 Mzc和男家丁的游戏

主义再次强调洛谷得输入字符系统好像有点问题和windows有一些差异具体原因是为什么还不知道,反正我得每次都会卡我字符输入于是改成字符串就ok 话说 说话得高性能呢 我剪枝得大刀都准备好了你告诉我这就AC了??

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <string>
#include <vector>
#include <queue>
using namespace std;
const int N = 2005;

int n , m , sx, sy;
char a[N][N];
bool vis[N][N];
int dir[4][2] = {{1,0},{-1,0},{0,1},{0,-1}};
bool inmap(int x,int y)
{
	return x >= 1 && x <= n && y >= 1 && y <= m;  
}
struct node{
	int x, y;
	int cost;
	node(int x,int y,int c):x(x),y(y),cost(c){}
};

int ans = 0x3f3f3f;
void bfs(int x,int y)
{
	vis[x][y] = 1;
	queue<node> q;
	q.push(node(x,y,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];
			
			if(inmap(nx,ny) && a[nx][ny] != '#' && !vis[nx][ny])
			{
				vis[nx][ny] = 1;
				if(a[nx][ny] == 'd')
				{
					ans = now.cost + 1;
					return;
				}else{
					q.push(node(nx,ny,now.cost + 1));
				}
			}
		}
		q.pop();
	}
}
void input()
{
	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];
			if(s[j] == 'm')
			{
				sx = i;sy = j + 1;	
			}
		}
	}	
}

int main()
{
	input();
	bfs(sx,sy);
	if(ans != 0x3f3f3f)
	{
		cout << ans << endl;
	}else{
		cout << "No Way!";
	}
	return 0;
}

luogu-P1683 入门

主要问题求得是联通块所以将起始点转化.成为连通块问题

dfs 版本 和bfs版本都有

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <string>
#include <vector>
#include <queue> 

using namespace std;
const int N = 25;
int n , m , sx, sy;
char a[N][N];
int dir[4][2] = {{1,0},{0,1},{-1,0},{0,-1}};
bool vis[N][N];

struct node{
	int x, y , c;
	node(int x,int y,int c):x(x),y(y),c(c){}
};

bool inmap(int x,int y)
{
	return x >= 1 && x <= n && y >= 1 && y <= m;
}

int ans = -1;
void bfs(int x,int y)
{
	queue<node> q;
	q.push(node(x,y,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];
			
			if(inmap(nx,ny) && !vis[nx][ny] && a[nx][ny] == '.')
			{
				vis[nx][ny] = 1;
				ans++;
				q.push(node(nx,ny,now.c + 1));
			}
		}
		q.pop();
	}
}

void dfs(int x,int y)
{
	for(int i = 0;i < 4;i ++)
	{
		int nx = x + dir[i][0];
		int ny = y + dir[i][1];
		if(inmap(nx,ny) && !vis[nx][ny] && a[nx][ny] == '.')
		{
			vis[nx][ny] = 1;ans++;
			dfs(nx,ny);
		}
	}
}

void input()
{
	cin >> m >> n;
	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];
			if(s[j] == '@'){
				sx = i;sy = j + 1;
				a[i][j + 1] = '.';
			}
		}
	}
}

int main()
{
	input();
	//bfs(sx,sy);
	dfs(sx,sy);
	cout << ans + 1<< endl;
	return 0;
} 

luogu-P1007独木桥

基础简单贪心
从中间划分 mid = (0 + l + 1) / 2如果是最少时间就让两边得尽量让去离自己近一些得终点,如果是最大时间就去离自己最远得终点,最后两边一起取一个max 就ok

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <string>

using namespace std;
const int N = 5005;

int a[N] , minc[N], maxc[N];
int n , l;
int main()
{
	cin >> l >> n;
	for(int i = 1;i <= n ;i ++)
	{
		int x = 0;
		scanf("%d",&x);
		a[x]++;
	}
	int mid = l + 1 >> 1;
	for(int i = 1;i <= l;i ++)
	{
		if(i <= mid && a[i] > 0){
			minc[i] = i;
			maxc[i] = l + 1 - i;
		}else if(i >= mid && a[i] > 0){
			minc[i] = l + 1 - i;
			maxc[i] = i;
		}
	}
	int ans1 = 0,ans2 = 0;
	for(int i = 1;i <= l ;i ++)
	{
		ans1 = max(minc[i],ans1);
		ans2 = max(maxc[i],ans2);
	}
	cout << ans1 << " " << ans2;
	return 0;
} 

猜你喜欢

转载自www.cnblogs.com/wlw-x/p/12555049.html