N诺刷题---搜索

枚举

1348 1165 1274

水题

广度优先搜索

1563

几个错误记录:
迷宫图有上下左右,所以应该弄大一点,不然数组越界i=1,j=1
数组从1开始后,字符串输入问题,j-1

#include<iostream>
#include<string>
#include<cstring>
#include<stack>
#include<vector>
#include <queue>
#include <functional>
using namespace std;

const int maxn=100+5;
char mpt[maxn][maxn];
int vis[maxn][maxn];
int dir[4][2]=
{
	{0,-1},
	{0,1},
	{-1,0},
	{1,0}
};
struct node
{
	int x,y;
	int step;
};

int bfs(int sx,int sy)
{
	queue<node>q;
	memset(vis,0,sizeof(vis));
	struct node t;
	t.x=sx;t.y=sy;t.step =0;
	q.push(t);
	vis[sx][sy]=1;
	int ans=-1;
	while(!q.empty())
	{
		node now=q.front();
		q.pop();
		if(mpt[now.x][now.y]=='E')
		{
			ans=now.step ;
			break;
		}
		for(int i=0;i<4;i++)
		{
			int nx=now.x+dir[i][0];
			int ny=now.y+dir[i][1];
			if((mpt[nx][ny]=='*'||mpt[nx][ny]=='E')&&vis[nx][ny]==0)
			{
				node f;
				f.x =nx;f.y=ny;f.step =now.step+1;
				q.push(f);
				vis[nx][ny]=1;
			}
		}
	}
	return ans;
}

int main()
{
	int h,w;
	int sx=0,sy=0;
	string s;
	while(cin>>h>>w)
	{
		if(h==0&&w==0)
			break;
		memset(mpt,0,sizeof(mpt));
		for(int i=1;i<=h;i++)
		{
			cin>>s;
			for(int j=1;j<=w;j++)
			{
				mpt[i][j]=s[j-1];

				if(mpt[i][j]=='S')
				{
					sx=i;sy=j;
				}
			}
		}
		int ans=bfs(sx,sy);
		cout<<ans<<endl;
	}
	return 0;
}

1308

#include<iostream>
#include<string>
#include<string.h>
#include<cstring>
#include<stack>
#include<vector>
#include <queue>
#include <functional>
using namespace std;
const int maxn=105;
int a[maxn][maxn][4];
char mpt[maxn][maxn];

struct node
{
	int x;
	int y;
}peop[4];
int fangxiang[4][2]={0,-1,0,1,1,0,-1,0};

//计算该点到任意一点的最少障碍物
void bfs(int index)
{
	queue<node>q;
	node now,next;
	now.x=peop[index].x;
	now.y=peop[index].y;
	q.push(now);
	a[now.x][now.y][index]=0;
	//每到达一个点进行全图更新
	while(!q.empty())
	{
		now=q.front ();
		q.pop();
		for(int i=0;i<4;i++)
		{
			next.x=now.x+fangxiang[i][0];
			next.y=now.y+fangxiang[i][1];

			//防止地图越界
			if(mpt[next.x][next.y]=='0')
				continue;

			int temp=a[now.x][now.y][index];
			if(mpt[next.x][next.y]=='#')
				temp++;
			if(temp<a[next.x][next.y][index])
			{
				a[next.x][next.y][index]=temp;
				q.push(next);
			}		
		}
	}
}
int main()
{
	int n,m;
	while(cin>>n>>m)
	{
		string s;
		fill(a[0][0],a[0][0]+maxn*maxn*4,1000);
		//构造初始地图
		memset(mpt,'0',sizeof(mpt));
		for(int i=1;i<=n;i++)
		{
			cin>>s;
			for(int j=1;j<=m;j++)
			{
				mpt[i][j]=s[j-1];
				if(s[j-1]=='w'){peop[1].x=i;peop[1].y=j;}
				else if(s[j-1]=='W'){peop[2].x=i;peop[2].y=j;}
				else if(s[j-1]=='f'){peop[3].x=i;peop[3].y=j;}
			}
		}
		bfs(1);
		bfs(2);
		bfs(3);

		int ans=99999;
		for (int i=1;i<=n;i++)
		{
			for(int j=1;j<=m;j++)
			{
				int temp=0;
				for(int k=1;k<4;k++)
					temp+=a[i][j][k];
				if(mpt[i][j]=='#')
					temp-=2;
				if(temp<ans)
					ans=temp;			
			}	
		}
		cout<<ans<<endl;
	}
}

1124

难 留着

1126

记录
fill函数用法
早已充满的判断

#include<iostream>
#include<string>
#include<string.h>
#include<cstring>
#include<stack>
#include<vector>
#include <queue>
#include <functional>
using namespace std;
const int maxn=10000;

