2019.3.7 kuangbin训练 UVA11624 POJ3984 HDU1241 HDU1495 HDU1612

UVA11624
队列中先加火再加人,node中state=0是人走过的格子,state=1是火走过的格子
坑点:
可能有多处火

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <queue>
using namespace std;
const int maxn=1e6;
int T,N,M;
char m[1007][1007];
struct node
{
	int x,y,step,state;
	node(int xx=0,int yy=0,int ss=0,int st=0)
	{
		x=xx;y=yy;step=ss;state=st;
	}
};
node J,F[maxn+7];
int cnt;// num of F
int vis[1007][1007];
int X[]={-1,0,1,0};
int Y[]={0,1,0,-1};
int ans;
void bfs()
{
	queue<node> q;
	while(!q.empty()) q.pop();
	for(int i=0;i<cnt;i++)
	{
		q.push(F[i]);
		vis[F[i].x][F[i].y]=1;
	}
	q.push(J);
	vis[J.x][J.y]=1;
	while(!q.empty())
	{
		node temp=q.front();
		q.pop();
		if((temp.x==0 || temp.x==N-1 || temp.y==0 || temp.y==M-1)&& temp.state==0 )
		{
            ans=temp.step;
            return;
		}
		for(int i=0;i<4;i++)
		{
			int nx=temp.x+X[i];
			int ny=temp.y+Y[i];
			if(nx<0 || nx>=N || ny<0 || ny>=M)
				continue;
			if(!vis[nx][ny] && m[nx][ny]=='.')
			{
				vis[nx][ny]=1;
				q.push(node(nx,ny,temp.step+1,temp.state));
			}

		}
	}
}
int main()
{
	scanf("%d",&T);
	while(T--)
	{
		ans=-1;
		cnt=0;
		memset(vis,0,sizeof(vis));
		scanf("%d %d",&N,&M);
		for(int i=0;i<N;i++)
		{
			scanf("%s",m[i]);
			for(int j=0;j<M;j++)
			{
				if(m[i][j]=='J')
				{
					J.x=i;
					J.y=j;
					J.step=0;
					J.state=0;
				}
				if(m[i][j]=='F')
				{
					F[cnt].x=i;
					F[cnt].y=j;
					F[cnt].step=0;
					F[cnt].state=1;
					cnt++;
				}
			}

		}
		bfs();
		if(ans==-1)
			printf("IMPOSSIBLE\n");
		else printf("%d\n",ans+1);
	}
	return 0;
}

poj3984
水题,bfs,pre的用法之前写过

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <queue>
using namespace std;
const int maxn=1e5+7;
int m[5][5],vis[5][5];
int n=5;
int X[]={-1,0,1,0};
int Y[]={0,1,0,-1};
struct node
{
	int x,y;
	node (int xx=0,int yy=0)
	{
		x=xx;y=yy;
	}
};
node pre[5][5];
void bfs()
{
	queue<node> q;
	while(!q.empty()) q.pop();
	q.push(node(0,0));
	vis[0][0]=1;
	pre[0][0]=node(-1,-1);
	while(!q.empty())
	{
		node temp=q.front();
		q.pop();
		if(temp.x==n-1 && temp.y==n-1)
			break;
		for(int i=0;i<4;i++)
		{
			int nx=temp.x+X[i];
			int ny=temp.y+Y[i];
			if(nx<0 || nx>=n || ny<0 || ny>=5)
				continue;
			if(!vis[nx][ny] && m[nx][ny]==0)
			{
				vis[nx][ny]=1;
				pre[nx][ny]=temp;
				q.push(node(nx,ny));
			}
		}
	}
}
void print(int x,int y)
{
	if(x==-1 && y==-1)
		return;
	print(pre[x][y].x,pre[x][y].y);
	printf("(%d, %d)\n",x,y);
}
int main()
{
	for(int i=0;i<n;i++)
		for(int j=0;j<n;j++)
			scanf("%d",&m[i][j]);
	bfs();
	print(4,4);
	return 0;
}

hdu1241
水题,dfs,8个方向

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <queue>
using namespace std;
const int maxn=1e5+7;
int n,m;
char maze[107][107];
int X[]={-1,-1,0,1,1,1,0,-1};
int Y[]={0,1,1,1,0,-1,-1,-1};
int ans;

void dfs(int x,int y)
{
	maze[x][y]='*';
	for(int i=0;i<8;i++)
	{
		int nx=x+X[i];
		int ny=y+Y[i];
		if(nx<0 || nx>=n || ny<0 || ny>=m)
			continue;
		if(maze[nx][ny]=='@')
			dfs(nx,ny);
	}
}
int main()
{
	while(scanf("%d %d",&n,&m)!=EOF && (n+m))
	{
		ans=0;
		for(int i=0;i<n;i++)
			scanf("%s",maze[i]);
		for(int i=0;i<n;i++)
		{
			for(int j=0;j<m;j++)
				if(maze[i][j]=='@')
				{
					dfs(i,j);
					ans++;
				}
		}
		printf("%d\n",ans);
	}
	return 0;
}

