问题 C: 解救小哈

在这里插入图片描述
在这里插入图片描述

/*dfs*/
#include<bits/stdc++.h>
using namespace std;
int m,n,p,q,minn;
int a[60][60],book[60][60];
void dfs(int x,int y,int step);
int main(){
	int i,j,startx,starty;
	while(cin>>m>>n){
		minn=9999999;
		for(i=0;i<m;i++){
			for(j=0;j<n;j++){
				cin>>a[i][j];
			}
		}
		cin>>startx>>starty>>p>>q;
		startx--;
		starty--;
		p--;
		q--;
		book[startx][starty]=1;
		dfs(startx,starty,0);
		if(minn==9999999){
			cout<<"No Way!"<<endl;
		}
		else
		cout<<minn<<endl;
	}
	return 0;
}
void dfs(int x,int y,int step){
	int next[4][2]={{1,0},{0,1},{-1,0},{0,-1}};
	int tx,ty,k;
	if(x==p and y==q){
		if(step<minn)
		minn=step;
		return ;
	}
	for(k=0;k<4;k++){
		tx=x+next[k][0];
		ty=y+next[k][1];
		if(tx<0 or tx>m-1 or ty<0 or ty>n-1)
		continue;
		if(a[tx][ty]==0 and book[tx][ty]==0){
			book[tx][ty]=1;
			dfs(tx,ty,step+1);
			book[tx][ty]=0;
		}
	}
	return ;
}
/*个人版dfs*/
#include<bits/stdc++.h>
using namespace std;
int m,n,q,p,minn;
int a[60][60],book[60][60];
void dfs(int x,int y,int step); 
int main(){
	while(cin>>m>>n){
		minn=9999999;
		for(int i=0;i<m;i++){
			for(int j=0;j<n;j++){
				cin>>a[i][j];
			}
		}
		int startx,starty;
		cin>>startx>>starty>>p>>q;
		startx--;
		starty--;
		p--;
		q--;
		dfs(startx,starty,0);
		if(minn==9999999){
			cout<<"No Way!"<<endl;
		}
		else{
			cout<<minn<<endl;
		}
	}
	return 0;
}
void dfs(int x,int y,int step){
	int next[4][2]={{1,0},{0,1},{-1,0},{0,-1}};//逆时针
	int tx,ty;
	if(x==p and y==q){
		if(step<minn)
		minn=step;
		return ;
	}
	for(int k=0;k<4;k++){
		tx=x+next[k][0];
		ty=y+next[k][1];
		if(tx<0 or tx>m-1 or ty<0 or ty>n-1)
		continue;
		if(a[tx][ty]==0 and book[tx][ty]==0){
			book[tx][ty]=1;
			dfs(tx,ty,step+1);
			book[tx][ty]=0;
		}
	}
	return ;
}
/*bfs*/
#include<bits/stdc++.h>
using namespace std;
typedef struct note{
	int x;
	int y;
	int step;
}notes;
int main(){
	notes que[3000];
	int a[60][60],book[60][60];
	int next[4][2]={1,0,-1,0,0,1,0,-1};
	int head,tail,i,j,k,m,n,p,q,startx,starty,tx,ty,flag;
	while(cin>>m>>n){
		for(i=0;i<m;i++){
			for(j=0;j<n;j++){
				cin>>a[i][j];
			}
		}
		cin>>startx>>starty>>p>>q;
		startx--;
		starty--;
		p--;
		q--;
		head=0;
		tail=0;
		que[tail].x=startx;
		que[tail].y=starty;
		que[tail].step=0;
		tail++;
		book[startx][starty]=1;
		flag=0;
		while(head<tail){
			for(k=0;k<4;k++){//该循环将宽度搜索的点塞入数组 
				tx=que[head].x+next[k][0];
				ty=que[head].y+next[k][1];
				if(tx<0 or tx>m-1 or ty<0 or ty>n-1)
				continue;
				if(a[tx][ty]==0 and book[tx][ty]==0){
					book[tx][ty]=1;
					que[tail].x=tx;
					que[tail].y=ty;
					que[tail].step=que[head].step+1;
					tail++;
				}
				if(tx==p and ty==q){
					flag=1;
					break;
				}
			}
			if(flag==1)
			break;
			head++;
		}
		cout<<que[tail-1].step<<endl;//只要第一个到达终点即最短 
	}
	return 0;
} 
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值