首页 > 其他分享 >ACM-ICPC Nanjing Onsite 2018 - K(随机枚举+四维bfs)

ACM-ICPC Nanjing Onsite 2018 - K(随机枚举+四维bfs)

时间:2023-05-31 10:07:34浏览次数:48  
标签:Onsite yi xi tot int Nanjing p2 ACM mx


题目链接:https://nanti.jisuanke.com/t/33680

 

解题思路:

随机两个袋鼠的位置,使得让他们相遇,那么这个操作就是一个四维的bfs,前两维代表第一只袋鼠的位置,后两维表示第二只袋鼠的位置。这样随机枚举最多是N*M次。

所以时间复杂度最最最最坏情况也就O(N^3*M^3)。

 

#include <bits/stdc++.h>
using namespace std;
const int mx = 22;
const int dx[4] = {0,0,-1,1};
const int dy[4] = {1,-1,0,0};
int n,m,xi[mx*mx],yi[mx*mx];
char str[mx][mx];
int tot,cnt[mx][mx];
int vis[mx][mx][mx][mx];
int q[mx*mx],head,ans[mx*mx*mx*mx],top;
int p[mx][mx][mx][mx]; 
struct node
{
	int x1,y1;
	int x2,y2;
}path[mx][mx][mx][mx];
char dire(int x)
{
	if(x==0) return 'R';
	if(x==1) return 'L';
	if(x==2) return 'U';
	return 'D';
}
void go(int &fx,int &fy,int x,int y,int d)
{
	fx = x + dx[d];
	fy = y + dy[d];
}
bool check(int x,int y)
{
	if(cnt[x][y]==-1) return 0;
	if(x<0||x==n) return 0;
	if(y<0||y==m) return 0;
	return 1;
}
node merge(int p1,int p2,int time)
{
	vis[xi[p1]][yi[p1]][xi[p2]][yi[p2]] = time;
	queue <node> que;
	que.push(node{xi[p1],yi[p1],xi[p2],yi[p2]});
	p[xi[p1]][yi[p1]][xi[p2]][yi[p2]] = -1;
	int fx,fy,px,py;
	while(!que.empty()){
		node now = que.front();
		que.pop();
		for(int i=0;i<4;i++){
			go(fx,fy,now.x1,now.y1,i);
			go(px,py,now.x2,now.y2,i);
			if(!check(fx,fy)) fx = now.x1,fy = now.y1;
			if(!check(px,py)) px = now.x2,py = now.y2;
			if(vis[fx][fy][px][py]!=time)
			{
				path[fx][fy][px][py] = now;
				p[fx][fy][px][py] = i;
				if(fx==px&&fy==py) return node{fx,fy,fx,fy};
				que.push(node{fx,fy,px,py});
				vis[fx][fy][px][py] = time;
			}
		}
	}
}
void up(char d)
{
	int col,row;
	if(d=='U') col = 0,row = 1;
	else col = 1,row = 0;
	for(int i=row;i<n;i++){
		for(int j=col;j<m;j++){
			if(cnt[i][j]==-1) continue;
			if(d=='U'){
				if(cnt[i-1][j]!=-1)
				cnt[i-1][j] += cnt[i][j],cnt[i][j] = 0;
			}else{
				if(cnt[i][j-1]!=-1)
				cnt[i][j-1] += cnt[i][j],cnt[i][j] = 0;
			}
		}
	}
}
void down(char d)
{
	int col,row;
	if(d=='D') col = m-1,row = n-2;
	else col = m-2,row = n-1;
	for(int i=row;~i;i--){
		for(int j=col;~j;j--){
			if(cnt[i][j]==-1) continue;
			if(d=='D'){
				if(cnt[i+1][j]!=-1)
				cnt[i+1][j] += cnt[i][j],cnt[i][j] = 0;
			}else{
				if(cnt[i][j+1]!=-1)
				cnt[i][j+1] += cnt[i][j],cnt[i][j] = 0;
			}
		}
	}
}
void Get()
{
	for(int i=0;i<head;i++){
		char d = dire(q[i]);
		if(d=='D'||d=='R') down(d);
		else up(d);
	}
}
void find(int x1,int y1,int x2,int y2)
{
	int i = p[x1][y1][x2][y2];
	if(i==-1) return ;
	node now = path[x1][y1][x2][y2];
	find(now.x1,now.y1,now.x2,now.y2);;
	ans[top++] = i, q[head++] = i;
}
int main()
{
	scanf("%d%d",&n,&m);
	for(int i=0;i<n;i++){
		scanf("%s",str[i]);
		for(int j=0;j<m;j++){
			cnt[i][j] = str[i][j] - '0';
			if(cnt[i][j]) xi[tot] = i,yi[tot++] = j;
			else cnt[i][j] = -1;
		}
	}
	int cas = 1;
	while(tot>1){
		int p1 = tot-1,p2 = (tot-1)/2;
		node eng = merge(p1,p2,cas++);
		tot = head = 0;
		find(eng.x1,eng.y1,eng.x2,eng.y2);
		Get();
		for(int i=0;i<n;i++){
			for(int j=0;j<m;j++){
				if(cnt[i][j]>0) xi[tot] = i,yi[tot++] = j;
			}
		}
	}
	for(int i=0;i<top;i++) putchar(dire(ans[i]));
	puts("");
	return 0;
}

 