hdu1495
bfs,模拟6种倒水方式

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <queue>
using namespace std;
const int maxn=1e5+7;
int N,M,S;
int vis[107][107][107];
int ans,ansS,ansN,ansM;
struct node
{
	int s,n,m,step;
	node(int ss,int nn,int mm,int st)
	{
		n=nn;m=mm;s=ss;step=st;
	}
};
void bfs()
{
	queue<node> q;
	while(!q.empty()) q.pop();
	q.push(node(S,0,0,0));
	vis[S][0][0]=1;
	while(!q.empty())
	{
		node temp=q.front();
		q.pop();
		//printf("(%d %d %d %d)\n",temp.s,temp.n,temp.m,temp.step);
		if(temp.s==S/2 || temp.n==S/2 || temp.m==S/2)
		{
			ans=temp.step;
			ansS=temp.s;
			ansN=temp.n;
			ansM=temp.m;
			break;
		}
		//S->N
		if(temp.s+temp.n>N)
		{
			if(!vis[temp.s+temp.n-N][N][temp.m])
			{
				vis[temp.s+temp.n-N][N][temp.m]=1;
				q.push(node(temp.s+temp.n-N,N,temp.m,temp.step+1));
			}
		}
		else
		{
			if(!vis[0][temp.s+temp.n][temp.m])
			{
				vis[0][temp.s+temp.n][temp.m]=1;
				q.push(node(0,temp.s+temp.n,temp.m,temp.step+1));
			}
		}
		//S->M
		if(temp.s+temp.m>M)
		{
			if(!vis[temp.s+temp.m-M][temp.n][M])
			{
				vis[temp.s+temp.m-M][temp.n][M]=1;
				q.push(node(temp.s+temp.m-M,temp.n,M,temp.step+1));
			}		
		}
		else
		{
			if(!vis[0][temp.n][temp.s+temp.m])
			{
				vis[0][temp.n][temp.s+temp.m]=1;
				q.push(node(0,temp.n,temp.s+temp.m,temp.step+1));
			}
		}
		//N->S
		if(!vis[temp.s+temp.n][0][temp.m])
		{
			vis[temp.s+temp.n][0][temp.m]=1;
			q.push(node(temp.s+temp.n,0,temp.m,temp.step+1));
		}
		//M->S
		if(!vis[temp.s+temp.m][temp.n][0])
		{
			vis[temp.s+temp.m][temp.n][0]=1;
			q.push(node(temp.s+temp.m,temp.n,0,temp.step+1));
		}
		//N->M
		if(temp.n+temp.m>M)
		{
			if(!vis[temp.s][temp.n+temp.m-M][M])
			{
				vis[temp.s][temp.n+temp.m-M][M]=1;
				q.push(node(temp.s,temp.n+temp.m-M,M,temp.step+1));
			}
		}
		else
		{
			if(!vis[temp.s][0][temp.n+temp.m])
			{
				vis[temp.s][0][temp.n+temp.m]=1;
				q.push(node(temp.s,0,temp.n+temp.m,temp.step+1));
			}
		}
		//M->N
		if(temp.n+temp.m>N)
		{
			if(!vis[temp.s][N][temp.n+temp.m-N])
			{
				vis[temp.s][N][temp.n+temp.m-N]=1;
				q.push(node(temp.s,N,temp.n+temp.m-N,temp.step+1));
			}
		}
		else
		{
			if(!vis[temp.s][temp.n+temp.m][0])
			{
				vis[temp.s][temp.n+temp.m][0]=1;
				q.push(node(temp.s,temp.n+temp.m,0,temp.step+1));
			}
		}
		
	}	
} 
int main()
{
	while(scanf("%d %d %d",&S,&N,&M)!=EOF && (S+N+M))
	{
		if(S%2)
		{
			printf("NO\n");
			continue;
		}
		memset(vis,0,sizeof(vis));
		ans=-1;
		bfs();
		if(ans==-1)
			printf("NO\n");
		else
		{
			if(!ansS || !ansN || !ansM)
				printf("%d\n",ans);
			else printf("%d\n",ans+1);
		}
		
		
	}
	return 0;
}

hdu2612

全部评论

相关推荐

刚刷到字节跳动官方发的消息,确实被这波阵仗吓了一跳。在大家还在纠结今年行情是不是又“寒冬”的时候,字节直接甩出了史上规模最大的转正实习计划——ByteIntern。咱们直接看几个最硬的数,别被花里胡哨的宣传词绕晕了。首先是“量大”。全球招7000多人是什么概念?这几乎是把很多中型互联网公司的总人数都给招进来了。最关键的是,这次的资源分配非常精准:研发岗给了4800多个Offer,占比直接超过六成。说白了,字节今年还是要死磕技术,尤其是产品和AI领域,这对于咱们写代码的同学来说,绝对是今年最厚的一块肥肉。其次是大家最关心的“转正率”。官方直接白纸黑字写了:整体转正率超过50%。这意味着只要你进去了,不划水、正常干,每两个人里就有一个能直接拿校招Offer。对于2027届(2026年9月到2027年8月毕业)的同学来说,这不仅是实习,这简直就是通往大厂的快捷通道。不过,我也得泼盆冷水。坑位多,不代表门槛低。字节的实习面试出了名的爱考算法和工程实操,尤其是今年重点倾斜AI方向,如果你简历里有和AI相关的项目,优势还是有的。而且,转正率50%也意味着剩下那50%的人是陪跑的,进去之后的考核压力肯定不小。一句话总结:&nbsp;27届的兄弟们,别犹豫了。今年字节这是铁了心要抢提前批的人才,现在投递就是占坑。与其等到明年秋招去千军万马挤独木桥,不如现在进去先占个工位,把转正名额攥在手里。
喵_coding:别逗了 50%转正率 仔细想想 就是转正与不转正
字节7000实习来了,你...
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务