Atcoder abc 302

传送门

B.Find Snuke

题意:

给一个 n*m的方阵,找到连续的 ”snuke" 字符串,要求是必须在同一条线上。

思路:

先找 s   第一次进入dfs 搜索四周找下一个字符,这个时候已经确定方向了,之后就往这个方向搜;

代码:

#include<bits/stdc++.h>
using namespace std;

const int N = 1010, INF = 0x3f3f3f3f,mod=1e9+7;
typedef long long LL;
typedef pair<int, int>PII;
int n,m,k,_;
char s[N][N];
string p("snuke");
vector<PII>v;

int dx[]={-1,-1,-1,0,0,1,1,1},dy[]={1,0,-1,1,-1,1,0,-1};
void dfs(int x,int y,int u,int cx,int cy)//cx,cy记录方向
{
	if(u>5) return;
	v.push_back({x,y});
	if(u==5)
	{
		for(auto t:v)
			cout<<t.first<<" "<<t.second<<endl;
		exit(0);
	}
	if(cx==-2&&cy==-2)
	{
		for(int i=0;i<8;i++)
		{
			int mx=x+dx[i],my=y+dy[i];
			if(s[mx][my]==p[u]&&mx<=n&&mx>0&&my<=m&&my>0)
			{
				dfs(mx,my,u+1,dx[i],dy[i]);
				v.pop_back();
			}
		}
	}
	else
	{
		int mx=x+cx,my=y+cy;
		if(s[mx][my]==p[u]&&mx<=n&&mx>0&&my<=m&&my>0)
			dfs(mx,my,u+1,cx,cy),v.pop_back();
	}

}
void solve()
{
	cin>>n>>m;
	for(int i=1;i<=n;i++)
		for(int j=1;j<=m;j++)
			cin>>s[i][j];
	for (int i = 1; i <=n; ++i)
	{
		for(int j=1;j<=m;j++)
		{
			if(s[i][j]=='s')
			{
				dfs(i,j,1,-2,-2);//第一次传入只要不是-1~1就行
				v.pop_back();
			}
		}
	}
}

int main()
{
	
	int T=1;
	//cin>>T;
	while(T--)
	{
		solve();
	}
		return 0;
}

C - Almost Equal

题意

给你n 个字符串,问能不能调整位置后使得从上往下相邻的字符串都只有一个字符不同

思路

数据范围很小,就想着用搜索(不知道有没有更好的),每个字符串都用一个set存他能走的字符串(即只有一个字符不同),只要能一直走完n个,就是可以;

代码

#include<bits/stdc++.h>
using namespace std;

const int N = 1010, INF = 0x3f3f3f3f,mod=1e9+7;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int>PII;
int n,m,k,_;
vector<string>s;
vector<int> v[N];
bool vis[N];

bool pd(string a,string b)
{
	int t=0;
	for(int i=0;i<m;i++)
		if(a[i]!=b[i])
			if(++t>1) return false;
	return true;

}

void dfs(int x,int u)
{
	vis[x]=true;
	if(u>n) return ;
	if(u==n) puts("Yes"),exit(0);
	for(auto ed:v[x])
	{
		if(!vis[ed])
		{
			dfs(ed,u+1);
			vis[ed]=false;
		}
	}
}
void solve()
{
	cin>>n>>m;
	for(int i=0;i<n;i++)
	{
		string a;
		cin>>a;
		s.push_back(a);
	}

	for(int i=0;i<n;i++)
		for(int j=i+1;j<n;j++)
		{
			
			if(pd(s[i],s[j]))
				v[i].push_back(j),v[j].push_back(i);
		}
	for(int i=0;i<n;i++)
	{
		memset(vis,0,sizeof vis);
		dfs(i,1);
	}
	puts("No");

}

int main()
{
	
	int T=1;
	//cin>>T;
	while(T--)
	{
		solve();
	}
		return 0;
}

D - Impartial Gift(二分)

题意:

给两个数组,从两个数组中各找一个数,差值不能大于k,使两个数和最大

思路;

既然要找最大,那肯定从最大的开始往小的找。

n m的范围是2*1-5,所以不可能遍历完两个数组。那找的时候从 a[i](第一个数组)+k 开始往下找,差值大于k就停

