搜索

这些是板子,防止自己忘掉 实际操作中不限于这些

dfs

int check(int x,int y){
	if(x<1||x>n||y<1||y>m)return 0;
	if(vis[x][y]||s[x][y]=='.')return 0;
	return 1;
}

void dfs(int x,int y){
	for(int i=0;i<4;i++){
		if(check(x+x1[i],y+y1[i])){
			vis[x+x1[i]][y+y1[i]]=1;
			dfs(x+x1[i],y+y1[i]);
		}
	}
}

bfs

const int maxn=100;
int vis[maxn][maxn];
int step[maxn][maxn];
int s[maxn][maxn];
struct node{
	int x,y,cnt;
};
node fa[maxn][maxn];
int x1[]={-1,1,0,0};
int y1[]={0,0,-1,1};
int n,m;

int check(int x,int y){
	if(x<1||x>n||y<1||y>m)return 0;
	if(vis[x][y]||s[x][y]=='.')return 0;
	return 1;
}

int bfs(){
	memset(vis,0,sizeof vis);
	node now;
	now.x=1;now.y=1;now.cnt=0;
	queue<node>q;
	vis[now.x][now.y]=1;
	q.push(now);
	while(!q.empty()){
		now=q.front();
		q.pop();
		step[now.x][now.y]=now.cnt;
//		if(now.cnt==)return now.cnt;
		for(int i=0;i<4;i++){
			node next;
			if(check(now.x+x1[i],now.y+y1[i])){
				next.x=now.x+x1[i];next.y=now.y+y1[i];
				vis[next.x][next.y]=1;
				fa[next.x][next.y]=now;
				q.push(next);
			}
		}
	}
	return -1;
}

void print(node now){
	stack<node>s;
	for(;;){
		s.push(now);
	//	if(now.x==&&now.y==)break;
		now=fa[now.x][now.y];
	}
	while(!s.empty()){
		now=s.top();
		//printf()
		s.pop();
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值