标签:Onsite,yi,xi,tot,int,Nanjing,p2,ACM,mx
From: https://blog.51cto.com/u_12468613/6384495

相关文章

  • 2018-2019 ACM-ICPC Southeastern European Regional Programming Contest (SEERC 201
    题目链接:https://vjudge.net/contest/339284#overview A.Numbers待做B.BrokenWatchs=input()s=s.split("")A,B,C,N=list(map(int,s))n=(N-1)//2ret=N*N*N-3*N*(n)*(n-1)-N-3*N*(N-1)ans=retmod=1<<64if(A==BandB==C):......
  • 用acme.sh给网站域名,申请免费SSL永久证书
    安装acme.sh1:在线安装方式curlhttps://get.acme.sh|[email protected]或者wget-0-https://get.acme.sh|[email protected]这里的-s参数指定的邮箱可以关联到已有的zeroSSL账号。关联成功后,通过acme.sh生成的zeroSSL证书会在zeroSSL网站的控......
  • 2018ACM浙江省赛 ZOJ 4029 Now Loading!!!(二分)
    NowLoading!!!TimeLimit: 1Second     MemoryLimit: 131072KBDreamGridhas  integers .DreamGridalsohas foragivennumber ,where , .InputTherearemultipletestcases.Thefirstlineofinputisaninteger Thefirstlinecon......
  • 洛阳师范学院ACM22级暑假前最后一次周测
    玩的开心B一个难pizzaHDU-1097HDU-1097正解是:枚举0-9每个数的次方循环0123456789100:100000000001:111111111112:124862486243:139713971394:146464646465:155555555556:16......
  • 2023年国际大学生程序设计竞赛(ACM-ICPC)新疆赛区 A.The Number Of Black Edges
    传送门大致题意:  爱丽丝得到一棵树,树上有n个节点,索引从1到n。树上的每条边可以是黑色或白色,所有的边最初都是白色的。有三种操作:1.将一条边的颜色改为黑色。2.将一条边的颜色改为白色。3.3.给定一个节点索引,计算从所有奇数节点到该节点的简单路径上的黑色边的数量之和。请......
  • 河北工业大学 ACM 集训队 2023 年夏季选拔 题解 12/12
    https://ac.nowcoder.com/acm/contest/59007A假设数字n有len位则小len的长度,每个都有九个方案。长度和len一样的,至少有n[0]-1种方案n[0]n[0]n[0]...的这个方案暴力地跑一遍看看是不是小于等于n即可#include<bits/stdc++.h>usingnamespacestd;typedeflonglongll;in......
  • acme.sh数据迁移
    acme.sh数据迁移1.1.安装脚本在新服务器安装acme.sh脚本工具curlhttps://get.acme.sh|sh#orwget-O-https://get.acme.sh|shll~/.acme.sh/aliasacme.sh=~/.acme.sh/acme.shll~/.acme.sh/1.2.打包备份acme.sh数据cd/root/ll-atar-zcvfacme.sh......
  • 2023春ACM组队训练1
    训练地址:训练一具体代码见提交B.明明的字符串可见abc242E求解小于等于一个字符串的回文串的个数就是有一个小小的区别,一个判断是小于等于,一个是小于 cin>>n>>k>>ss; rss=ss; lll=ss.size(),sum=0,len=l; l=(l-1)/2; for(inti=0;i<=l;++i){ rss[len-1-i]=rss[i]; s......
  • 使用Pandoc构建Acm模板
    使用Pandoc构建Acm模板下周日打完河南ICPC省赛就要退役了,以后一场比赛前想要整理一下板子,想要一个拥有目录,页眉。页脚的Acm模板,这样就可以在比赛的时候快速翻阅,而且要更加好看但是存在的问题是:很多构建Acm模板的时候会使用Latex进行构建,但是我使用了很多,要么是些许麻烦,也许是我......
  • ACM板子(1)(缺最短路、计算几何、数学、高级数据结构)
    ACM板子(1)(缺最短路、计算几何、数学、高级数据结构)快排、归并voidquicksort(int*num,intl,intr){if(r<=l)return;intx=l-1,y=r+1,z=num[l+r>>1];while(x<y){dox++;while(num[x]<z);doy--;while(num[y]>z);if(x<y)s......