代码

#include<bits/stdc++.h>
using namespace std;

const int N = 1000010, INF = 0x3f3f3f3f,mod=1e9+7;
typedef long long LL;
typedef pair<int, int>PII;
LL n,m,k,_,d;
LL a[N],b[N],p[N];
vector<string>s;
std::vector<int> v[N];

void solve()
{
	cin>>n>>m>>d;
	for(int i=1;i<=n;i++)
		cin>>a[i];
	for(int i=1;i<=m;i++)
		cin>>b[i];

	sort(a+1,a+1+n);
	sort(b+1,b+1+m);
	for(int i=1;i<=n;i++)
	{
		p[i]=upper_bound(b+1,b+1+m,a[i]+d)-(b+1);
	}
	for(int i=n;i>=1;i--)
		for(int j=p[i];j>=1;j--)
		{
			if(a[i]-b[j]>d) break;
			if(abs(a[i]-b[j])<=d)
			{
				cout<<(LL)a[i]+b[j];
				exit(0);
			}
		}
	cout<<-1;

}

int main()
{
	
	int T=1;
	//cin>>T;
	while(T--)
	{
		solve();
	}
		return 0;
}

E - Isolation

题意

给N条边 m个询问,每次询问后输出当前还有几个点没有边

1.连两个点,2.把一个点的所有边删掉;

思路:set直接写,

代码

#include<bits/stdc++.h>
using namespace std;

const int N = 1000010, INF = 0x3f3f3f3f,mod=1e9+7;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int>PII;
int n,m,k,_,d;
set<int>e[N];
void solve()
{
	cin>>n>>m;
	int cnt=n;
	while(m--)
	{
		int op;

		cin>>op;
		if(op==1)
		{
			int a,b;
			cin>>a>>b;
			if(e[a].size()==0) cnt--;
			if(e[b].size()==0) cnt--;
			e[a].insert(b);
			e[b].insert(a);
			cout<<cnt<<endl;
		}
		else
		{
			int x;
			cin>>x;
			if(e[x].size()!=0) cnt++;
			for(auto ed:e[x])
			{
				e[ed].erase(x);
				if(e[ed].size()==0) cnt++;
			
			}
			e[x].clear();
			cout<<cnt<<endl;
		}
	}

}

int main()
{
	
	int T=1;
	//cin>>T;
	while(T--)
	{
		solve();
	}
		return 0;
}

F - Merge Set

题意:

n个集合,,每个集合的点在1-m间。 只有两个集合有共同元素时才可以合并两个集合,问能否得到集合包含1和m,如果可以输出最小操作次数;

思路:

转化成最短路问题,其实就是判断能否从1走到m。在每个集合外面放置一个虚拟点,从集合里的点走到虚拟点代价为1,相反为0,输出的时候要减一。

代码:

#include<bits/stdc++.h>
using namespace std;

const int N = 1000010, INF = 0x3f3f3f3f,mod=1e9+7;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int>PII;
int n,m,k,_;
vector<PII>v[N];
int dist[N];
bool vis[N];
void dijkstra()
{
	memset(dist,INF,sizeof dist);
	dist[1]=0;

	priority_queue<PII,vector<PII>,greater<PII>>q;
	q.push({0,1});
	while(q.size())
	{
		int pos=q.top().second;
		q.pop();
		if(vis[pos]) continue;
		vis[pos]=true;
		for(auto t:v[pos])
		{
			int ed=t.first,w=t.second;
			if(dist[ed]>dist[pos]+w)
			{
				dist[ed]=dist[pos]+w;
				q.push({dist[ed],ed});
			}
		}
	}
}
void solve()
{
	cin>>n>>m;
	for(int i=1;i<=n;i++)
	{
		cin>>k;
		while(k--)
		{
			int x;
			cin>>x;
			v[x].push_back({i+m,1});
		v[i+m].push_back({x,0});
		}
	}

	dijkstra();
	if(dist[m]==INF) cout<<-1;
	else cout<<dist[m]-1;

}

int main()
{
	
	int T=1;
	//cin>>T;
	while(T--)
	{
		solve();
	}
		return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值