[作业]深度搜索1

 Tips:题目下方没有就是还没写。

#include <iostream>
using namespace std;
int n;
bool b[110][110]={0};
int sx,sy,ex,ey;
bool f=0;
int dx[4]={0,1,0,-1},dy[4]={1,0,-1,0};
void dfs(int,int);
int main(){
	cin>>n;
	for(int i=0;i<=n+1;i++){
		for(int j=0;j<=n+1;j++){
			if(i==0||j==0||i==n+1||j==n+1){
				b[i][j]=1;
			}
			else{
				cin>>b[i][j];
			}
		}
	}
	cin>>sx>>sy>>ex>>ey;
	dfs(sx,sx);
	if(f)cout<<"YES"<<endl;
	else cout<<"NO"<<endl;
	
	return 0;
}
void dfs(int x,int y){
	if(f)return;
	if(x==ex&&y==ey){
		f=1;
		return;
	}
	
	for(int i=0;i<4;i++){
		int tx=x+dx[i],ty=dy[i]+y;
		b[x][y]=1;
		if(b[tx][ty]==0)dfs(tx,ty);
		b[x][y]=0;
	}
	
}

#include <iostream>
using namespace std;
int w,h;
char b[110][110]={0};
bool p[110][110]={0};
int sx,sy;
int dx[4]={0,1,0,-1},dy[4]={1,0,-1,0};
void dfs(int,int);
int main(){//not finished
	cin>>w>>h;
	for(int i=0;i<=h+1;i++){
		for(int j=0;j<=w+1;j++){
			if(i==0||j==0||i==h+1||j==w+1){
				b[i][j]='#';//wall
			}
			else{
				cin>>b[i][j];
				if(b[i][j]=='@'){
					sx=i;
					sy=j;
				}
			}
		}
	}
	dfs(sx,sy);
	int cnt=0;
	for(int i=1;i<=h;i++){
		for(int j=1;j<=w;j++){
			if(b[i][j])cnt++;
		}
	}
	cout<<cnt<<endl;
	return 0;
}
void dfs(int x,int y){
	p[x][y]=1;
	for(int i=0;i<4;i++){
		int tx=x+dx[i],ty=dy[i]+y;
		b[x][y]='#';
		if(b[tx][ty]!='#'){
			p[tx][ty]=1;
			dfs(tx,ty);
		}
		b[x][y]='.';
	}
	
}

#include <iostream>
using namespace std;
int w,h;
char b[110][110]={0};
bool p[110][110]={0};
int sx,sy;
int dx[4]={0,1,0,-1},dy[4]={1,0,-1,0};
void dfs(int,int);
int main(){//not finished
	cin>>h>>w;
	for(int i=0;i<=h+1;i++){
		for(int j=0;j<=w+1;j++){
			if(i==0||j==0||i==h+1||j==w+1){
				b[i][j]='.';//wall
			}
			else{
				cin>>b[i][j];
				
			}
		}
	}
	int cnt=0;
	for(int i=1;i<=h;i++){
		for(int j=1;j<=w;j++){
			if(b[i][j]=='W'){
				cnt++;
				dfs(i,j);
			}
		}
	}
	cout<<cnt;
	return 0;
}
void dfs(int x,int y){
	b[x][y]='.';
	for(int i=0;i<4;i++){
		int tx=x+dx[i],ty=dy[i]+y;
		b[x][y]='.';
		if(b[tx][ty]=='W'){
			b[tx][ty]='.';
			dfs(tx,ty);
		}
	}
}

#include <iostream>
using namespace std;
int n;
int t=0,mi=2147483647;
int b[110][110]={0};
int sx,sy,ex,ey;
bool f=0;
int dx[4]={0,1,0,-1},dy[4]={1,0,-1,0};
void dfs(int,int);
int main(){
	cin>>n;
	for(int i=0;i<=n+1;i++){
		for(int j=0;j<=n+1;j++){
			if(i==0||j==0||i==n+1||j==n+1){
				b[i][j]=-1;
			}
			else{
				cin>>b[i][j];
			}
		}
	}
	sx=1;
	sy=1;
	ex=n;
	ey=n;
	dfs(sx,sx);
	cout<<t<<endl;
	return 0;
}
void dfs(int x,int y){
	if(x==ex&&y==ey){
		f=1;
		return;
	}
	if(f)return;
	for(int i=0;i<4;i++){
		int tx=x+dx[i],ty=dy[i]+y,tem=b[x][y];
		b[x][y]=-1;
		if(b[tx][ty]!=-1&&b[tx][ty]<=t){
			dfs(tx,ty);
		}
		else if(b[tx][ty]!=-1)mi=min(mi,b[tx][ty]);
		b[x][y]=tem;
	}
	if(x==sx&&y==sy&&f==0){
		t=mi;
		mi=2147483647;
		dfs(x,y);
	}
}

#include <iostream>
using namespace std;
int n,m;
int r=0;
int t=0,ma=0;
bool b[110][110]= {0};
int w[110][110];
int sx,sy,ex,ey;
bool f=0;
int dx[4]= {0,-1,0,1},dy[4]= {-1,0,1,0};
void dfs(int,int);
int main() {
	cin>>m>>n;
	for(int i=1; i<=m; i++) {
		for(int j=1; j<=n; j++) {
			cin>>w[i][j];
		}
	}
	int cnt=0;
	for(int i=1; i<=m; i++) {
		for(int j=1; j<=n; j++) {
			if(b[i][j]==0){
				r=0;
				dfs(i,j);
				cnt++;
				ma=max(r,ma);
			}
		}
	}
	cout<<cnt<<endl<<ma<<endl;
	return 0;
}
void dfs(int x,int y) {
	r++;
	bool a[4];
	int t=w[x][y];
	for(int i=0; i<4; i++) {
		a[i]=t%2;
		t/=2;
	}
	for(int i=0; i<4; i++) {
		int tx=x+dx[i],ty=dy[i]+y;
		b[x][y]=1;
		if(!a[i]&&b[tx][ty]==0)dfs(tx,ty);
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值