char mpt[maxn][maxn];
int sum=0,flag;
struct node
{
	int x;
	int y;
	int step;
}beg;
int fangxiang[4][2]={0,-1,0,1,1,0,-1,0};

void bfs(int t)
{
	queue<node>q;
	node now,next;
	q.push(beg);
	mpt[beg.x][beg.y]='#';
	flag=0;
	while(!q.empty())
	{
		now=q.front ();
		q.pop();

		if(now.step==t)
			flag=1;//刚好充满或未充满,早已全部充满时step是达不到t的

		if(now.step>=t)
			continue;

		for(int i=0;i<4;i++)
		{
			next=now;
			next.x+=fangxiang[i][0];
			next.y+=fangxiang[i][1];

			//防止地图碰墙
			if(mpt[next.x][next.y]=='X'||mpt[next.x][next.y]=='#')
				continue;
			else if(mpt[next.x][next.y]=='.')
			{
				mpt[next.x][next.y]='#';
				next.step ++;
				q.push(next);
			}
		}
	}
}
int main()
{
	int m,n,t;
	while(cin>>n>>m>>t)
	{
		//构造整个地图
		fill(mpt[0],mpt[0]+maxn*maxn,'0');
		for(int i=1;i<=n;i++)
		{
			string s;
			cin>>s;
			for (int j=1;j<=s.size();j++)
			{
				mpt[i][j]=s[j-1];
				if(s[j-1]=='*'){beg.x=i;beg.y=j;beg.step =0;}
			}
		}
		bfs(t);
		if(flag==1)
		{
			for(int i=1;i<=n;i++)
			{
				for (int j=1;j<=m;j++)
				{
					if(mpt[i][j]!='0')
						cout<<mpt[i][j];
				}
				cout<<endl;
			}
		}
		else
		{
			cout<<"No"<<endl;
		}
		cout<<endl;
	}
}

递归

1082

经典递归
汉诺塔

#include<iostream>
#include<string>
#include<cstring>
#include<stack>
#include<vector>
#include <queue>
#include <functional>
using namespace std;
int step;
void hannuo(int n,char a,char b,char c)
{
	if(n==1)
	{
		if((step+1)%5==0)
			cout<<a<<"-->"<<c<<endl;
		else
			cout<<a<<"-->"<<c<<"   ";
		step++;
		return;
	}
	hannuo(n-1,a,c,b);
	hannuo(1,a,b,c);
	hannuo(n-1,b,a,c);
}

int main()
{
	int n;
	while(cin>>n)
	{
		step=0;
		if(n==0)
			break;
		hannuo(n,'A','B','C');
		cout<<endl;
	}
	return 0;
}

1185全排列

经典算法
在c++里可使用next_permutation()
P234

#include<iostream>
using namespace std;
string s;
int a[10];//标记字母
int b[10];
void paixu(int step,int n)
{
	if(step==n)
	{
		for(int i=0;i<n;i++)
			cout<<s[b[i]];
		cout<<endl;
	}
	for(int i=0;i<n;i++)
	{
		if(a[i]==0)
		{
			a[i]=1;
			b[step]=i;
			paixu(step+1,n);
			a[i]=0;
		}
	}
}
int main()
{
	while(cin>>s)
		paixu(0,s.size());
	return 0;
}

深度优先搜索

1564

这道题只要求判断连通性质,无回溯

#include<iostream>
#include<string>
#include<string.h>
#include<cstring>
#include<stack>
#include<vector>
#include <queue>
#include <functional>
using namespace std;
int ans;
const int maxn=100+5;
char mpt[maxn][maxn];
int vis[maxn][maxn];
int dir[8][2]=
{
	{0,-1},
	{0,1},
	{-1,0},
	{1,0},
	{-1,1},
	{-1,-1},
	{1,-1},
	{1,1}
};

void dfs(int x,int y)
{
	vis[x][y]=1;
	for(int i=0;i<8;i++)
	{
		int nx=x+dir[i][0];
		int ny=y+dir[i][1];
		if(mpt[nx][ny]=='@' && vis[nx][ny]==0)
			dfs(nx,ny);
	}
}
int main()
{
	int h,w;
	int sx=0,sy=0;
	string s;
	while(cin>>h>>w)
	{
		if(h==0&&w==0)
			break;
		memset(mpt,0,sizeof(mpt));
		memset(vis,0,sizeof(vis));
		ans=0;
		//存入地图
		for(int i=1;i<=h;i++)
		{
			cin>>s;
			for(int j=1;j<=w;j++)
				mpt[i][j]=s[j-1];
		}
		//浏览地图
		for(int i=1;i<=h;i++)
		{
			for(int j=1;j<=w;j++)
				if(mpt[i][j]=='@'&&vis[i][j]==0)
				{
					ans++;
					dfs(i,j);
				}
		}
		cout<<ans<<endl;
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

王蒟蒻

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值